From ccb994c4c68cd42b1739fc0304991d2137638de4 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 29 May 2011 21:36:45 -0400 Subject: [PATCH] Keep track of the child socket handle and close() it properly --- Source/NetworkCallbackController.h | 6 +++++- Source/NetworkCallbackController.mm | 14 +++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Source/NetworkCallbackController.h b/Source/NetworkCallbackController.h index 64a141c..7ac5810 100644 --- a/Source/NetworkCallbackController.h +++ b/Source/NetworkCallbackController.h @@ -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_; diff --git a/Source/NetworkCallbackController.mm b/Source/NetworkCallbackController.mm index 537dacc..1635f7b 100644 --- a/Source/NetworkCallbackController.mm +++ b/Source/NetworkCallbackController.mm @@ -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; }; -- 2.22.5