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