From a2e4f5b89cfc6ce5aa03fd4bac6eb42c7c6f1c9a Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Wed, 19 Jun 2013 22:24:51 -0400 Subject: [PATCH] Improve MessageQueueDelegate. --- Source/MessageQueue.h | 17 +++++++---------- Source/MessageQueue.m | 11 ++++------- Source/ProtocolClient.m | 38 +++++++++----------------------------- 3 files changed, 20 insertions(+), 46 deletions(-) diff --git a/Source/MessageQueue.h b/Source/MessageQueue.h index 2e4f968..2e62288 100644 --- a/Source/MessageQueue.h +++ b/Source/MessageQueue.h @@ -80,24 +80,21 @@ // Delegate //////////////////////////////////////////////////////////////////// -// The delegate for the message queue. These methods may be called on any thread. +// The delegate for the message queue. These methods are called from the thread +// on which the MessageQueue was initialized. @protocol MessageQueueDelegate // Callback for any errors that the MessageQueue encounters. -- (void)messageQueueError:(NSError*)error; +- (void)messageQueue:(MessageQueue*)queue error:(NSError*)error; // Called when the listening socket has accepted a child socket. -- (void)clientDidConnect:(MessageQueue*)queue; +- (void)messageQueueDidConnect:(MessageQueue*)queue; // Called when the child socket has been disconnected. -- (void)clientDidDisconnect:(MessageQueue*)queue; - -// If the write stream is ready, the delegate controls whether or not the next -// pending message should be sent via the result of this method. -- (BOOL)shouldSendMessage; +- (void)messageQueueDidDisconnect:(MessageQueue*)queue; // Callback for when a message has been sent. -- (void)didSendMessage:(NSString*)message; +- (void)messageQueue:(MessageQueue*)queue didSendMessage:(NSString*)message; // Callback with the message content when one has been receieved. -- (void)didReceiveMessage:(NSString*)message; +- (void)messageQueue:(MessageQueue*)queue didReceiveMessage:(NSString*)message; @end diff --git a/Source/MessageQueue.m b/Source/MessageQueue.m index c7c4ff0..8ada000 100644 --- a/Source/MessageQueue.m +++ b/Source/MessageQueue.m @@ -213,7 +213,7 @@ static void MessageQueueWriteEvent(CFWriteStreamRef stream, } _connected = NO; - [_delegate clientDidDisconnect:self]; + [_delegate messageQueueDidDisconnect:self]; } - (void)stopRunLoop { @@ -230,9 +230,6 @@ static void MessageQueueWriteEvent(CFWriteStreamRef stream, if (![_queue count]) return; - if (![(id)_delegate.object shouldSendMessage]) - return; - if (!CFWriteStreamCanAcceptBytes(_writeStream)) return; @@ -278,7 +275,7 @@ static void MessageQueueWriteEvent(CFWriteStreamRef stream, totalWritten += bytesWritten; } - [_delegate didSendMessage:message]; + [_delegate messageQueue:self didSendMessage:message]; free(buffer); } @@ -322,7 +319,7 @@ static void MessageQueueWriteEvent(CFWriteStreamRef stream, // If this read finished the packet, handle it and reset. if (_messageSize >= _totalMessageSize) { - [_delegate didReceiveMessage:[_message autorelease]]; + [_delegate messageQueue:self didReceiveMessage:[_message autorelease]]; _message = nil; // Process any outgoing messages. @@ -384,7 +381,7 @@ static void MessageQueueWriteEvent(CFWriteStreamRef stream, } _connected = YES; - [_delegate clientDidConnect:self]; + [_delegate messageQueueDidConnect:self]; CFSocketInvalidate(_socket); CFRelease(_socket); diff --git a/Source/ProtocolClient.m b/Source/ProtocolClient.m index 931a9d1..4ae1892 100644 --- a/Source/ProtocolClient.m +++ b/Source/ProtocolClient.m @@ -16,10 +16,6 @@ #import "ProtocolClient.h" -@interface ProtocolClient (Private) -- (void)postReceivedMessage:(NSXMLDocument*)message; -@end - @implementation ProtocolClient - (id)initWithDelegate:(NSObject*)delegate { @@ -94,30 +90,24 @@ // MessageQueueDelegate //////////////////////////////////////////////////////// -- (void)messageQueueError:(NSError*)error { +- (void)messageQueue:(MessageQueue*)queue error:(NSError*)error { NSLog(@"error = %@", error); } -- (void)clientDidConnect:(MessageQueue*)queue { +- (void)messageQueueDidConnect:(MessageQueue*)queue { [_lock lock]; _nextID = 0; _lastReadID = 0; _lastWrittenID = 0; [_lock unlock]; - [_delegate performSelector:@selector(debuggerEngineConnected:) - onThread:_delegateThread - withObject:self - waitUntilDone:NO]; + [_delegate debuggerEngineConnected:self]; } -- (void)clientDidDisconnect:(MessageQueue*)queue { - [_delegate performSelector:@selector(debuggerEngineDisconnected:) - onThread:_delegateThread - withObject:self - waitUntilDone:NO]; +- (void)messageQueueDidDisconnect:(MessageQueue*)queue { [_messageQueue release]; _messageQueue = nil; + [_delegate debuggerEngineDisconnected:self]; } // If the write stream is ready, the delegate controls whether or not the next @@ -130,7 +120,7 @@ } // Callback for when a message has been sent. -- (void)didSendMessage:(NSString*)message { +- (void)messageQueue:(MessageQueue*)queue didSendMessage:(NSString*)message { NSInteger tag = [self transactionIDFromCommand:message]; [_lock lock]; _lastWrittenID = tag; @@ -138,14 +128,14 @@ } // Callback with the message content when one has been receieved. -- (void)didReceiveMessage:(NSString*)message { +- (void)messageQueue:(MessageQueue*)queue didReceiveMessage:(NSString*)message { // Test if we can convert it into an NSXMLDocument. NSError* error = nil; NSXMLDocument* xml = [[NSXMLDocument alloc] initWithXMLString:message options:NSXMLDocumentTidyXML error:&error]; if (error) { - [self messageQueueError:error]; + [self messageQueue:queue error:error]; return; } @@ -163,17 +153,7 @@ _lastReadID = transaction; - [self performSelector:@selector(postReceivedMessage:) - onThread:_delegateThread - withObject:xml - waitUntilDone:NO - modes:@[ NSDefaultRunLoopMode ]]; -} - -// Private ///////////////////////////////////////////////////////////////////// - -- (void)postReceivedMessage:(NSXMLDocument*)message { - [_delegate debuggerEngine:self receivedMessage:message]; + [_delegate debuggerEngine:self receivedMessage:xml]; } @end -- 2.43.5