diff -r 8267d5c429c4 -r 5936db2c1987 TCP/TCPConnection.m --- a/TCP/TCPConnection.m Sat May 24 13:26:02 2008 -0700 +++ b/TCP/TCPConnection.m Sun May 25 13:43:03 2008 -0700 @@ -19,6 +19,7 @@ @interface TCPConnection () @property TCPConnectionStatus status; +@property (retain) IPAddress *address; - (BOOL) _checkIfClosed; - (void) _closed; @end @@ -40,7 +41,7 @@ { self = [super init]; if (self != nil) { - if( !address || !input || !output ) { + if( !input || !output ) { LogTo(TCP,@"Failed to create %@: addr=%@, in=%@, out=%@", self.class,address,input,output); [self release]; @@ -49,7 +50,7 @@ _address = [address copy]; _reader = [[[self readerClass] alloc] initWithConnection: self stream: input]; _writer = [[[self writerClass] alloc] initWithConnection: self stream: output]; - LogTo(TCP,@"%@ initialized",self); + LogTo(TCP,@"%@ initialized, address=%@",self,address); } return self; } @@ -74,6 +75,22 @@ return [self initToAddress: address localPort: 0]; } +- (id) initToNetService: (NSNetService*)service +{ + IPAddress *address = nil; + NSInputStream *input; + NSOutputStream *output; + if( [service getInputStream: &input outputStream: &output] ) { + NSArray *addresses = service.addresses; + if( addresses.count > 0 ) + address = [[[IPAddress alloc] initWithSockAddr: [[addresses objectAtIndex: 0] bytes]] autorelease]; + } else { + input = nil; + output = nil; + } + return [self _initWithAddress: address inputStream: input outputStream: output]; +} + - (id) initIncomingFromSocket: (CFSocketNativeHandle)socket listener: (TCPListener*)listener @@ -253,8 +270,10 @@ - (void) _streamOpened: (TCPStream*)stream { + if( ! _address ) + self.address = stream.peerAddress; if( _status==kTCP_Opening && _reader.isOpen && _writer.isOpen ) { - LogTo(TCP,@"%@ opened",self); + LogTo(TCP,@"%@ opened; address=%@",self,_address); self.status = kTCP_Open; [self tellDelegate: @selector(connectionDidOpen:) withObject: nil]; }