From 92dcb89133d68e8fbd91604420eaa13c336a9800 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 29 May 2011 20:53:47 -0400 Subject: [PATCH] Rename socket_ to listeningSocket_ and add CloseSocket() to help shut it down --- Source/NetworkCallbackController.h | 6 ++++- Source/NetworkCallbackController.mm | 40 +++++++++++++++++------------ 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/Source/NetworkCallbackController.h b/Source/NetworkCallbackController.h index 6f149c6..64a141c 100644 --- a/Source/NetworkCallbackController.h +++ b/Source/NetworkCallbackController.h @@ -65,6 +65,10 @@ class NetworkCallbackController void OnReadStreamEvent(CFReadStreamRef stream, CFStreamEventType eventType); void OnWriteStreamEvent(CFWriteStreamRef stream, CFStreamEventType eventType); + // Closes down the listening socket but keeps the streams alive. This can be + // called multiple times, even if the socket is NULL. + void CloseSocket(); + // Removes the read or write stream from the run loop, closes the stream, // releases the reference. void UnscheduleReadStream(); @@ -74,7 +78,7 @@ class NetworkCallbackController void ReportError(CFErrorRef error); // The actual socket. - CFSocketRef socket_; // Strong. + CFSocketRef listeningSocket_; // Strong. // The read and write streams that are scheduled on the |runLoop_|. Both are // weak and are owned by the run loop source. diff --git a/Source/NetworkCallbackController.mm b/Source/NetworkCallbackController.mm index 16ab54f..537dacc 100644 --- a/Source/NetworkCallbackController.mm +++ b/Source/NetworkCallbackController.mm @@ -52,35 +52,32 @@ void NetworkCallbackController::OpenConnection(NSUInteger port) signature.address = (CFDataRef)[NSData dataWithBytes:&address length:sizeof(address)]; do { - socket_ = CFSocketCreateWithSocketSignature(kCFAllocatorDefault, - &signature, // Socket signature. - kCFSocketAcceptCallBack, // Callback types. - &NetworkCallbackController::SocketAcceptCallback, // Callout function pointer. - &context); // Context to pass to callout. - if (!socket_) { + listeningSocket_ = + CFSocketCreateWithSocketSignature(kCFAllocatorDefault, + &signature, // Socket signature. + kCFSocketAcceptCallBack, // Callback types. + &NetworkCallbackController::SocketAcceptCallback, // Callout function pointer. + &context); // Context to pass to callout. + if (!listeningSocket_) { [connection_ errorEncountered:@"Could not open socket."]; sleep(1); } - } while (!socket_); + } while (!listeningSocket_); // Allow old, yet-to-be recycled sockets to be reused. BOOL yes = YES; - setsockopt(CFSocketGetNative(socket_), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(BOOL)); - setsockopt(CFSocketGetNative(socket_), SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(BOOL)); + setsockopt(CFSocketGetNative(listeningSocket_), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(BOOL)); + setsockopt(CFSocketGetNative(listeningSocket_), SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(BOOL)); // Schedule the socket on the run loop. - CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(kCFAllocatorDefault, socket_, 0); + CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(kCFAllocatorDefault, listeningSocket_, 0); CFRunLoopAddSource(runLoop_, source, kCFRunLoopCommonModes); CFRelease(source); } void NetworkCallbackController::CloseConnection() { - if (socket_) { - CFSocketInvalidate(socket_); - CFRelease(socket_); - socket_ = NULL; - } + CloseSocket(); UnscheduleReadStream(); UnscheduleWriteStream(); } @@ -198,8 +195,10 @@ void NetworkCallbackController::OnSocketAccept(CFSocketRef socket, ReportError(CFWriteStreamCopyError(writeStream_)); return; } - + [connection_ socketDidAccept]; + + CloseSocket(); } void NetworkCallbackController::OnReadStreamEvent(CFReadStreamRef stream, @@ -245,6 +244,15 @@ void NetworkCallbackController::OnWriteStreamEvent(CFWriteStreamRef stream, } } +void NetworkCallbackController::CloseSocket() +{ + if (listeningSocket_) { + CFSocketInvalidate(listeningSocket_); + CFRelease(listeningSocket_); + listeningSocket_ = NULL; + } +} + void NetworkCallbackController::UnscheduleReadStream() { if (!readStream_) -- 2.43.5