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