From 8199a38fe6a5e49214479b1c96a344e30a2b6200 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Thu, 2 Aug 2007 22:30:49 -0700 Subject: [PATCH] Threads can't communicate with each other and call methods on each other's objects, so we do have to use notifications. * Source/SocketWrapper.h: Defining some of the notification constants * Source/SocketWrapper.m: ([SocketWrapper _connect:]): Use the new notification system ([SocketWrapper _postNotification:withObject:]): Helper method for posting notifications ([SocketWrapper setDelegate:]): Register the delegate for these new notifications * Source/DebuggerConnection.m: ([DebuggerConnection socketDidAccept:]): Made notification compatible --- Source/DebuggerConnection.m | 9 ++++++--- Source/SocketWrapper.h | 4 ++++ Source/SocketWrapper.m | 19 +++++++++++++++---- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/Source/DebuggerConnection.m b/Source/DebuggerConnection.m index 89c1a60..074eb09 100644 --- a/Source/DebuggerConnection.m +++ b/Source/DebuggerConnection.m @@ -16,7 +16,6 @@ #import "DebuggerConnection.h" - @implementation DebuggerConnection /** @@ -103,9 +102,13 @@ * Called by SocketWrapper after the connection is successful. This immediately calls * -[SocketWrapper receive] to clear the way for communication */ -- (void)socketAccepted +- (void)socketDidAccept: (NSNotification *)notif { - [_socket receive]; + NSLog(@"accepted %@", notif); + if ([[notif userInfo] objectForKey: SocketWrapperNotificationConnection] == self) + { + NSLog(@"accepted :)"); + } } @end diff --git a/Source/SocketWrapper.h b/Source/SocketWrapper.h index b2dba33..519756c 100644 --- a/Source/SocketWrapper.h +++ b/Source/SocketWrapper.h @@ -16,6 +16,9 @@ #import +extern NSString *SocketWrapperNotificationConnection; // fancy name for _delegate +extern NSString *SocketDidAcceptNotification; + @interface SocketWrapper : NSObject { int _port; @@ -50,5 +53,6 @@ // ============== internal functions for threading - (void)_connect: (id)obj; +- (void)_postNotification: (NSString *)name withObject: (id)obj; @end diff --git a/Source/SocketWrapper.m b/Source/SocketWrapper.m index d159586..d243564 100644 --- a/Source/SocketWrapper.m +++ b/Source/SocketWrapper.m @@ -21,6 +21,9 @@ #include #include +NSString *SocketWrapperNotificationConnection = @"debuggerconnection"; +NSString *SocketDidAcceptNotification = @"socketdidaccept"; + @implementation SocketWrapper /** @@ -57,8 +60,15 @@ * Sets the delegate but does *not* retain it */ - (void)setDelegate: (id)delegate -{ +{ + if (_delegate != nil) + { + [[NSNotificationCenter defaultCenter] removeObserver: _delegate]; + } + _delegate = delegate; + + [[NSNotificationCenter defaultCenter] addObserver: _delegate selector: @selector(socketDidAccept:) name: SocketDidAcceptNotification object: nil]; } /** @@ -120,8 +130,7 @@ // we're done listening now that we have a connection close(socketOpen); - [_delegate performSelectorOnMainThread: @selector(socketDidAccept:) withObject: nil waitUntilDone: NO]; - //[_delegate socketDidAccept]; + [self _postNotification: SocketDidAcceptNotification withObject: nil]; [pool release]; } @@ -211,7 +220,9 @@ */ - (void)_postNotification: (NSString *)name withObject: (id)obj { - [[NSNotificationCenter defaultCenter] postNotificationName: name object: obj]; + NSDictionary *dict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: _delegate, nil] + forKeys: [NSArray arrayWithObjects: SocketWrapperNotificationConnection, nil]]; + [[NSNotificationCenter defaultCenter] postNotificationName: name object: obj userInfo: dict]; } @end -- 2.22.5