TCP/TCPListener.m
changeset 25 a4875607a3a0
parent 8 6f539dd9921c
child 26 cb9cdf247239
     1.1 --- a/TCP/TCPListener.m	Thu May 29 16:40:36 2008 -0700
     1.2 +++ b/TCP/TCPListener.m	Tue Dec 02 22:42:56 2008 -0800
     1.3 @@ -23,6 +23,8 @@
     1.4                                        CFDataRef address, const void *data, void *info);
     1.5  
     1.6  @interface TCPListener()
     1.7 +- (void) _openBonjour;
     1.8 +- (void) _closeBonjour;
     1.9  @property BOOL bonjourPublished;
    1.10  @property NSInteger bonjourError;
    1.11  - (void) _updateTXTRecord;
    1.12 @@ -51,8 +53,9 @@
    1.13  
    1.14  
    1.15  @synthesize delegate=_delegate, port=_port, useIPv6=_useIPv6,
    1.16 -            bonjourServiceType=_bonjourServiceType, bonjourServiceName=_bonjourServiceName,
    1.17 +            bonjourServiceType=_bonjourServiceType,
    1.18              bonjourPublished=_bonjourPublished, bonjourError=_bonjourError,
    1.19 +            bonjourService=_netService,
    1.20              pickAvailablePort=_pickAvailablePort;
    1.21  
    1.22  
    1.23 @@ -62,7 +65,7 @@
    1.24  }
    1.25  
    1.26  
    1.27 -// Stores the last error from CFSocketCreate or CFSocketSetAddress into *ouError.
    1.28 +// Stores the last error from CFSocketCreate or CFSocketSetAddress into *outError.
    1.29  static void* getLastCFSocketError( NSError **outError ) {
    1.30      if( outError )
    1.31          *outError = [NSError errorWithDomain: NSPOSIXErrorDomain code: errno userInfo: nil];
    1.32 @@ -160,23 +163,7 @@
    1.33          }
    1.34      }
    1.35      
    1.36 -    // Open Bonjour:
    1.37 -    if( _bonjourServiceType && !_netService) {
    1.38 -        // instantiate the NSNetService object that will advertise on our behalf.
    1.39 -        _netService = [[NSNetService alloc] initWithDomain: @"local." 
    1.40 -                                                      type: _bonjourServiceType
    1.41 -                                                      name: _bonjourServiceName ?:@""
    1.42 -                                                      port: _port];
    1.43 -        if( _netService ) {
    1.44 -            [_netService setDelegate:self];
    1.45 -            if( _bonjourTXTRecord )
    1.46 -                [self _updateTXTRecord];
    1.47 -            [_netService publish];
    1.48 -        } else {
    1.49 -            self.bonjourError = -1;
    1.50 -            Warn(@"%@: Failed to create NSNetService",self);
    1.51 -        }
    1.52 -    }
    1.53 +    [self _openBonjour];
    1.54  
    1.55      LogTo(TCP,@"%@ is open",self);
    1.56      [self tellDelegate: @selector(listenerDidOpen:) withObject: nil];
    1.57 @@ -192,14 +179,7 @@
    1.58  - (void) close 
    1.59  {
    1.60      if( _ipv4socket ) {
    1.61 -        if( _netService ) {
    1.62 -            [_netService stop];
    1.63 -            [_netService release];
    1.64 -            _netService = nil;
    1.65 -            self.bonjourPublished = NO;
    1.66 -        }
    1.67 -        self.bonjourError = 0;
    1.68 -
    1.69 +        [self _closeBonjour];
    1.70          _ipv4socket = closeSocket(_ipv4socket);
    1.71          _ipv6socket = closeSocket(_ipv6socket);
    1.72  
    1.73 @@ -266,6 +246,51 @@
    1.74  #pragma mark BONJOUR:
    1.75  
    1.76  
    1.77 +- (void) _openBonjour
    1.78 +{
    1.79 +    if( self.isOpen && _bonjourServiceType && !_netService) {
    1.80 +        // instantiate the NSNetService object that will advertise on our behalf.
    1.81 +        _netService = [[NSNetService alloc] initWithDomain: @"local." 
    1.82 +                                                      type: _bonjourServiceType
    1.83 +                                                      name: _bonjourServiceName ?:@""
    1.84 +                                                      port: _port];
    1.85 +        if( _netService ) {
    1.86 +            [_netService setDelegate:self];
    1.87 +            if( _bonjourTXTRecord )
    1.88 +                [self _updateTXTRecord];
    1.89 +            [_netService publish];
    1.90 +        } else {
    1.91 +            self.bonjourError = -1;
    1.92 +            Warn(@"%@: Failed to create NSNetService",self);
    1.93 +        }
    1.94 +    }
    1.95 +}
    1.96 +
    1.97 +- (void) _closeBonjour
    1.98 +{
    1.99 +    if( _netService ) {
   1.100 +        [_netService stop];
   1.101 +        [_netService release];
   1.102 +        _netService = nil;
   1.103 +        self.bonjourPublished = NO;
   1.104 +    }
   1.105 +    if( self.bonjourError )
   1.106 +        self.bonjourError = 0;
   1.107 +}
   1.108 +
   1.109 +
   1.110 +- (NSString*) bonjourServiceName {return _bonjourServiceName;}
   1.111 +
   1.112 +- (void) setBonjourServiceName: (NSString*)name
   1.113 +{
   1.114 +    if( ! $equal(name,_bonjourServiceName) ) {
   1.115 +        [self _closeBonjour];
   1.116 +        setObj(&_bonjourServiceName,name);
   1.117 +        [self _openBonjour];
   1.118 +    }
   1.119 +}
   1.120 +
   1.121 +
   1.122  - (NSDictionary*) bonjourTXTRecord
   1.123  {
   1.124      return _bonjourTXTRecord;