From 293019fcbb14efd9edaf7285223489a161105c0e Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 29 May 2011 22:05:38 -0400 Subject: [PATCH] Redo how socket shutdown works: * Keep hold of the socketHandle_ from the accept callback * close() this socket (creating a CFSocket breaks CFStreams) * Implement -[NetworkConnectionDelegate connectionDidClose:] to reconnect if attached * When kCFStreamEventEndEncountered is received, just go through CloseConnection(), same with kCFStreamEventErrorOccurred --- Source/DebuggerBackEnd.m | 13 +++++++++++++ Source/NetworkCallbackController.mm | 29 +++++++++++++++++++---------- Source/NetworkConnection.mm | 6 ++++-- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/Source/DebuggerBackEnd.m b/Source/DebuggerBackEnd.m index d8500f2..62861d6 100644 --- a/Source/DebuggerBackEnd.m +++ b/Source/DebuggerBackEnd.m @@ -290,6 +290,19 @@ // TODO: update the status. } +/** + * Called when the connection is finally closed. This will reopen the listening + * socket if the debugger remains attached. + */ +- (void)connectionDidClose:(NetworkConnection*)connection +{ + if ([delegate respondsToSelector:@selector(debuggerDisconnected)]) + [delegate debuggerDisconnected]; + + if (self.attached) + [connection_ connect]; +} + - (void)handleResponse:(NSXMLDocument*)response { NSInteger transactionID = [connection_ transactionIDFromResponse:response]; diff --git a/Source/NetworkCallbackController.mm b/Source/NetworkCallbackController.mm index 1635f7b..5e5bd08 100644 --- a/Source/NetworkCallbackController.mm +++ b/Source/NetworkCallbackController.mm @@ -79,12 +79,24 @@ void NetworkCallbackController::OpenConnection(NSUInteger port) void NetworkCallbackController::CloseConnection() { + NSUInteger closeCount = 0; + + if (readStream_) { + UnscheduleReadStream(); + ++closeCount; + } + if (writeStream_) { + UnscheduleWriteStream(); + ++closeCount; + } + if (socketHandle_) { - close(socketHandle_); - //socketHandle_ = NULL; + for ( ; closeCount > 0; --closeCount) + close(socketHandle_); + + socketHandle_ = NULL; + [connection_ socketDisconnected]; } - UnscheduleReadStream(); - UnscheduleWriteStream(); } BOOL NetworkCallbackController::WriteStreamCanAcceptBytes() @@ -220,13 +232,11 @@ void NetworkCallbackController::OnReadStreamEvent(CFReadStreamRef stream, case kCFStreamEventErrorOccurred: ReportError(CFReadStreamCopyError(stream)); - UnscheduleReadStream(); + CloseConnection(); break; case kCFStreamEventEndEncountered: - UnscheduleReadStream(); CloseConnection(); - [connection_ socketDisconnected]; break; }; } @@ -242,12 +252,11 @@ void NetworkCallbackController::OnWriteStreamEvent(CFWriteStreamRef stream, case kCFStreamEventErrorOccurred: ReportError(CFWriteStreamCopyError(stream)); - UnscheduleWriteStream(); + CloseConnection(); break; case kCFStreamEventEndEncountered: - UnscheduleReadStream(); - [connection_ socketDisconnected]; + CloseConnection(); break; } } diff --git a/Source/NetworkConnection.mm b/Source/NetworkConnection.mm index c133614..2fae29f 100644 --- a/Source/NetworkConnection.mm +++ b/Source/NetworkConnection.mm @@ -100,6 +100,9 @@ void PerformQuitSignal(void* info) CFRelease(quitSource_); quitSource_ = NULL; + if ([delegate_ respondsToSelector:@selector(connectionDidClose:)]) + [delegate_ connectionDidClose:self]; + [pool release]; } @@ -157,8 +160,7 @@ void PerformQuitSignal(void* info) */ - (void)socketDisconnected { - if ([delegate_ respondsToSelector:@selector(connectionDidClose:)]) - [delegate_ connectionDidClose:self]; + [self close]; } /** -- 2.43.5