From 36d0b8f7b16743fdefebe5f3d8683aa5e37b0f06 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Wed, 22 Jul 2009 00:06:50 -0400 Subject: [PATCH] * Completely de-couple SocketWrapper from GDBpConnection by passing the port directly to the ctor, rather than extracting it from the connection. * Make SocketWrapperDelegate a formal protocl and GDBpConnection now conforms to it. * Clean up delegate interface. --- Source/GDBpConnection.h | 2 +- Source/GDBpConnection.m | 9 +++++---- Source/SocketWrapper.h | 21 +++++++++------------ Source/SocketWrapper.m | 31 +++++++------------------------ 4 files changed, 22 insertions(+), 41 deletions(-) diff --git a/Source/GDBpConnection.h b/Source/GDBpConnection.h index e2aa13f..0a80c01 100644 --- a/Source/GDBpConnection.h +++ b/Source/GDBpConnection.h @@ -21,7 +21,7 @@ extern NSString* kErrorOccurredNotif; -@interface GDBpConnection : NSObject +@interface GDBpConnection : NSObject { int port; NSString* session; diff --git a/Source/GDBpConnection.m b/Source/GDBpConnection.m index f8463f2..0386d88 100644 --- a/Source/GDBpConnection.m +++ b/Source/GDBpConnection.m @@ -47,8 +47,8 @@ NSString* kErrorOccurredNotif = @"GDBpConnection_ErrorOccured_Notification"; connected = NO; // now that we have our host information, open the socket - socket = [[SocketWrapper alloc] initWithConnection:self]; - [socket setDelegate:self]; + socket = [[SocketWrapper alloc] initWithPort:port]; + socket.delegate = self; [socket connect]; self.status = @"Connecting"; @@ -109,7 +109,7 @@ NSString* kErrorOccurredNotif = @"GDBpConnection_ErrorOccured_Notification"; * -[SocketWrapper receive] to clear the way for communication, though the information * could be useful server information that we don't use right now. */ -- (void)socketDidAccept:(id)obj +- (void)socketDidAccept { connected = YES; [socket receive]; @@ -121,7 +121,8 @@ NSString* kErrorOccurredNotif = @"GDBpConnection_ErrorOccured_Notification"; [self addBreakpoint:bp]; } - [[[NSApp delegate] debugger] startDebugger]; // this will just load the debugger to make it look active + // Load the debugger to make it look active. + [[[NSApp delegate] debugger] performSelectorOnMainThread:@selector(startDebugger) withObject:nil waitUntilDone:YES]; } /** diff --git a/Source/SocketWrapper.h b/Source/SocketWrapper.h index d9692de..81d82dc 100644 --- a/Source/SocketWrapper.h +++ b/Source/SocketWrapper.h @@ -16,23 +16,19 @@ #import -@class GDBpConnection; +@protocol SocketWrapperDelegate; @interface SocketWrapper : NSObject { - GDBpConnection* connection; - int port; int sock; NSString* hostname; - id delegate; + id delegate; } +@property (assign) id delegate; -- (id)initWithConnection:(GDBpConnection*)cnx; - -- (id)delegate; -- (void)setDelegate:(id)aDelegate; +- (id)initWithPort:(int)aPort; - (void)connect; - (void)close; @@ -43,12 +39,13 @@ @end -@interface NSObject (SocketWrapperDelegate) +@protocol SocketWrapperDelegate -// error +// Returns a human-readable error message when an error occurred. - (void)errorEncountered:(NSString*)error; -// connection components -- (void)socketDidAccept:(id)obj; +// Called after the socket is ready for reading and writing. This is not called +// from the main thread. +- (void)socketDidAccept; @end diff --git a/Source/SocketWrapper.m b/Source/SocketWrapper.m index 38cffc3..30d78bd 100644 --- a/Source/SocketWrapper.m +++ b/Source/SocketWrapper.m @@ -29,16 +29,16 @@ @implementation SocketWrapper @synthesize hostname; +@synthesize delegate; /** - * Initializes the socket wrapper with a host and port + * Initializes the socket wrapper with a port */ -- (id)initWithConnection:(GDBpConnection*)cnx +- (id)initWithPort:(int)aPort; { if (self = [super init]) { - connection = [cnx retain]; - port = [connection port]; + port = aPort; } return self; } @@ -48,7 +48,6 @@ */ - (void)dealloc { - [connection release]; [hostname release]; [super dealloc]; } @@ -61,22 +60,6 @@ close(sock); } -/** - * Returns the delegate - */ -- (id)delegate -{ - return delegate; -} - -/** - * Sets the delegate but does* not* retain it - */ -- (void)setDelegate:(id)aDelegate -{ - delegate = aDelegate; -} - /** * Connects to a socket on the port specified during init. This will dispatch another thread to do the * actual waiting. Delegate notifications are posted along the way to let the client know what is going on. @@ -153,7 +136,7 @@ char* name = inet_ntoa(addr.sin_addr); [self setHostname:[NSString stringWithUTF8String:name]]; - [connection performSelectorOnMainThread:@selector(socketDidAccept:) withObject:nil waitUntilDone:NO]; + [delegate socketDidAccept]; [pool release]; } @@ -224,11 +207,11 @@ } /** - * Helper method that just calls -[DebuggerWindowController setError:] on the main thread + * Helper method that just calls |-errorEncountered:| */ - (void)error:(NSString*)msg { - [delegate performSelectorOnMainThread:@selector(setError:) withObject:msg waitUntilDone:NO]; + [delegate errorEncountered:msg]; } @end -- 2.22.5