Copied the necessary Google Toolbox source files into the MYUtilities project, so people don't have to download a separate library.
5 // Created by Jens Alfke on 5/18/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
9 #import "TCPEndpoint.h"
10 #import <Security/Security.h>
12 @class TCPReader, TCPWriter, TCPListener;
13 @protocol TCPConnectionDelegate;
17 kTCP_Disconnected = -1,
22 } TCPConnectionStatus;
25 /** A generic class that manages a TCP socket connection.
26 It creates a TCPReader and a TCPWriter to handle I/O.
27 TCPConnection itself mostly deals with SSL setup and opening/closing the socket.
28 (The SSL related methods are inherited from TCPEndpoint.) */
29 @interface TCPConnection : TCPEndpoint
34 BOOL _isIncoming, _checkedPeerCert;
35 TCPConnectionStatus _status;
41 /** Initializes a TCPConnection to the given IP address.
42 Afer configuring settings, you should call -open to begin the connection. */
43 - (id) initToAddress: (IPAddress*)address;
45 /** Initializes a TCPConnection to the given IP address, binding to a specific outgoing port
46 number. (This is usually only necessary when attempting to tunnel through a NAT.) */
47 - (id) initToAddress: (IPAddress*)address
48 localPort: (UInt16)localPort;
50 /** Initializes a TCPConnection to the given NSNetService's address and port.
51 If the service's address cannot be resolved, nil is returned. */
52 - (id) initToNetService: (NSNetService*)service;
54 /** Initializes a TCPConnection from an incoming TCP socket.
55 You don't usually need to call this; TCPListener does it automatically. */
56 - (id) initIncomingFromSocket: (CFSocketNativeHandle)socket listener: (TCPListener*)listener;
58 /** The delegate object that will be called when the connection opens, closes or receives messages. */
59 @property (assign) id<TCPConnectionDelegate> delegate;
61 /** The certificate(s) of the connected peer, if this connection uses SSL.
62 The items in the array are SecCertificateRefs; use the Keychain API to work with them. */
63 @property (readonly) NSArray *peerSSLCerts;
65 /** Connection's current status */
66 @property (readonly) TCPConnectionStatus status;
68 /** Opens the connection. This happens asynchronously; wait for a delegate method to be called.
69 You don't need to open incoming connections received via a TCPListener. */
72 /** Closes the connection, after waiting for all in-progress messages to be sent or received.
73 This happens asynchronously; wait for a delegate method to be called.*/
76 /** Closes the connection, like -close, but if it hasn't closed by the time the timeout
77 expires, it will disconnect the socket. */
78 - (void) closeWithTimeout: (NSTimeInterval)timeout;
80 /** Closes all open TCPConnections. */
81 + (void) closeAllWithTimeout: (NSTimeInterval)timeout;
83 /** Blocks until all open TCPConnections close. You should call +closeAllWithTimeout: first. */
84 + (void) waitTillAllClosed;
86 /** The IP address of the other peer. */
87 @property (readonly,retain) IPAddress *address;
89 /** The TCPListener that created this incoming connection, or nil */
90 @property (readonly) TCPListener *server;
92 /** Is this an incoming connection, received via a TCPListener? */
93 @property (readonly) BOOL isIncoming;
95 /** The fatal error, if any,
96 that caused the connection to fail to open or to disconnect unexpectedly. */
97 @property (readonly) NSError *error;
99 /** The actual security level of this connection.
100 Value is nil or one of the security level constants from NSStream.h,
101 such as NSStreamSocketSecurityLevelTLSv1. */
102 @property (readonly) NSString* actualSecurityLevel;
105 @property (readonly) TCPReader *reader;
106 @property (readonly) TCPWriter *writer;
110 - (Class) readerClass;
111 - (Class) writerClass;
117 /** The delegate messages sent by TCPConnection.
118 All methods are optional. */
119 @protocol TCPConnectionDelegate <NSObject>
121 /** Called after the connection successfully opens. */
122 - (void) connectionDidOpen: (TCPConnection*)connection;
123 /** Called after the connection fails to open due to an error. */
124 - (void) connection: (TCPConnection*)connection failedToOpen: (NSError*)error;
125 /** Called when the identity of the peer is known, if using an SSL connection and the SSL
126 settings say to check the peer's certificate.
127 This happens, if at all, after the -connectionDidOpen: call. */
128 - (BOOL) connection: (TCPConnection*)connection authorizeSSLPeer: (SecCertificateRef)peerCert;
129 /** Called after the connection closes. */
130 - (void) connectionDidClose: (TCPConnection*)connection;