TCP/TCPConnection.h
author Jens Alfke <jens@mooseyard.com>
Fri May 23 17:37:36 2008 -0700 (2008-05-23)
changeset 0 9d67172bb323
child 2 9fdd8dba529c
permissions -rw-r--r--
First checkin after breaking out of Cloudy
     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 @interface TCPConnection : TCPEndpoint
    28 {
    29     @private
    30     TCPListener *_server;
    31     IPAddress *_address;
    32     BOOL _isIncoming, _checkedPeerCert;
    33     TCPConnectionStatus _status;
    34     TCPReader *_reader;
    35     TCPWriter *_writer;
    36     NSError *_error;
    37 }
    38 
    39 /** Initializes a TCPConnection to the given IP address.
    40     Afer configuring settings, you should call -open to begin the connection. */
    41 - (id) initToAddress: (IPAddress*)address;
    42 
    43 /** Initializes a TCPConnection to the given IP address, binding to a specific outgoing port
    44     number. (This is usually only necessary when attempting to tunnel through a NAT.) */
    45 - (id) initToAddress: (IPAddress*)address
    46            localPort: (UInt16)localPort;
    47 
    48 /** Initializes a TCPConnection from an incoming TCP socket.
    49     You don't usually need to call this; TCPListener does it automatically. */
    50 - (id) initIncomingFromSocket: (CFSocketNativeHandle)socket listener: (TCPListener*)listener;
    51 
    52 /** The delegate object that will be called when the connection opens, closes or receives messages. */
    53 @property (assign) id<TCPConnectionDelegate> delegate;
    54 
    55 /** The certificate(s) of the connected peer, if this connection uses SSL.
    56     The items in the array are SecCertificateRefs; use the Keychain API to work with them. */
    57 @property (readonly) NSArray *peerSSLCerts;
    58 
    59 /** Connection's current status */
    60 @property (readonly) TCPConnectionStatus status;
    61 
    62 /** Opens the connection. This happens asynchronously; wait for a delegate method to be called.
    63     You don't need to open incoming connections received via a TCPListener. */
    64 - (void) open;
    65 
    66 /** Closes the connection, after waiting for all in-progress messages to be sent or received.
    67     This happens asynchronously; wait for a delegate method to be called.*/
    68 - (void) close;
    69 
    70 /** Closes the connection, like -close, but if it hasn't closed by the time the timeout
    71     expires, it will disconnect the socket. */
    72 - (void) closeWithTimeout: (NSTimeInterval)timeout;
    73 
    74 /** Closes all open TCPConnections. */
    75 + (void) closeAllWithTimeout: (NSTimeInterval)timeout;
    76 
    77 /** Blocks until all open TCPConnections close. You should call +closeAllWithTimeout: first. */
    78 + (void) waitTillAllClosed;
    79 
    80 /** The IP address of the other peer. */
    81 @property (readonly) IPAddress *address;
    82 
    83 /** The TCPListener that created this incoming connection, or nil */
    84 @property (readonly) TCPListener *server;
    85 
    86 /** Is this an incoming connection, received via a TCPListener? */
    87 @property (readonly) BOOL isIncoming;
    88 
    89 /** The fatal error, if any, 
    90     that caused the connection to fail to open or to disconnect unexpectedly. */
    91 @property (readonly) NSError *error;
    92 
    93 /** The actual security level of this connection. 
    94     Value is nil or one of the security level constants from NSStream.h,
    95     such as NSStreamSocketSecurityLevelTLSv1. */
    96 @property (readonly) NSString* actualSecurityLevel;
    97 
    98 
    99 @property (readonly) TCPReader *reader;
   100 @property (readonly) TCPWriter *writer;
   101 
   102 
   103 // protected:
   104 - (Class) readerClass;
   105 - (Class) writerClass;
   106 
   107 @end
   108 
   109 
   110 
   111 /** The delegate messages sent by TCPConnection. */
   112 @protocol TCPConnectionDelegate <NSObject>
   113 @optional
   114 /** Called after the connection successfully opens. */
   115 - (void) connectionDidOpen: (TCPConnection*)connection;
   116 /** Called after the connection fails to open due to an error. */
   117 - (void) connection: (TCPConnection*)connection failedToOpen: (NSError*)error;
   118 /** Called when the identity of the peer is known, if using an SSL connection and the SSL
   119     settings say to check the peer's certificate.
   120     This happens, if at all, after the -connectionDidOpen: call. */
   121 - (BOOL) connection: (TCPConnection*)connection authorizeSSLPeer: (SecCertificateRef)peerCert;
   122 /** Called after the connection closes. */
   123 - (void) connectionDidClose: (TCPConnection*)connection;
   124 @end