TCP/TCPConnection.m
changeset 7 5936db2c1987
parent 1 8267d5c429c4
child 8 6f539dd9921c
     1.1 --- a/TCP/TCPConnection.m	Sat May 24 13:26:02 2008 -0700
     1.2 +++ b/TCP/TCPConnection.m	Sun May 25 13:43:03 2008 -0700
     1.3 @@ -19,6 +19,7 @@
     1.4  
     1.5  @interface TCPConnection ()
     1.6  @property TCPConnectionStatus status;
     1.7 +@property (retain) IPAddress *address;
     1.8  - (BOOL) _checkIfClosed;
     1.9  - (void) _closed;
    1.10  @end
    1.11 @@ -40,7 +41,7 @@
    1.12  {
    1.13      self = [super init];
    1.14      if (self != nil) {
    1.15 -        if( !address || !input || !output ) {
    1.16 +        if( !input || !output ) {
    1.17              LogTo(TCP,@"Failed to create %@: addr=%@, in=%@, out=%@",
    1.18                    self.class,address,input,output);
    1.19              [self release];
    1.20 @@ -49,7 +50,7 @@
    1.21          _address = [address copy];
    1.22          _reader = [[[self readerClass] alloc] initWithConnection: self stream: input];
    1.23          _writer = [[[self writerClass] alloc] initWithConnection: self stream: output];
    1.24 -        LogTo(TCP,@"%@ initialized",self);
    1.25 +        LogTo(TCP,@"%@ initialized, address=%@",self,address);
    1.26      }
    1.27      return self;
    1.28  }
    1.29 @@ -74,6 +75,22 @@
    1.30      return [self initToAddress: address localPort: 0];
    1.31  }
    1.32  
    1.33 +- (id) initToNetService: (NSNetService*)service
    1.34 +{
    1.35 +    IPAddress *address = nil;
    1.36 +    NSInputStream *input;
    1.37 +    NSOutputStream *output;
    1.38 +    if( [service getInputStream: &input outputStream: &output] ) {
    1.39 +        NSArray *addresses = service.addresses;
    1.40 +        if( addresses.count > 0 )
    1.41 +            address = [[[IPAddress alloc] initWithSockAddr: [[addresses objectAtIndex: 0] bytes]] autorelease];
    1.42 +    } else {
    1.43 +        input = nil;
    1.44 +        output = nil;
    1.45 +    }
    1.46 +    return [self _initWithAddress: address inputStream: input outputStream: output];
    1.47 +}
    1.48 +
    1.49  
    1.50  - (id) initIncomingFromSocket: (CFSocketNativeHandle)socket
    1.51                       listener: (TCPListener*)listener
    1.52 @@ -253,8 +270,10 @@
    1.53  
    1.54  - (void) _streamOpened: (TCPStream*)stream
    1.55  {
    1.56 +    if( ! _address )
    1.57 +        self.address = stream.peerAddress;
    1.58      if( _status==kTCP_Opening && _reader.isOpen && _writer.isOpen ) {
    1.59 -        LogTo(TCP,@"%@ opened",self);
    1.60 +        LogTo(TCP,@"%@ opened; address=%@",self,_address);
    1.61          self.status = kTCP_Open;
    1.62          [self tellDelegate: @selector(connectionDidOpen:) withObject: nil];
    1.63      }