# HG changeset patch # User Jens Alfke # Date 1214254951 25200 # Node ID 16454d63d4c24a2b8e87c97deb28a0b65ab78f43 # Parent 3be241de163028d5f4718ffac9a8d584c4322dd7 Implemented BLIP 1.1 (explicit 'bye' message) diff -r 3be241de1630 -r 16454d63d4c2 BLIP/BLIPConnection.h --- a/BLIP/BLIPConnection.h Thu Jun 19 16:22:05 2008 -0700 +++ b/BLIP/BLIPConnection.h Mon Jun 23 14:02:31 2008 -0700 @@ -79,7 +79,7 @@ - (BOOL) connectionReceivedCloseRequest: (BLIPConnection*)connection; /** Called if the peer refuses a close request. - The typical error is BLIP error kBLIPError_Forbidden. */ + The typical error is kBLIPError_Forbidden. */ - (void) connection: (BLIPConnection*)connection closeRequestFailedWithError: (NSError*)error; @end diff -r 3be241de1630 -r 16454d63d4c2 BLIP/BLIPConnection.m --- a/BLIP/BLIPConnection.m Thu Jun 19 16:22:05 2008 -0700 +++ b/BLIP/BLIPConnection.m Mon Jun 23 14:02:31 2008 -0700 @@ -8,6 +8,7 @@ #import "BLIPConnection.h" #import "BLIP_Internal.h" +#import "TCP_Internal.h" #import "BLIPReader.h" #import "BLIPWriter.h" #import "BLIPDispatcher.h" @@ -153,8 +154,8 @@ NSError *error = response.error; LogTo(BLIPVerbose,@"Received close response: error=%@",error); if( error ) { - if( [_delegate respondsToSelector: @selector(connection:closeRequestFailedWithError:)] ) - [_delegate connection: self closeRequestFailedWithError: error]; + [self _unclose]; + [self tellDelegate: @selector(connection:closeRequestFailedWithError:) withObject: error]; } else { // Now finally close the socket: [super _beginClose]; diff -r 3be241de1630 -r 16454d63d4c2 BLIP/BLIPTest.m --- a/BLIP/BLIPTest.m Thu Jun 19 16:22:05 2008 -0700 +++ b/BLIP/BLIPTest.m Mon Jun 23 14:02:31 2008 -0700 @@ -194,6 +194,13 @@ Log(@"Now %u replies pending", _pending.count); } +- (BOOL) connectionReceivedCloseRequest: (BLIPConnection*)connection +{ + BOOL response = NO; + Log(@"***** %@ received a close request; returning %i",connection,response); + return response; +} + @end @@ -324,6 +331,17 @@ } } +- (BOOL) connectionReceivedCloseRequest: (BLIPConnection*)connection; +{ + Log(@"***** %@ received a close request",connection); + return YES; +} + +- (void) connection: (BLIPConnection*)connection closeRequestFailedWithError: (NSError*)error +{ + Log(@"***** %@'s close request failed: %@",connection,error); +} + @end diff -r 3be241de1630 -r 16454d63d4c2 BLIP/BLIP_Internal.h --- a/BLIP/BLIP_Internal.h Thu Jun 19 16:22:05 2008 -0700 +++ b/BLIP/BLIP_Internal.h Mon Jun 23 14:02:31 2008 -0700 @@ -42,7 +42,7 @@ UInt16 size; // total size of frame, _including_ this header } BLIPFrameHeader; -#define kBLIPFrameHeaderMagicNumber 0x9B34F205 +#define kBLIPFrameHeaderMagicNumber 0x9B34F206 #define kBLIPProfile_Hi @"Hi" // Used for Profile header in meta greeting message #define kBLIPProfile_Bye @"Bye" // Used for Profile header in meta close-request message diff -r 3be241de1630 -r 16454d63d4c2 TCP/TCPConnection.h --- a/TCP/TCPConnection.h Thu Jun 19 16:22:05 2008 -0700 +++ b/TCP/TCPConnection.h Mon Jun 23 14:02:31 2008 -0700 @@ -109,6 +109,7 @@ - (Class) readerClass; - (Class) writerClass; - (void) _beginClose; +- (void) _unclose; @end diff -r 3be241de1630 -r 16454d63d4c2 TCP/TCPConnection.m --- a/TCP/TCPConnection.m Thu Jun 19 16:22:05 2008 -0700 +++ b/TCP/TCPConnection.m Mon Jun 23 14:02:31 2008 -0700 @@ -247,7 +247,24 @@ [self disconnect]; } +- (void) _stopCloseTimer +{ + [NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector(_closeTimeoutExpired) object: nil]; +} +- (void) _unclose +{ + if( _status == kTCP_Closing ) { + LogTo(TCP,@"%@: _unclose!",self); + [_reader _unclose]; + [_writer _unclose]; + [self _stopCloseTimer]; + self.status = kTCP_Open; + } +} + + +/** Subclasses can override this to customize what happens when -close is called. */ - (void) _beginClose { [_reader close]; @@ -278,9 +295,7 @@ else [self tellDelegate: @selector(connectionDidClose:) withObject: nil]; } - [NSObject cancelPreviousPerformRequestsWithTarget: self - selector: @selector(_closeTimeoutExpired) - object: nil]; + [self _stopCloseTimer]; [self _stopOpenTimer]; [sAllConnections removeObjectIdenticalTo: self]; } diff -r 3be241de1630 -r 16454d63d4c2 TCP/TCPStream.m --- a/TCP/TCPStream.m Thu Jun 19 16:22:05 2008 -0700 +++ b/TCP/TCPStream.m Mon Jun 23 14:02:31 2008 -0700 @@ -121,17 +121,24 @@ - (BOOL) close { - _shouldClose = YES; + if( ! _shouldClose ) { + _shouldClose = YES; + LogTo(TCP,@"Request to close %@",self); + } if( self.isBusy ) { return NO; } else { - LogTo(TCP,@"Request to close %@",self); [[self retain] autorelease]; // don't let myself be dealloced in the midst of this [_conn _streamCanClose: self]; return YES; } } +- (void) _unclose +{ + _shouldClose = NO; +} + - (BOOL) isOpen { diff -r 3be241de1630 -r 16454d63d4c2 TCP/TCP_Internal.h --- a/TCP/TCP_Internal.h Thu Jun 19 16:22:05 2008 -0700 +++ b/TCP/TCP_Internal.h Mon Jun 23 14:02:31 2008 -0700 @@ -24,3 +24,9 @@ - (void) _streamGotEOF: (TCPStream*)stream; - (void) _streamDisconnected: (TCPStream*)stream; @end + + +@interface TCPStream () +- (void) _unclose; +@end +