Keep track of the child socket handle and close() it properly
authorRobert Sesek <rsesek@bluestatic.org>
Mon, 30 May 2011 01:36:45 +0000 (21:36 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Mon, 30 May 2011 01:36:45 +0000 (21:36 -0400)
Source/NetworkCallbackController.h
Source/NetworkCallbackController.mm

index 64a141ce809b580804d09971d6082667ce5dce2c..7ac5810e55fe5703d5afa9e427083de6e28ea4f7 100644 (file)
@@ -77,9 +77,13 @@ class NetworkCallbackController
   // Messages the NetworkConnection's delegate and takes ownership of |error|.
   void ReportError(CFErrorRef error);
 
-  // The actual socket.
+  // The socket that listens for incoming connections from the engine.
   CFSocketRef listeningSocket_;  // Strong.
 
+  // The child socket from the |listeningSocket_| that is connected to the
+  // engine. The streams are attached to this socket.
+  CFSocketNativeHandle socketHandle_;
+
   // The read and write streams that are scheduled on the |runLoop_|. Both are
   // weak and are owned by the run loop source.
   CFReadStreamRef readStream_;
index 537daccd447ae7bf340ece1cba3b56e592ef02bc..1635f7b9906ddac140ceb4fc2c0cc3e86dbf35c1 100644 (file)
@@ -23,7 +23,9 @@
 #import "NetworkConnectionPrivate.h"
 
 NetworkCallbackController::NetworkCallbackController(NetworkConnection* connection)
-    : readStream_(NULL),
+    : listeningSocket_(NULL),
+      socketHandle_(NULL),
+      readStream_(NULL),
       writeStream_(NULL),
       connection_(connection),
       runLoop_(CFRunLoopGetCurrent())
@@ -77,7 +79,10 @@ void NetworkCallbackController::OpenConnection(NSUInteger port)
 
 void NetworkCallbackController::CloseConnection()
 {
-  CloseSocket();
+  if (socketHandle_) {
+    close(socketHandle_);
+    //socketHandle_ = NULL;
+  }
   UnscheduleReadStream();
   UnscheduleWriteStream();
 }
@@ -152,9 +157,11 @@ void NetworkCallbackController::OnSocketAccept(CFSocketRef socket,
                                                CFDataRef address,
                                                const void* data)
 {
+  socketHandle_ = *(CFSocketNativeHandle*)data;
+
   // Create the streams on the socket.
   CFStreamCreatePairWithSocket(kCFAllocatorDefault,
-                               *(CFSocketNativeHandle*)data,  // Socket handle.
+                               socketHandle_,  // Socket handle.
                                &readStream_,  // Read stream in-pointer.
                                &writeStream_);  // Write stream in-pointer.
   
@@ -218,6 +225,7 @@ void NetworkCallbackController::OnReadStreamEvent(CFReadStreamRef stream,
       
     case kCFStreamEventEndEncountered:
       UnscheduleReadStream();
+      CloseConnection();
       [connection_ socketDisconnected];
       break;
   };