ソースを参照

support closing sockets explicitly

David Rose 22 年 前
コミット
5e5abe8c50

+ 0 - 36
panda/src/downloader/bioStream.I

@@ -48,18 +48,6 @@ open(BioPtr *source) {
   return *this;
 }
 
-////////////////////////////////////////////////////////////////////
-//     Function: IBioStream::close
-//       Access: Public
-//  Description: Resets the BioStream to empty, but does not actually
-//               close the source BIO unless owns_source was true.
-////////////////////////////////////////////////////////////////////
-INLINE IBioStream &IBioStream::
-close() {
-  _buf.close();
-  return *this;
-}
-
 ////////////////////////////////////////////////////////////////////
 //     Function: OBioStream::Constructor
 //       Access: Public
@@ -91,18 +79,6 @@ open(BioPtr *source) {
   return *this;
 }
 
-////////////////////////////////////////////////////////////////////
-//     Function: OBioStream::close
-//       Access: Public
-//  Description: Resets the BioStream to empty, but does not actually
-//               close the source BIO unless owns_source was true.
-////////////////////////////////////////////////////////////////////
-INLINE OBioStream &OBioStream::
-close() {
-  _buf.close();
-  return *this;
-}
-
 ////////////////////////////////////////////////////////////////////
 //     Function: BioStream::Constructor
 //       Access: Public
@@ -133,15 +109,3 @@ open(BioPtr *source) {
   _buf.open(source);
   return *this;
 }
-
-////////////////////////////////////////////////////////////////////
-//     Function: BioStream::close
-//       Access: Public
-//  Description: Resets the BioStream to empty, but does not actually
-//               close the source BIO unless owns_source was true.
-////////////////////////////////////////////////////////////////////
-INLINE BioStream &BioStream::
-close() {
-  _buf.close();
-  return *this;
-}

+ 33 - 0
panda/src/downloader/bioStream.cxx

@@ -37,6 +37,17 @@ is_closed() {
   return false;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: IBioStream::close
+//       Access: Public, Virtual
+//  Description: Resets the BioStream to empty, but does not actually
+//               close the source BIO unless owns_source was true.
+////////////////////////////////////////////////////////////////////
+void IBioStream::
+close() {
+  _buf.close();
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: OBioStream::is_closed
 //       Access: Public, Virtual
@@ -54,6 +65,17 @@ is_closed() {
   return false;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: OBioStream::close
+//       Access: Public, Virtual
+//  Description: Resets the BioStream to empty, but does not actually
+//               close the source BIO unless owns_source was true.
+////////////////////////////////////////////////////////////////////
+void OBioStream::
+close() {
+  _buf.close();
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: BioStream::is_closed
 //       Access: Public, Virtual
@@ -71,4 +93,15 @@ is_closed() {
   return false;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: BioStream::close
+//       Access: Public, Virtual
+//  Description: Resets the BioStream to empty, but does not actually
+//               close the source BIO unless owns_source was true.
+////////////////////////////////////////////////////////////////////
+void BioStream::
+close() {
+  _buf.close();
+}
+
 #endif  // HAVE_SSL

+ 3 - 3
panda/src/downloader/bioStream.h

@@ -42,9 +42,9 @@ public:
   INLINE IBioStream(BioPtr *source);
 
   INLINE IBioStream &open(BioPtr *source);
-  INLINE IBioStream &close();
 
   virtual bool is_closed();
+  virtual void close();
 
 private:
   BioStreamBuf _buf;
@@ -65,9 +65,9 @@ public:
   INLINE OBioStream(BioPtr *source);
 
   INLINE OBioStream &open(BioPtr *source);
-  INLINE OBioStream &close();
 
   virtual bool is_closed();
+  virtual void close();
 
 private:
   BioStreamBuf _buf;
@@ -84,9 +84,9 @@ public:
   INLINE BioStream(BioPtr *source);
 
   INLINE BioStream &open(BioPtr *source);
-  INLINE BioStream &close();
 
   virtual bool is_closed();
+  virtual void close();
 
 private:
   BioStreamBuf _buf;

+ 32 - 30
panda/src/downloader/bioStreamBuf.cxx

@@ -194,39 +194,41 @@ underflow() {
 ////////////////////////////////////////////////////////////////////
 size_t BioStreamBuf::
 write_chars(const char *start, size_t length) {
-  size_t wrote_so_far = 0;
-
-  int write_count = BIO_write(*_source, start, length);
-  while (write_count != (int)(length - wrote_so_far)) {
-    if (write_count <= 0) {
-      _is_closed = !BIO_should_retry(*_source);
-      if (_is_closed) {
-        return wrote_so_far;
-      }
+  if (length != 0) {
+    size_t wrote_so_far = 0;
 
-      // Block on the underlying socket before we try to write some
-      // more.
-      int fd = -1;
-      BIO_get_fd(*_source, &fd);
-      if (fd < 0) {
-        downloader_cat.warning()
-          << "socket BIO has no file descriptor.\n";
+    int write_count = BIO_write(*_source, start, length);
+    while (write_count != (int)(length - wrote_so_far)) {
+      if (write_count <= 0) {
+        _is_closed = !BIO_should_retry(*_source);
+        if (_is_closed) {
+          return wrote_so_far;
+        }
+        
+        // Block on the underlying socket before we try to write some
+        // more.
+        int fd = -1;
+        BIO_get_fd(*_source, &fd);
+        if (fd < 0) {
+          downloader_cat.warning()
+            << "socket BIO has no file descriptor.\n";
+        } else {
+          downloader_cat.spam()
+            << "waiting to write to BIO.\n";
+          fd_set wset;
+          FD_ZERO(&wset);
+          FD_SET(fd, &wset);
+          select(fd + 1, NULL, &wset, NULL, NULL);
+        }        
+        
       } else {
-        downloader_cat.spam()
-          << "waiting to write to BIO.\n";
-        fd_set wset;
-        FD_ZERO(&wset);
-        FD_SET(fd, &wset);
-        select(fd + 1, NULL, &wset, NULL, NULL);
-      }        
-
-    } else {
-      // wrote some characters.
-      wrote_so_far += write_count;
+        // wrote some characters.
+        wrote_so_far += write_count;
+      }
+      
+      // Try to write some more.
+      write_count = BIO_write(*_source, start + wrote_so_far, length - wrote_so_far);
     }
-
-    // Try to write some more.
-    write_count = BIO_write(*_source, start + wrote_so_far, length - wrote_so_far);
   }
 
   return length;

+ 0 - 12
panda/src/downloader/chunkedStream.I

@@ -47,15 +47,3 @@ open(BioStreamPtr *source, HTTPChannel *doc) {
   _buf.open_read(source, doc);
   return *this;
 }
-
-////////////////////////////////////////////////////////////////////
-//     Function: IChunkedStream::close
-//       Access: Public
-//  Description: Resets the ChunkedStream to empty, but does not actually
-//               close the source CHUNKED unless owns_source was true.
-////////////////////////////////////////////////////////////////////
-INLINE IChunkedStream &IChunkedStream::
-close() {
-  _buf.close_read();
-  return *this;
-}

+ 11 - 0
panda/src/downloader/chunkedStream.cxx

@@ -37,4 +37,15 @@ is_closed() {
   return false;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: IChunkedStream::close
+//       Access: Public, Virtual
+//  Description: Resets the ChunkedStream to empty, but does not actually
+//               close the source BIO unless owns_source was true.
+////////////////////////////////////////////////////////////////////
+void IChunkedStream::
+close() {
+  _buf.close_read();
+}
+
 #endif  // HAVE_SSL

+ 1 - 1
panda/src/downloader/chunkedStream.h

@@ -45,9 +45,9 @@ public:
   INLINE IChunkedStream(BioStreamPtr *source, HTTPChannel *doc);
 
   INLINE IChunkedStream &open(BioStreamPtr *source, HTTPChannel *doc);
-  INLINE IChunkedStream &close();
 
   virtual bool is_closed();
+  virtual void close();
 
 private:
   ChunkedStreamBuf _buf;

+ 0 - 12
panda/src/downloader/identityStream.I

@@ -51,15 +51,3 @@ open(BioStreamPtr *source, HTTPChannel *doc,
   _buf.open_read(source, doc, has_content_length, content_length);
   return *this;
 }
-
-////////////////////////////////////////////////////////////////////
-//     Function: IIdentityStream::close
-//       Access: Public
-//  Description: Resets the IdentityStream to empty, but does not actually
-//               close the source IDENTITY unless owns_source was true.
-////////////////////////////////////////////////////////////////////
-INLINE IIdentityStream &IIdentityStream::
-close() {
-  _buf.close_read();
-  return *this;
-}

+ 11 - 0
panda/src/downloader/identityStream.cxx

@@ -38,4 +38,15 @@ is_closed() {
   return false;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: IIdentityStream::close
+//       Access: Public, Virtual
+//  Description: Resets the IdentityStream to empty, but does not actually
+//               close the source BIO unless owns_source was true.
+////////////////////////////////////////////////////////////////////
+void IIdentityStream::
+close() {
+  _buf.close_read();
+}
+
 #endif  // HAVE_SSL

+ 1 - 1
panda/src/downloader/identityStream.h

@@ -52,9 +52,9 @@ public:
 
   INLINE IIdentityStream &open(BioStreamPtr *source, HTTPChannel *doc,
                                bool has_content_length, size_t content_length);
-  INLINE IIdentityStream &close();
 
   virtual bool is_closed();
+  virtual void close();
 
 private:
   IdentityStreamBuf _buf;

+ 3 - 0
panda/src/downloader/socketStream.h

@@ -48,6 +48,7 @@ PUBLISHED:
   bool receive_datagram(Datagram &dg);
 
   virtual bool is_closed() = 0;
+  virtual void close() = 0;
 
 private:
   size_t _data_expected;
@@ -70,6 +71,7 @@ PUBLISHED:
   bool send_datagram(const Datagram &dg);
 
   virtual bool is_closed() = 0;
+  virtual void close() = 0;
 
   INLINE void set_collect_tcp(bool collect_tcp);
   INLINE bool get_collect_tcp() const;
@@ -99,6 +101,7 @@ PUBLISHED:
   bool send_datagram(const Datagram &dg);
 
   virtual bool is_closed() = 0;
+  virtual void close() = 0;
 
   INLINE void set_collect_tcp(bool collect_tcp);
   INLINE bool get_collect_tcp() const;