From 8a5a8f3c0782c90f994fd2d5a6feea83df3f023b Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 5 Jan 2008 15:09:16 -0800 Subject: [PATCH] In SocketWraper, don't use the notification posting system to send errors to the delegate, instead just use performSelectorOnMainThread:withObject:waitUntilDone: on the delegate object. Also, -[NSObject(SocketWrapperDelegate) errorEncountered:] now just takes a string. --- Source/DebuggerConnection.m | 4 ++-- Source/SocketWrapper.h | 2 +- Source/SocketWrapper.m | 27 ++++++++++++++++----------- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/Source/DebuggerConnection.m b/Source/DebuggerConnection.m index f267a8a..44ba1e1 100644 --- a/Source/DebuggerConnection.m +++ b/Source/DebuggerConnection.m @@ -127,9 +127,9 @@ /** * Receives errors from the SocketWrapper and updates the display */ -- (void)errorEncountered:(NSError *)error +- (void)errorEncountered:(NSString *)error { - [windowController setError:[error domain]]; + [windowController setError:error]; } /** diff --git a/Source/SocketWrapper.h b/Source/SocketWrapper.h index 750e00b..4a21863 100644 --- a/Source/SocketWrapper.h +++ b/Source/SocketWrapper.h @@ -45,7 +45,7 @@ @interface NSObject (SocketWrapperDelegate) // error -- (void)errorEncountered:(NSError *)error; +- (void)errorEncountered:(NSString *)error; // connection components - (void)socketDidBind; diff --git a/Source/SocketWrapper.m b/Source/SocketWrapper.m index 0ae14e8..070045e 100644 --- a/Source/SocketWrapper.m +++ b/Source/SocketWrapper.m @@ -23,13 +23,14 @@ NSString *sockNotificationDebuggerConnection = @"DebuggerConnection"; NSString *sockNotificationReceiver = @"SEL-del-SocketWrapper_dataReceived"; -NSString *NsockError = @"SocketWrapper_Error"; NSString *NsockDidAccept = @"SocketWrapper_DidAccept"; NSString *NsockDataReceived = @"SocketWrapper_DataReceived"; NSString *NsockDataSent = @"SocketWrapper_DataSent"; @interface SocketWrapper (Private) +- (void)error:(NSString *)msg; + - (void)connect:(id)obj; - (void)postNotification:(NSString *)name withObject:(id)obj; - (void)postNotification:(NSString *)name withObject:(id)obj withDict:(NSMutableDictionary *)dict; @@ -91,7 +92,7 @@ NSString *NsockDataSent = @"SocketWrapper_DataSent"; if (getpeername(sock, (struct sockaddr *)&addr, &addrLength) < 0) { - [self postNotification:NsockError withObject:[NSError errorWithDomain:@"Could not get remote hostname." code:-1 userInfo:nil]]; + [self error:@"Could not get remote hostname."]; } char *name = inet_ntoa(addr.sin_addr); @@ -127,10 +128,6 @@ NSString *NsockDataSent = @"SocketWrapper_DataSent"; { [delegate dataSent:[notif object]]; } - else if (name == NsockError) - { - [delegate errorEncountered:[NSError errorWithDomain:[notif object] code:-1 userInfo:nil]]; - } } /** @@ -168,7 +165,7 @@ NSString *NsockDataSent = @"SocketWrapper_DataSent"; if (tries >= 5) { close(socketOpen); - [self postNotification:NsockError withObject:@"Could not bind to socket"]; + [self error:@"Could not bind to socket"]; return; } NSLog(@"couldn't bind to the socket... trying again in 5"); @@ -179,7 +176,7 @@ NSString *NsockDataSent = @"SocketWrapper_DataSent"; // now we just have to keep our ears open if (listen(socketOpen, 0) == -1) { - [self postNotification:NsockError withObject:@"Could not use bound socket for listening"]; + [self error:@"Could not use bound socket for listening"]; } // accept a connection @@ -189,7 +186,7 @@ NSString *NsockDataSent = @"SocketWrapper_DataSent"; if (sock < 0) { close(socketOpen); - [self postNotification:NsockError withObject:@"Client failed to accept remote socket"]; + [self error:@"Client failed to accept remote socket"]; return; } @@ -250,7 +247,7 @@ NSString *NsockDataSent = @"SocketWrapper_DataSent"; int latest = recv(sock, &buffer, sizeof(buffer), 0); if (latest < 1) { - [self postNotification:NsockError withObject:@"Socket closed or could not be read"]; + [self error:@"Socket closed or could not be read"]; return; } [data appendBytes:buffer length:latest]; @@ -281,7 +278,7 @@ NSString *NsockDataSent = @"SocketWrapper_DataSent"; int sent = send(sock, [data UTF8String], [data length], 0); if (sent < 0) { - [self postNotification:NsockError withObject:@"Failed to write data to socket"]; + [self error:@"Failed to write data to socket"]; return; } if (sent < [data length]) @@ -311,4 +308,12 @@ NSString *NsockDataSent = @"SocketWrapper_DataSent"; [[NSNotificationCenter defaultCenter] postNotificationName:name object:obj userInfo:dict]; } +/** + * Helper method that just calls -[DebuggerWindowController setError:] on the main thread + */ +- (void)error:(NSString *)msg +{ + [delegate performSelectorOnMainThread:@selector(errorEncountered:) withObject:msg waitUntilDone:NO]; +} + @end -- 2.43.5