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@7
|
49 |
/** Initializes a TCPConnection to the given NSNetService's address and port.
|
jens@7
|
50 |
If the service's address cannot be resolved, nil is returned. */
|
jens@7
|
51 |
- (id) initToNetService: (NSNetService*)service;
|
jens@7
|
52 |
|
jens@0
|
53 |
/** Initializes a TCPConnection from an incoming TCP socket.
|
jens@0
|
54 |
You don't usually need to call this; TCPListener does it automatically. */
|
jens@0
|
55 |
- (id) initIncomingFromSocket: (CFSocketNativeHandle)socket listener: (TCPListener*)listener;
|
jens@0
|
56 |
|
jens@0
|
57 |
/** The delegate object that will be called when the connection opens, closes or receives messages. */
|
jens@0
|
58 |
@property (assign) id<TCPConnectionDelegate> delegate;
|
jens@0
|
59 |
|
jens@0
|
60 |
/** The certificate(s) of the connected peer, if this connection uses SSL.
|
jens@0
|
61 |
The items in the array are SecCertificateRefs; use the Keychain API to work with them. */
|
jens@0
|
62 |
@property (readonly) NSArray *peerSSLCerts;
|
jens@0
|
63 |
|
jens@0
|
64 |
/** Connection's current status */
|
jens@0
|
65 |
@property (readonly) TCPConnectionStatus status;
|
jens@0
|
66 |
|
jens@0
|
67 |
/** Opens the connection. This happens asynchronously; wait for a delegate method to be called.
|
jens@0
|
68 |
You don't need to open incoming connections received via a TCPListener. */
|
jens@0
|
69 |
- (void) open;
|
jens@0
|
70 |
|
jens@0
|
71 |
/** Closes the connection, after waiting for all in-progress messages to be sent or received.
|
jens@0
|
72 |
This happens asynchronously; wait for a delegate method to be called.*/
|
jens@0
|
73 |
- (void) close;
|
jens@0
|
74 |
|
jens@0
|
75 |
/** Closes the connection, like -close, but if it hasn't closed by the time the timeout
|
jens@0
|
76 |
expires, it will disconnect the socket. */
|
jens@0
|
77 |
- (void) closeWithTimeout: (NSTimeInterval)timeout;
|
jens@0
|
78 |
|
jens@0
|
79 |
/** Closes all open TCPConnections. */
|
jens@0
|
80 |
+ (void) closeAllWithTimeout: (NSTimeInterval)timeout;
|
jens@0
|
81 |
|
jens@0
|
82 |
/** Blocks until all open TCPConnections close. You should call +closeAllWithTimeout: first. */
|
jens@0
|
83 |
+ (void) waitTillAllClosed;
|
jens@0
|
84 |
|
jens@0
|
85 |
/** The IP address of the other peer. */
|
jens@7
|
86 |
@property (readonly,retain) IPAddress *address;
|
jens@0
|
87 |
|
jens@0
|
88 |
/** The TCPListener that created this incoming connection, or nil */
|
jens@0
|
89 |
@property (readonly) TCPListener *server;
|
jens@0
|
90 |
|
jens@0
|
91 |
/** Is this an incoming connection, received via a TCPListener? */
|
jens@0
|
92 |
@property (readonly) BOOL isIncoming;
|
jens@0
|
93 |
|
jens@0
|
94 |
/** The fatal error, if any,
|
jens@0
|
95 |
that caused the connection to fail to open or to disconnect unexpectedly. */
|
jens@0
|
96 |
@property (readonly) NSError *error;
|
jens@0
|
97 |
|
jens@0
|
98 |
/** The actual security level of this connection.
|
jens@0
|
99 |
Value is nil or one of the security level constants from NSStream.h,
|
jens@0
|
100 |
such as NSStreamSocketSecurityLevelTLSv1. */
|
jens@0
|
101 |
@property (readonly) NSString* actualSecurityLevel;
|
jens@0
|
102 |
|
jens@0
|
103 |
|
jens@0
|
104 |
@property (readonly) TCPReader *reader;
|
jens@0
|
105 |
@property (readonly) TCPWriter *writer;
|
jens@0
|
106 |
|
jens@0
|
107 |
|
jens@0
|
108 |
// protected:
|
jens@0
|
109 |
- (Class) readerClass;
|
jens@0
|
110 |
- (Class) writerClass;
|
jens@0
|
111 |
|
jens@0
|
112 |
@end
|
jens@0
|
113 |
|
jens@0
|
114 |
|
jens@0
|
115 |
|
jens@2
|
116 |
/** The delegate messages sent by TCPConnection.
|
jens@2
|
117 |
All methods are optional. */
|
jens@0
|
118 |
@protocol TCPConnectionDelegate <NSObject>
|
jens@0
|
119 |
@optional
|
jens@0
|
120 |
/** Called after the connection successfully opens. */
|
jens@0
|
121 |
- (void) connectionDidOpen: (TCPConnection*)connection;
|
jens@0
|
122 |
/** Called after the connection fails to open due to an error. */
|
jens@0
|
123 |
- (void) connection: (TCPConnection*)connection failedToOpen: (NSError*)error;
|
jens@0
|
124 |
/** Called when the identity of the peer is known, if using an SSL connection and the SSL
|
jens@0
|
125 |
settings say to check the peer's certificate.
|
jens@0
|
126 |
This happens, if at all, after the -connectionDidOpen: call. */
|
jens@0
|
127 |
- (BOOL) connection: (TCPConnection*)connection authorizeSSLPeer: (SecCertificateRef)peerCert;
|
jens@0
|
128 |
/** Called after the connection closes. */
|
jens@0
|
129 |
- (void) connectionDidClose: (TCPConnection*)connection;
|
jens@0
|
130 |
@end
|