From edf892a6a263e5eb10994be1fddca8f2c8dc7b22 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Mon, 31 May 2010 22:28:14 -0400 Subject: [PATCH] Make DebuggerConnection work on a separate thread. --- Source/DebuggerConnection.h | 3 +++ Source/DebuggerConnection.m | 26 ++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/Source/DebuggerConnection.h b/Source/DebuggerConnection.h index 4c1f0dd..c34ea8b 100644 --- a/Source/DebuggerConnection.h +++ b/Source/DebuggerConnection.h @@ -27,6 +27,9 @@ // If the connection to the debugger engine is currently active. BOOL connected_; + // Reference to the message loop that the socket runs on. Weak. + NSRunLoop* runLoop_; + // The raw CFSocket on which the two streams are based. Strong. CFSocketRef socket_; diff --git a/Source/DebuggerConnection.m b/Source/DebuggerConnection.m index c4e1005..73e5b27 100644 --- a/Source/DebuggerConnection.m +++ b/Source/DebuggerConnection.m @@ -34,6 +34,8 @@ @property NSUInteger lastWrittenTransaction; @property (retain) NSMutableArray* queuedWrites; +- (void)connectInternal; + @end // CFNetwork Callbacks ///////////////////////////////////////////////////////// @@ -220,10 +222,20 @@ void SocketAcceptCallback(CFSocketRef socket, } /** - * Creates, connects to, and schedules a CFSocket. + * Kicks off the socket on another thread. */ - (void)connect { + [NSThread detachNewThreadSelector:@selector(connectInternal) toTarget:self withObject:nil]; +} + +/** + * Creates, connects to, and schedules a CFSocket. + */ +- (void)connectInternal +{ + runLoop_ = [NSRunLoop currentRunLoop]; + // Pass ourselves to the callback so we don't have to use ugly globals. CFSocketContext context; context.version = 0; @@ -264,8 +276,10 @@ void SocketAcceptCallback(CFSocketRef socket, // Schedule the socket on the run loop. CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(kCFAllocatorDefault, socket_, 0); - CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopCommonModes); + CFRunLoopAddSource([runLoop_ getCFRunLoop], source, kCFRunLoopCommonModes); CFRelease(source); + + [runLoop_ run]; } /** @@ -273,9 +287,13 @@ void SocketAcceptCallback(CFSocketRef socket, */ - (void)close { + CFRunLoopStop([runLoop_ getCFRunLoop]); + // The socket goes down, so do the streams, which clean themselves up. - CFSocketInvalidate(socket_); - CFRelease(socket_); + if (socket_) { + CFSocketInvalidate(socket_); + CFRelease(socket_); + } self.queuedWrites = nil; [writeQueueLock_ release]; } -- 2.22.5