TCP/TCPConnection.h
author Jens Alfke <jens@mooseyard.com>
Sun May 25 13:43:03 2008 -0700 (2008-05-25)
changeset 7 5936db2c1987
parent 2 9fdd8dba529c
child 8 6f539dd9921c
permissions -rw-r--r--
Added -[TCPConnection initToNetService:] to make it easier to use with Bonjour. This allowed me to simplify BLIPEchoClient quite a lot.
     1 //
     2 //  TCPConnection.h
     3 //  MYNetwork
     4 //
     5 //  Created by Jens Alfke on 5/18/08.
     6 //  Copyright 2008 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import "TCPEndpoint.h"
    10 @class IPAddress;
    11 @class TCPReader, TCPWriter, TCPListener;
    12 @protocol TCPConnectionDelegate;
    13 
    14 
    15 typedef enum {
    16     kTCP_Disconnected = -1,
    17     kTCP_Closed,
    18     kTCP_Opening,
    19     kTCP_Open,
    20     kTCP_Closing
    21 } TCPConnectionStatus;
    22 
    23 
    24 /** A generic class that manages a TCP socket connection.
    25     It creates a TCPReader and a TCPWriter to handle I/O.
    26     TCPConnection itself mostly deals with SSL setup and opening/closing the socket.
    27     (The SSL related methods are inherited from TCPEndpoint.) */
    28 @interface TCPConnection : TCPEndpoint
    29 {
    30     @private
    31     TCPListener *_server;
    32     IPAddress *_address;
    33     BOOL _isIncoming, _checkedPeerCert;
    34     TCPConnectionStatus _status;
    35     TCPReader *_reader;
    36     TCPWriter *_writer;
    37     NSError *_error;
    38 }
    39 
    40 /** Initializes a TCPConnection to the given IP address.
    41     Afer configuring settings, you should call -open to begin the connection. */
    42 - (id) initToAddress: (IPAddress*)address;
    43 
    44 /** Initializes a TCPConnection to the given IP address, binding to a specific outgoing port
    45     number. (This is usually only necessary when attempting to tunnel through a NAT.) */
    46 - (id) initToAddress: (IPAddress*)address
    47            localPort: (UInt16)localPort;
    48 
    49 /** Initializes a TCPConnection to the given NSNetService's address and port.
    50     If the service's address cannot be resolved, nil is returned. */
    51 - (id) initToNetService: (NSNetService*)service;
    52 
    53 /** Initializes a TCPConnection from an incoming TCP socket.
    54     You don't usually need to call this; TCPListener does it automatically. */
    55 - (id) initIncomingFromSocket: (CFSocketNativeHandle)socket listener: (TCPListener*)listener;
    56 
    57 /** The delegate object that will be called when the connection opens, closes or receives messages. */
    58 @property (assign) id<TCPConnectionDelegate> delegate;
    59 
    60 /** The certificate(s) of the connected peer, if this connection uses SSL.
    61     The items in the array are SecCertificateRefs; use the Keychain API to work with them. */
    62 @property (readonly) NSArray *peerSSLCerts;
    63 
    64 /** Connection's current status */
    65 @property (readonly) TCPConnectionStatus status;
    66 
    67 /** Opens the connection. This happens asynchronously; wait for a delegate method to be called.
    68     You don't need to open incoming connections received via a TCPListener. */
    69 - (void) open;
    70 
    71 /** Closes the connection, after waiting for all in-progress messages to be sent or received.
    72     This happens asynchronously; wait for a delegate method to be called.*/
    73 - (void) close;
    74 
    75 /** Closes the connection, like -close, but if it hasn't closed by the time the timeout
    76     expires, it will disconnect the socket. */
    77 - (void) closeWithTimeout: (NSTimeInterval)timeout;
    78 
    79 /** Closes all open TCPConnections. */
    80 + (void) closeAllWithTimeout: (NSTimeInterval)timeout;
    81 
    82 /** Blocks until all open TCPConnections close. You should call +closeAllWithTimeout: first. */
    83 + (void) waitTillAllClosed;
    84 
    85 /** The IP address of the other peer. */
    86 @property (readonly,retain) IPAddress *address;
    87 
    88 /** The TCPListener that created this incoming connection, or nil */
    89 @property (readonly) TCPListener *server;
    90 
    91 /** Is this an incoming connection, received via a TCPListener? */
    92 @property (readonly) BOOL isIncoming;
    93 
    94 /** The fatal error, if any, 
    95     that caused the connection to fail to open or to disconnect unexpectedly. */
    96 @property (readonly) NSError *error;
    97 
    98 /** The actual security level of this connection. 
    99     Value is nil or one of the security level constants from NSStream.h,
   100     such as NSStreamSocketSecurityLevelTLSv1. */
   101 @property (readonly) NSString* actualSecurityLevel;
   102 
   103 
   104 @property (readonly) TCPReader *reader;
   105 @property (readonly) TCPWriter *writer;
   106 
   107 
   108 // protected:
   109 - (Class) readerClass;
   110 - (Class) writerClass;
   111 
   112 @end
   113 
   114 
   115 
   116 /** The delegate messages sent by TCPConnection.
   117     All methods are optional. */
   118 @protocol TCPConnectionDelegate <NSObject>
   119 @optional
   120 /** Called after the connection successfully opens. */
   121 - (void) connectionDidOpen: (TCPConnection*)connection;
   122 /** Called after the connection fails to open due to an error. */
   123 - (void) connection: (TCPConnection*)connection failedToOpen: (NSError*)error;
   124 /** Called when the identity of the peer is known, if using an SSL connection and the SSL
   125     settings say to check the peer's certificate.
   126     This happens, if at all, after the -connectionDidOpen: call. */
   127 - (BOOL) connection: (TCPConnection*)connection authorizeSSLPeer: (SecCertificateRef)peerCert;
   128 /** Called after the connection closes. */
   129 - (void) connectionDidClose: (TCPConnection*)connection;
   130 @end