PortMapper/MYPortMapper.m
changeset 28 732576fa8a0d
parent 27 92581f26073e
child 31 1d6924779df7
     1.1 --- a/PortMapper/MYPortMapper.m	Fri Apr 24 10:10:32 2009 -0700
     1.2 +++ b/PortMapper/MYPortMapper.m	Mon Apr 27 09:03:56 2009 -0700
     1.3 @@ -33,6 +33,7 @@
     1.4      if (self != nil) {
     1.5          _localPort = localPort;
     1.6          _mapTCP = YES;
     1.7 +        self.continuous = YES;
     1.8          [self priv_updateLocalAddress];
     1.9      }
    1.10      return self;
    1.11 @@ -87,11 +88,9 @@
    1.12                publicAddress: (UInt32)rawPublicAddress
    1.13                   publicPort: (UInt16)publicPort
    1.14  {
    1.15 -    LogTo(PortMapper,@"Callback got err %i, addr %08X:%hu",
    1.16 -          errorCode, rawPublicAddress, publicPort);
    1.17      if( errorCode==kDNSServiceErr_NoError ) {
    1.18          if( rawPublicAddress==0 || (publicPort==0 && (_mapTCP || _mapUDP)) ) {
    1.19 -            LogTo(PortMapper,@"(Callback reported no mapping available)");
    1.20 +            LogTo(PortMapper,@"%@: No port-map available", self);
    1.21              errorCode = kDNSServiceErr_NATPortMappingUnsupported;
    1.22          }
    1.23      }
    1.24 @@ -104,8 +103,8 @@
    1.25          self.publicAddress = publicAddress;
    1.26      
    1.27      if( ! errorCode ) {
    1.28 -        LogTo(PortMapper,@"Callback got %08X:%hu -> %@ (mapped=%i)",
    1.29 -              rawPublicAddress,publicPort, self.publicAddress, self.isMapped);
    1.30 +        LogTo(PortMapper,@"%@: Public addr is %@ (mapped=%i)",
    1.31 +              self, self.publicAddress, self.isMapped);
    1.32      }
    1.33      [[NSNotificationCenter defaultCenter] postNotificationName: MYPortMapperChangedNotification
    1.34                                                          object: self];
    1.35 @@ -128,13 +127,11 @@
    1.36                        void                             *context
    1.37                        )
    1.38  {
    1.39 -    NSAutoreleasePool *pool = [NSAutoreleasePool new];
    1.40      @try{
    1.41          [(MYPortMapper*)context priv_portMapStatus: errorCode 
    1.42                                       publicAddress: publicAddress
    1.43                                          publicPort: ntohs(publicPort)];  // port #s in network byte order!
    1.44      }catchAndReport(@"PortMapper");
    1.45 -    [pool drain];
    1.46  }
    1.47  
    1.48  
    1.49 @@ -157,24 +154,12 @@
    1.50  }
    1.51  
    1.52  
    1.53 -- (void) stopService
    1.54 -{
    1.55 -    [super stopService];
    1.56 -    if (_publicAddress)
    1.57 -        self.publicAddress = nil;
    1.58 -}
    1.59 -
    1.60 -
    1.61  - (BOOL) waitTillOpened
    1.62  {
    1.63      if( ! self.serviceRef )
    1.64 -        if( ! [self open] )
    1.65 +        if( ! [self start] )
    1.66              return NO;
    1.67 -    // Run the runloop until there's either an error or a result:
    1.68 -    while( self.error==0 && _publicAddress==nil )
    1.69 -        if( ! [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode
    1.70 -                                       beforeDate: [NSDate distantFuture]] )
    1.71 -            break;
    1.72 +    [self waitForReply];
    1.73      return (self.error==0);
    1.74  }
    1.75  
    1.76 @@ -183,9 +168,10 @@
    1.77  {
    1.78      IPAddress *addr = nil;
    1.79      MYPortMapper *mapper = [[self alloc] initWithNullMapping];
    1.80 +    mapper.continuous = NO;
    1.81      if( [mapper waitTillOpened] )
    1.82          addr = [mapper.publicAddress retain];
    1.83 -    [mapper close];
    1.84 +    [mapper stop];
    1.85      [mapper release];
    1.86      return [addr autorelease];
    1.87  }