17 | If you are a seasoned networking professional, with 10+ years of experience writing low-level socket code,
18 | and detailed knowledge of the underlying BSD networking stack, then you can skip the CommonPitfalls page.
19 | Otherwise, it should be considered mandatory reading:
20 | https://github.com/robbiehanson/CocoaAsyncSocket/wiki/CommonPitfalls
21 |
22 |
23 |
24 | A little bit of investment in your knowledge and understanding of networking fundamentals can go a long way.
25 | And it can save you a LOT of time and frustration in the long run.
26 |
27 |
28 |
29 | Your first goto for reference should ALWAYS be the header files. They are extremely well documented. Please read them.
30 |
31 |
32 |
33 | Did I mention you should read the headers? They're docemented very nicely, in plain english.
34 |
35 |
36 |
37 | If you have any questions you are welcome to post to the CocoaAsyncSocket mailing list:
38 | http://groups.google.com/group/cocoaasyncsocket
39 |
40 | The list is archived, and available for browsing online.
41 | You may be able to instantly find the answer you're looking for with a quick search.
42 |
43 |
44 |
We hope the CocoaAsyncSocket project can provide you with powerful and easy to use networking libraries.
17 | If you are a seasoned networking professional, with 10+ years of experience writing low-level socket code,
18 | and detailed knowledge of the underlying BSD networking stack, then you can skip the CommonPitfalls page.
19 | Otherwise, it should be considered mandatory reading:
20 | https://github.com/robbiehanson/CocoaAsyncSocket/wiki/CommonPitfalls
21 |
22 |
23 |
24 | A little bit of investment in your knowledge and understanding of networking fundamentals can go a long way.
25 | And it can save you a LOT of time and frustration in the long run.
26 |
27 |
28 |
29 | Your first goto for reference should ALWAYS be the header files. They are extremely well documented. Please read them.
30 |
31 |
32 |
33 | Did I mention you should read the headers? They're docemented very nicely, in plain english.
34 |
35 |
36 |
37 | If you have any questions you are welcome to post to the CocoaAsyncSocket mailing list:
38 | http://groups.google.com/group/cocoaasyncsocket
39 |
40 | The list is archived, and available for browsing online.
41 | You may be able to instantly find the answer you're looking for with a quick search.
42 |
43 |
44 |
We hope the CocoaAsyncSocket project can provide you with powerful and easy to use networking libraries.
45 |
46 |
47 |
--------------------------------------------------------------------------------
/SocketClient-Demo/AppDelegate.m:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.m
3 | // udp
4 | //
5 | // Created by jakey on 14-2-26.
6 | // Copyright (c) 2014年 jakey. All rights reserved.
7 | //
8 |
9 | #import "AppDelegate.h"
10 |
11 | @implementation AppDelegate
12 |
13 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
14 | {
15 | // Override point for customization after application launch.
16 | NSString *test = [[NSBundle mainBundle]bundlePath];
17 |
18 | return YES;
19 | }
20 |
21 | - (void)applicationWillResignActive:(UIApplication *)application
22 | {
23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
24 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
25 | }
26 |
27 | - (void)applicationDidEnterBackground:(UIApplication *)application
28 | {
29 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
30 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
31 | }
32 |
33 | - (void)applicationWillEnterForeground:(UIApplication *)application
34 | {
35 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
36 | }
37 |
38 | - (void)applicationDidBecomeActive:(UIApplication *)application
39 | {
40 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
41 | }
42 |
43 | - (void)applicationWillTerminate:(UIApplication *)application
44 | {
45 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
46 | }
47 |
48 | @end
49 |
--------------------------------------------------------------------------------
/SocketClient-Demo/TCPViewController.m:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.m
3 | // udp
4 | //
5 | // Created by jakey on 14-2-26.
6 | // Copyright (c) 2014年 jakey. All rights reserved.
7 | //
8 | #define HOST @"127.0.0.1"
9 | #define PORT 8808
10 |
11 | #import "TCPViewController.h"
12 |
13 | @interface TCPViewController ()
14 |
15 | @end
16 |
17 | @implementation TCPViewController
18 |
19 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
20 | {
21 | self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
22 | if (self) {
23 | // Custom initialization
24 | }
25 | return self;
26 | }
27 | - (IBAction)connectTCPTest:(id)sender{
28 | if (!_asyncSocket)
29 | {
30 | _asyncSocket=nil;
31 | }
32 |
33 | _asyncSocket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
34 | _asyncSocket.delegate = self;
35 |
36 | NSError *error = nil;
37 | [_asyncSocket connectToHost:HOST onPort:PORT withTimeout:-1 error:&error];
38 | if (error!=nil) {
39 | NSLog(@"连接失败:%@",error);
40 | }else{
41 | NSLog(@"连接成功");
42 | }
43 | }
44 | - (IBAction)sendTCPTouched:(id)sender{
45 | [_asyncSocket writeData:[_submitText.text dataUsingEncoding:NSUTF8StringEncoding] withTimeout:3 tag:0];
46 | }
47 |
48 |
49 |
50 | - (void)socket:(GCDAsyncSocket *)sock willDisconnectWithError:(NSError *)err
51 | {
52 | NSLog(@"willDisconnectWithError");
53 | //[self logInfo:FORMAT(@"Client Disconnected: %@:%hu", [sock connectedHost], [sock connectedPort])];
54 | if (err) {
55 | NSLog(@"错误报告:%@",err);
56 | }else{
57 | NSLog(@"连接工作正常");
58 | }
59 | _asyncSocket = nil;
60 | }
61 |
62 | - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
63 | {
64 | NSLog(@"didConnectToHost");
65 | NSData *writeData = [@"connected\r\n" dataUsingEncoding:NSUTF8StringEncoding];
66 | [sock writeData:writeData withTimeout:-1 tag:0];
67 | [sock readDataWithTimeout:0.5 tag:0];
68 | }
69 |
70 | - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
71 | {
72 | NSLog(@"didReadData");
73 | NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length])];
74 | NSString *msg = [[NSString alloc] initWithData:strData encoding:NSUTF8StringEncoding];
75 | if(msg)
76 | {
77 | NSLog(@"%@",msg);
78 | }
79 | else
80 | {
81 | NSLog(@"错误");
82 | }
83 | [sock readDataWithTimeout:-1 tag:0]; //一直监听网络
84 |
85 | }
86 |
87 | - (void)socket:(GCDAsyncSocket *)sock didReadPartialDataOfLength:(NSUInteger)partialLength tag:(long)tag{
88 |
89 |
90 | }
91 | - (void)viewDidLoad
92 | {
93 | [super viewDidLoad];
94 |
95 | // Do any additional setup after loading the view from its nib.
96 | }
97 |
98 | @end
--------------------------------------------------------------------------------
/SocketClient-Demo/UDPViewController.m:
--------------------------------------------------------------------------------
1 | //
2 | // UDPViewController.m
3 | // udp
4 | //
5 | // Created by Jakey on 15/1/12.
6 | // Copyright (c) 2015年 jakey. All rights reserved.
7 | //
8 |
9 | #define HOST @"127.0.0.1"
10 | #define PORT 8888
11 |
12 | #import "UDPViewController.h"
13 |
14 | @interface UDPViewController ()
15 |
16 | @end
17 |
18 | @implementation UDPViewController
19 |
20 | - (void)viewDidLoad {
21 | [super viewDidLoad];
22 | // Do any additional setup after loading the view.
23 | }
24 |
25 | - (IBAction)connectUDPTest:(id)sender{
26 | int nPort = 2346;
27 |
28 | NSError *error = nil;
29 | if (!_udpSocket)
30 | {
31 | _udpSocket=nil;
32 | }
33 | _udpSocket = [[GCDAsyncUdpSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
34 |
35 |
36 | if (error!=nil) {
37 | NSLog(@"连接失败:%@",error);
38 | }else{
39 | NSLog(@"连接成功");
40 | }
41 | if (![_udpSocket bindToPort:nPort error:&error]) {
42 | NSLog(@"Error starting server (bind): %@", error);
43 | return;
44 | }
45 | if (![_udpSocket enableBroadcast:YES error:&error]) {
46 | NSLog(@"Error enableBroadcast (bind): %@", error);
47 | return;
48 | }
49 | if (![_udpSocket joinMulticastGroup:@"224.0.0.1" error:&error]) {
50 | NSLog(@"Error joinMulticastGroup (bind): %@", error);
51 | return;
52 | }
53 | if (![_udpSocket beginReceiving:&error]) {
54 | [_udpSocket close];
55 | NSLog(@"Error starting server (recv): %@", error);
56 | return;
57 | }
58 |
59 |
60 |
61 | }
62 | - (IBAction)sendUDPTouched:(id)sender{
63 |
64 | [_udpSocket sendData:[_submitText.text dataUsingEncoding:NSUTF8StringEncoding] toHost:HOST port:PORT withTimeout:-1 tag:1];
65 | //NSLog(@"Udp Echo server started on port %hu", [_udpSocket localPort]);
66 |
67 | }
68 |
69 |
70 | -(void)udpSocket:(GCDAsyncUdpSocket *)sock didConnectToAddress:(NSData *)address
71 | {
72 | NSError *error = nil;
73 | NSLog(@"Message didConnectToAddress: %@",[[NSString alloc]initWithData:address encoding:NSUTF8StringEncoding]);
74 | [_udpSocket beginReceiving:&error];
75 | // [sock readDataWithTimeout:-1 tag:0];
76 | }
77 |
78 | -(void)udpSocket:(GCDAsyncUdpSocket *)sock didNotConnect:(NSError *)error
79 | {
80 | NSLog(@"Message didNotConnect: %@",error);
81 | }
82 |
83 | -(void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error
84 | {
85 | NSLog(@"Message didNotSendDataWithTag: %@",error);
86 | }
87 |
88 | -(void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext
89 | {
90 | NSLog(@"filterContext is %@",filterContext);
91 | NSLog(@"Message didReceiveData :%@", [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
92 | // [sock sendData:data toAddress:address withTimeout:-1 tag:0]; //一直监听
93 | }
94 |
95 | -(void)udpSocket:(GCDAsyncUdpSocket *)sock didSendDataWithTag:(long)tag
96 | {
97 | // NSLog(@"Message didSendDataWithTag");
98 | }
99 |
100 | -(void)udpSocketDidClose:(GCDAsyncUdpSocket *)sock withError:(NSError *)error
101 | {
102 | NSLog(@"Message withError: %@",error);
103 | }
104 |
105 |
106 | @end
107 |
--------------------------------------------------------------------------------
/SocketClient-Demo/Base.lproj/LaunchScreen.xib:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
20 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/SocketClient-Demo/Main.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
28 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
83 |
84 |
85 |
86 |
87 |
88 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
130 |
131 |
132 |
133 |
134 |
135 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
--------------------------------------------------------------------------------
/SocketClient-Demo/AsyncSocket/RunLoop/AsyncUdpSocket.h:
--------------------------------------------------------------------------------
1 | //
2 | // AsyncUdpSocket.h
3 | //
4 | // This class is in the public domain.
5 | // Originally created by Robbie Hanson on Wed Oct 01 2008.
6 | // Updated and maintained by Deusty Designs and the Mac development community.
7 | //
8 | // http://code.google.com/p/cocoaasyncsocket/
9 | //
10 |
11 | #import
12 |
13 | @class AsyncSendPacket;
14 | @class AsyncReceivePacket;
15 |
16 | extern NSString *const AsyncUdpSocketException;
17 | extern NSString *const AsyncUdpSocketErrorDomain;
18 |
19 | enum AsyncUdpSocketError
20 | {
21 | AsyncUdpSocketCFSocketError = kCFSocketError, // From CFSocketError enum
22 | AsyncUdpSocketNoError = 0, // Never used
23 | AsyncUdpSocketBadParameter, // Used if given a bad parameter (such as an improper address)
24 | AsyncUdpSocketIPv4Unavailable, // Used if you bind/connect using IPv6 only
25 | AsyncUdpSocketIPv6Unavailable, // Used if you bind/connect using IPv4 only (or iPhone)
26 | AsyncUdpSocketSendTimeoutError,
27 | AsyncUdpSocketReceiveTimeoutError
28 | };
29 | typedef enum AsyncUdpSocketError AsyncUdpSocketError;
30 |
31 | @interface AsyncUdpSocket : NSObject
32 | {
33 | CFSocketRef theSocket4; // IPv4 socket
34 | CFSocketRef theSocket6; // IPv6 socket
35 |
36 | CFRunLoopSourceRef theSource4; // For theSocket4
37 | CFRunLoopSourceRef theSource6; // For theSocket6
38 | CFRunLoopRef theRunLoop;
39 | CFSocketContext theContext;
40 | NSArray *theRunLoopModes;
41 |
42 | NSMutableArray *theSendQueue;
43 | AsyncSendPacket *theCurrentSend;
44 | NSTimer *theSendTimer;
45 |
46 | NSMutableArray *theReceiveQueue;
47 | AsyncReceivePacket *theCurrentReceive;
48 | NSTimer *theReceiveTimer;
49 |
50 | id theDelegate;
51 | UInt16 theFlags;
52 |
53 | long theUserData;
54 |
55 | NSString *cachedLocalHost;
56 | UInt16 cachedLocalPort;
57 |
58 | NSString *cachedConnectedHost;
59 | UInt16 cachedConnectedPort;
60 |
61 | UInt32 maxReceiveBufferSize;
62 | }
63 |
64 | /**
65 | * Creates new instances of AsyncUdpSocket.
66 | **/
67 | - (id)init;
68 | - (id)initWithDelegate:(id)delegate;
69 | - (id)initWithDelegate:(id)delegate userData:(long)userData;
70 |
71 | /**
72 | * Creates new instances of AsyncUdpSocket that support only IPv4 or IPv6.
73 | * The other init methods will support both, unless specifically binded or connected to one protocol.
74 | * If you know you'll only be using one protocol, these init methods may be a bit more efficient.
75 | **/
76 | - (id)initIPv4;
77 | - (id)initIPv6;
78 |
79 | - (id)delegate;
80 | - (void)setDelegate:(id)delegate;
81 |
82 | - (long)userData;
83 | - (void)setUserData:(long)userData;
84 |
85 | /**
86 | * Returns the local address info for the socket.
87 | *
88 | * Note: Address info may not be available until after the socket has been bind'ed,
89 | * or until after data has been sent.
90 | **/
91 | - (NSString *)localHost;
92 | - (UInt16)localPort;
93 |
94 | /**
95 | * Returns the remote address info for the socket.
96 | *
97 | * Note: Since UDP is connectionless by design, connected address info
98 | * will not be available unless the socket is explicitly connected to a remote host/port
99 | **/
100 | - (NSString *)connectedHost;
101 | - (UInt16)connectedPort;
102 |
103 | /**
104 | * Returns whether or not this socket has been connected to a single host.
105 | * By design, UDP is a connectionless protocol, and connecting is not needed.
106 | * If connected, the socket will only be able to send/receive data to/from the connected host.
107 | **/
108 | - (BOOL)isConnected;
109 |
110 | /**
111 | * Returns whether or not this socket has been closed.
112 | * The only way a socket can be closed is if you explicitly call one of the close methods.
113 | **/
114 | - (BOOL)isClosed;
115 |
116 | /**
117 | * Returns whether or not this socket supports IPv4.
118 | * By default this will be true, unless the socket is specifically initialized as IPv6 only,
119 | * or is binded or connected to an IPv6 address.
120 | **/
121 | - (BOOL)isIPv4;
122 |
123 | /**
124 | * Returns whether or not this socket supports IPv6.
125 | * By default this will be true, unless the socket is specifically initialized as IPv4 only,
126 | * or is binded or connected to an IPv4 address.
127 | *
128 | * This method will also return false on platforms that do not support IPv6.
129 | * Note: The iPhone does not currently support IPv6.
130 | **/
131 | - (BOOL)isIPv6;
132 |
133 | /**
134 | * Returns the mtu of the socket.
135 | * If unknown, returns zero.
136 | *
137 | * Sending data larger than this may result in an error.
138 | * This is an advanced topic, and one should understand the wide range of mtu's on networks and the internet.
139 | * Therefore this method is only for reference and may be of little use in many situations.
140 | **/
141 | - (unsigned int)maximumTransmissionUnit;
142 |
143 | /**
144 | * Binds the UDP socket to the given port and optional address.
145 | * Binding should be done for server sockets that receive data prior to sending it.
146 | * Client sockets can skip binding,
147 | * as the OS will automatically assign the socket an available port when it starts sending data.
148 | *
149 | * You cannot bind a socket after its been connected.
150 | * You can only bind a socket once.
151 | * You can still connect a socket (if desired) after binding.
152 | *
153 | * On success, returns YES.
154 | * Otherwise returns NO, and sets errPtr. If you don't care about the error, you can pass nil for errPtr.
155 | **/
156 | - (BOOL)bindToPort:(UInt16)port error:(NSError **)errPtr;
157 | - (BOOL)bindToAddress:(NSString *)localAddr port:(UInt16)port error:(NSError **)errPtr;
158 |
159 | /**
160 | * Connects the UDP socket to the given host and port.
161 | * By design, UDP is a connectionless protocol, and connecting is not needed.
162 | *
163 | * Choosing to connect to a specific host/port has the following effect:
164 | * - You will only be able to send data to the connected host/port.
165 | * - You will only be able to receive data from the connected host/port.
166 | * - You will receive ICMP messages that come from the connected host/port, such as "connection refused".
167 | *
168 | * Connecting a UDP socket does not result in any communication on the socket.
169 | * It simply changes the internal state of the socket.
170 | *
171 | * You cannot bind a socket after its been connected.
172 | * You can only connect a socket once.
173 | *
174 | * On success, returns YES.
175 | * Otherwise returns NO, and sets errPtr. If you don't care about the error, you can pass nil for errPtr.
176 | **/
177 | - (BOOL)connectToHost:(NSString *)host onPort:(UInt16)port error:(NSError **)errPtr;
178 | - (BOOL)connectToAddress:(NSData *)remoteAddr error:(NSError **)errPtr;
179 |
180 | /**
181 | * Join multicast group
182 | *
183 | * Group should be an IP address (eg @"225.228.0.1")
184 | **/
185 | - (BOOL)joinMulticastGroup:(NSString *)group error:(NSError **)errPtr;
186 | - (BOOL)joinMulticastGroup:(NSString *)group withAddress:(NSString *)interface error:(NSError **)errPtr;
187 |
188 | /**
189 | * By default, the underlying socket in the OS will not allow you to send broadcast messages.
190 | * In order to send broadcast messages, you need to enable this functionality in the socket.
191 | *
192 | * A broadcast is a UDP message to addresses like "192.168.255.255" or "255.255.255.255" that is
193 | * delivered to every host on the network.
194 | * The reason this is generally disabled by default is to prevent
195 | * accidental broadcast messages from flooding the network.
196 | **/
197 | - (BOOL)enableBroadcast:(BOOL)flag error:(NSError **)errPtr;
198 |
199 | /**
200 | * Asynchronously sends the given data, with the given timeout and tag.
201 | *
202 | * This method may only be used with a connected socket.
203 | *
204 | * If data is nil or zero-length, this method does nothing and immediately returns NO.
205 | * If the socket is not connected, this method does nothing and immediately returns NO.
206 | **/
207 | - (BOOL)sendData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;
208 |
209 | /**
210 | * Asynchronously sends the given data, with the given timeout and tag, to the given host and port.
211 | *
212 | * This method cannot be used with a connected socket.
213 | *
214 | * If data is nil or zero-length, this method does nothing and immediately returns NO.
215 | * If the socket is connected, this method does nothing and immediately returns NO.
216 | * If unable to resolve host to a valid IPv4 or IPv6 address, this method returns NO.
217 | **/
218 | - (BOOL)sendData:(NSData *)data toHost:(NSString *)host port:(UInt16)port withTimeout:(NSTimeInterval)timeout tag:(long)tag;
219 |
220 | /**
221 | * Asynchronously sends the given data, with the given timeout and tag, to the given address.
222 | *
223 | * This method cannot be used with a connected socket.
224 | *
225 | * If data is nil or zero-length, this method does nothing and immediately returns NO.
226 | * If the socket is connected, this method does nothing and immediately returns NO.
227 | **/
228 | - (BOOL)sendData:(NSData *)data toAddress:(NSData *)remoteAddr withTimeout:(NSTimeInterval)timeout tag:(long)tag;
229 |
230 | /**
231 | * Asynchronously receives a single datagram packet.
232 | *
233 | * If the receive succeeds, the onUdpSocket:didReceiveData:fromHost:port:tag delegate method will be called.
234 | * Otherwise, a timeout will occur, and the onUdpSocket:didNotReceiveDataWithTag: delegate method will be called.
235 | **/
236 | - (void)receiveWithTimeout:(NSTimeInterval)timeout tag:(long)tag;
237 |
238 | /**
239 | * Closes the socket immediately. Any pending send or receive operations are dropped.
240 | **/
241 | - (void)close;
242 |
243 | /**
244 | * Closes after all pending send operations have completed.
245 | * After calling this, the sendData: and receive: methods will do nothing.
246 | * In other words, you won't be able to add any more send or receive operations to the queue.
247 | * The socket will close even if there are still pending receive operations.
248 | **/
249 | - (void)closeAfterSending;
250 |
251 | /**
252 | * Closes after all pending receive operations have completed.
253 | * After calling this, the sendData: and receive: methods will do nothing.
254 | * In other words, you won't be able to add any more send or receive operations to the queue.
255 | * The socket will close even if there are still pending send operations.
256 | **/
257 | - (void)closeAfterReceiving;
258 |
259 | /**
260 | * Closes after all pending send and receive operations have completed.
261 | * After calling this, the sendData: and receive: methods will do nothing.
262 | * In other words, you won't be able to add any more send or receive operations to the queue.
263 | **/
264 | - (void)closeAfterSendingAndReceiving;
265 |
266 | /**
267 | * Gets/Sets the maximum size of the buffer that will be allocated for receive operations.
268 | * The default size is 9216 bytes.
269 | *
270 | * The theoretical maximum size of any IPv4 UDP packet is UINT16_MAX = 65535.
271 | * The theoretical maximum size of any IPv6 UDP packet is UINT32_MAX = 4294967295.
272 | *
273 | * In practice, however, the size of UDP packets will be much smaller.
274 | * Indeed most protocols will send and receive packets of only a few bytes,
275 | * or will set a limit on the size of packets to prevent fragmentation in the IP layer.
276 | *
277 | * If you set the buffer size too small, the sockets API in the OS will silently discard
278 | * any extra data, and you will not be notified of the error.
279 | **/
280 | - (UInt32)maxReceiveBufferSize;
281 | - (void)setMaxReceiveBufferSize:(UInt32)max;
282 |
283 | /**
284 | * When you create an AsyncUdpSocket, it is added to the runloop of the current thread.
285 | * So it is easiest to simply create the socket on the thread you intend to use it.
286 | *
287 | * If, however, you need to move the socket to a separate thread at a later time, this
288 | * method may be used to accomplish the task.
289 | *
290 | * This method must be called from the thread/runloop the socket is currently running on.
291 | *
292 | * Note: After calling this method, all further method calls to this object should be done from the given runloop.
293 | * Also, all delegate calls will be sent on the given runloop.
294 | **/
295 | - (BOOL)moveToRunLoop:(NSRunLoop *)runLoop;
296 |
297 | /**
298 | * Allows you to configure which run loop modes the socket uses.
299 | * The default set of run loop modes is NSDefaultRunLoopMode.
300 | *
301 | * If you'd like your socket to continue operation during other modes, you may want to add modes such as
302 | * NSModalPanelRunLoopMode or NSEventTrackingRunLoopMode. Or you may simply want to use NSRunLoopCommonModes.
303 | *
304 | * Note: NSRunLoopCommonModes is defined in 10.5. For previous versions one can use kCFRunLoopCommonModes.
305 | **/
306 | - (BOOL)setRunLoopModes:(NSArray *)runLoopModes;
307 |
308 | /**
309 | * Returns the current run loop modes the AsyncSocket instance is operating in.
310 | * The default set of run loop modes is NSDefaultRunLoopMode.
311 | **/
312 | - (NSArray *)runLoopModes;
313 |
314 | @end
315 |
316 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
317 | #pragma mark -
318 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
319 |
320 | @protocol AsyncUdpSocketDelegate
321 | @optional
322 |
323 | /**
324 | * Called when the datagram with the given tag has been sent.
325 | **/
326 | - (void)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithTag:(long)tag;
327 |
328 | /**
329 | * Called if an error occurs while trying to send a datagram.
330 | * This could be due to a timeout, or something more serious such as the data being too large to fit in a sigle packet.
331 | **/
332 | - (void)onUdpSocket:(AsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error;
333 |
334 | /**
335 | * Called when the socket has received the requested datagram.
336 | *
337 | * Due to the nature of UDP, you may occasionally receive undesired packets.
338 | * These may be rogue UDP packets from unknown hosts,
339 | * or they may be delayed packets arriving after retransmissions have already occurred.
340 | * It's important these packets are properly ignored, while not interfering with the flow of your implementation.
341 | * As an aid, this delegate method has a boolean return value.
342 | * If you ever need to ignore a received packet, simply return NO,
343 | * and AsyncUdpSocket will continue as if the packet never arrived.
344 | * That is, the original receive request will still be queued, and will still timeout as usual if a timeout was set.
345 | * For example, say you requested to receive data, and you set a timeout of 500 milliseconds, using a tag of 15.
346 | * If rogue data arrives after 250 milliseconds, this delegate method would be invoked, and you could simply return NO.
347 | * If the expected data then arrives within the next 250 milliseconds,
348 | * this delegate method will be invoked, with a tag of 15, just as if the rogue data never appeared.
349 | *
350 | * Under normal circumstances, you simply return YES from this method.
351 | **/
352 | - (BOOL)onUdpSocket:(AsyncUdpSocket *)sock
353 | didReceiveData:(NSData *)data
354 | withTag:(long)tag
355 | fromHost:(NSString *)host
356 | port:(UInt16)port;
357 |
358 | /**
359 | * Called if an error occurs while trying to receive a requested datagram.
360 | * This is generally due to a timeout, but could potentially be something else if some kind of OS error occurred.
361 | **/
362 | - (void)onUdpSocket:(AsyncUdpSocket *)sock didNotReceiveDataWithTag:(long)tag dueToError:(NSError *)error;
363 |
364 | /**
365 | * Called when the socket is closed.
366 | * A socket is only closed if you explicitly call one of the close methods.
367 | **/
368 | - (void)onUdpSocketDidClose:(AsyncUdpSocket *)sock;
369 |
370 | @end
371 |
--------------------------------------------------------------------------------
/SocketClient-Demo.xcodeproj/project.pbxproj:
--------------------------------------------------------------------------------
1 | // !$*UTF8*$!
2 | {
3 | archiveVersion = 1;
4 | classes = {
5 | };
6 | objectVersion = 46;
7 | objects = {
8 |
9 | /* Begin PBXBuildFile section */
10 | A22185FA1A649E3D00E4BF87 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = A22185F91A649E3D00E4BF87 /* main.m */; };
11 | A22185FD1A649E3D00E4BF87 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = A22185FC1A649E3D00E4BF87 /* AppDelegate.m */; };
12 | A22185FF1A649E3D00E4BF87 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A22185FE1A649E3D00E4BF87 /* Images.xcassets */; };
13 | A22186021A649E3D00E4BF87 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = A22186001A649E3D00E4BF87 /* LaunchScreen.xib */; };
14 | A221860E1A649E3D00E4BF87 /* SocketClient_DemoTests.m in Sources */ = {isa = PBXBuildFile; fileRef = A221860D1A649E3D00E4BF87 /* SocketClient_DemoTests.m */; };
15 | A22186241A649E4A00E4BF87 /* Documentation.html in Resources */ = {isa = PBXBuildFile; fileRef = A22186191A649E4A00E4BF87 /* Documentation.html */; };
16 | A22186251A649E4A00E4BF87 /* GCDAsyncSocket.m in Sources */ = {isa = PBXBuildFile; fileRef = A221861B1A649E4A00E4BF87 /* GCDAsyncSocket.m */; };
17 | A22186261A649E4A00E4BF87 /* GCDAsyncUdpSocket.m in Sources */ = {isa = PBXBuildFile; fileRef = A221861D1A649E4A00E4BF87 /* GCDAsyncUdpSocket.m */; };
18 | A22186271A649E4A00E4BF87 /* AsyncSocket.m in Sources */ = {isa = PBXBuildFile; fileRef = A22186201A649E4A00E4BF87 /* AsyncSocket.m */; };
19 | A22186281A649E4A00E4BF87 /* AsyncUdpSocket.m in Sources */ = {isa = PBXBuildFile; fileRef = A22186221A649E4A00E4BF87 /* AsyncUdpSocket.m */; };
20 | A22186291A649E4A00E4BF87 /* Documentation.html in Resources */ = {isa = PBXBuildFile; fileRef = A22186231A649E4A00E4BF87 /* Documentation.html */; };
21 | A221862E1A649E5200E4BF87 /* TCPViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A221862B1A649E5200E4BF87 /* TCPViewController.m */; };
22 | A221862F1A649E5200E4BF87 /* UDPViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A221862D1A649E5200E4BF87 /* UDPViewController.m */; };
23 | A22186341A649E8A00E4BF87 /* tcp.py in Resources */ = {isa = PBXBuildFile; fileRef = A22186311A649E8A00E4BF87 /* tcp.py */; };
24 | A22186351A649E8A00E4BF87 /* udp.py in Resources */ = {isa = PBXBuildFile; fileRef = A22186321A649E8A00E4BF87 /* udp.py */; };
25 | A22186371A649ED800E4BF87 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A22186361A649ED800E4BF87 /* Main.storyboard */; };
26 | /* End PBXBuildFile section */
27 |
28 | /* Begin PBXContainerItemProxy section */
29 | A22186081A649E3D00E4BF87 /* PBXContainerItemProxy */ = {
30 | isa = PBXContainerItemProxy;
31 | containerPortal = A22185EC1A649E3D00E4BF87 /* Project object */;
32 | proxyType = 1;
33 | remoteGlobalIDString = A22185F31A649E3D00E4BF87;
34 | remoteInfo = "SocketClient-Demo";
35 | };
36 | /* End PBXContainerItemProxy section */
37 |
38 | /* Begin PBXFileReference section */
39 | A22185F41A649E3D00E4BF87 /* SocketClient-Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "SocketClient-Demo.app"; sourceTree = BUILT_PRODUCTS_DIR; };
40 | A22185F81A649E3D00E4BF87 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; };
41 | A22185F91A649E3D00E4BF87 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; };
42 | A22185FB1A649E3D00E4BF87 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; };
43 | A22185FC1A649E3D00E4BF87 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; };
44 | A22185FE1A649E3D00E4BF87 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; };
45 | A22186011A649E3D00E4BF87 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = ""; };
46 | A22186071A649E3D00E4BF87 /* SocketClient-DemoTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "SocketClient-DemoTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
47 | A221860C1A649E3D00E4BF87 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; };
48 | A221860D1A649E3D00E4BF87 /* SocketClient_DemoTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SocketClient_DemoTests.m; sourceTree = ""; };
49 | A22186191A649E4A00E4BF87 /* Documentation.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = Documentation.html; sourceTree = ""; };
50 | A221861A1A649E4A00E4BF87 /* GCDAsyncSocket.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GCDAsyncSocket.h; sourceTree = ""; };
51 | A221861B1A649E4A00E4BF87 /* GCDAsyncSocket.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GCDAsyncSocket.m; sourceTree = ""; };
52 | A221861C1A649E4A00E4BF87 /* GCDAsyncUdpSocket.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GCDAsyncUdpSocket.h; sourceTree = ""; };
53 | A221861D1A649E4A00E4BF87 /* GCDAsyncUdpSocket.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GCDAsyncUdpSocket.m; sourceTree = ""; };
54 | A221861F1A649E4A00E4BF87 /* AsyncSocket.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AsyncSocket.h; sourceTree = ""; };
55 | A22186201A649E4A00E4BF87 /* AsyncSocket.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AsyncSocket.m; sourceTree = ""; };
56 | A22186211A649E4A00E4BF87 /* AsyncUdpSocket.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AsyncUdpSocket.h; sourceTree = ""; };
57 | A22186221A649E4A00E4BF87 /* AsyncUdpSocket.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AsyncUdpSocket.m; sourceTree = ""; };
58 | A22186231A649E4A00E4BF87 /* Documentation.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = Documentation.html; sourceTree = ""; };
59 | A221862A1A649E5200E4BF87 /* TCPViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TCPViewController.h; sourceTree = ""; };
60 | A221862B1A649E5200E4BF87 /* TCPViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TCPViewController.m; sourceTree = ""; };
61 | A221862C1A649E5200E4BF87 /* UDPViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UDPViewController.h; sourceTree = ""; };
62 | A221862D1A649E5200E4BF87 /* UDPViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UDPViewController.m; sourceTree = ""; };
63 | A22186311A649E8A00E4BF87 /* tcp.py */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.python; path = tcp.py; sourceTree = SOURCE_ROOT; };
64 | A22186321A649E8A00E4BF87 /* udp.py */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.python; path = udp.py; sourceTree = SOURCE_ROOT; };
65 | A22186361A649ED800E4BF87 /* Main.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Main.storyboard; sourceTree = ""; };
66 | /* End PBXFileReference section */
67 |
68 | /* Begin PBXFrameworksBuildPhase section */
69 | A22185F11A649E3D00E4BF87 /* Frameworks */ = {
70 | isa = PBXFrameworksBuildPhase;
71 | buildActionMask = 2147483647;
72 | files = (
73 | );
74 | runOnlyForDeploymentPostprocessing = 0;
75 | };
76 | A22186041A649E3D00E4BF87 /* Frameworks */ = {
77 | isa = PBXFrameworksBuildPhase;
78 | buildActionMask = 2147483647;
79 | files = (
80 | );
81 | runOnlyForDeploymentPostprocessing = 0;
82 | };
83 | /* End PBXFrameworksBuildPhase section */
84 |
85 | /* Begin PBXGroup section */
86 | A22185EB1A649E3D00E4BF87 = {
87 | isa = PBXGroup;
88 | children = (
89 | A22185F61A649E3D00E4BF87 /* SocketClient-Demo */,
90 | A221860A1A649E3D00E4BF87 /* SocketClient-DemoTests */,
91 | A22185F51A649E3D00E4BF87 /* Products */,
92 | );
93 | sourceTree = "";
94 | };
95 | A22185F51A649E3D00E4BF87 /* Products */ = {
96 | isa = PBXGroup;
97 | children = (
98 | A22185F41A649E3D00E4BF87 /* SocketClient-Demo.app */,
99 | A22186071A649E3D00E4BF87 /* SocketClient-DemoTests.xctest */,
100 | );
101 | name = Products;
102 | sourceTree = "";
103 | };
104 | A22185F61A649E3D00E4BF87 /* SocketClient-Demo */ = {
105 | isa = PBXGroup;
106 | children = (
107 | A22186171A649E4A00E4BF87 /* AsyncSocket */,
108 | A22185FB1A649E3D00E4BF87 /* AppDelegate.h */,
109 | A22185FC1A649E3D00E4BF87 /* AppDelegate.m */,
110 | A22186381A64B13300E4BF87 /* TCP */,
111 | A22186391A64B14000E4BF87 /* UDP */,
112 | A22186361A649ED800E4BF87 /* Main.storyboard */,
113 | A22185FE1A649E3D00E4BF87 /* Images.xcassets */,
114 | A22186001A649E3D00E4BF87 /* LaunchScreen.xib */,
115 | A22185F71A649E3D00E4BF87 /* Supporting Files */,
116 | );
117 | path = "SocketClient-Demo";
118 | sourceTree = "";
119 | };
120 | A22185F71A649E3D00E4BF87 /* Supporting Files */ = {
121 | isa = PBXGroup;
122 | children = (
123 | A22185F81A649E3D00E4BF87 /* Info.plist */,
124 | A22185F91A649E3D00E4BF87 /* main.m */,
125 | );
126 | name = "Supporting Files";
127 | sourceTree = "";
128 | };
129 | A221860A1A649E3D00E4BF87 /* SocketClient-DemoTests */ = {
130 | isa = PBXGroup;
131 | children = (
132 | A221860D1A649E3D00E4BF87 /* SocketClient_DemoTests.m */,
133 | A221860B1A649E3D00E4BF87 /* Supporting Files */,
134 | );
135 | path = "SocketClient-DemoTests";
136 | sourceTree = "";
137 | };
138 | A221860B1A649E3D00E4BF87 /* Supporting Files */ = {
139 | isa = PBXGroup;
140 | children = (
141 | A221860C1A649E3D00E4BF87 /* Info.plist */,
142 | );
143 | name = "Supporting Files";
144 | sourceTree = "";
145 | };
146 | A22186171A649E4A00E4BF87 /* AsyncSocket */ = {
147 | isa = PBXGroup;
148 | children = (
149 | A22186181A649E4A00E4BF87 /* GCD */,
150 | A221861E1A649E4A00E4BF87 /* RunLoop */,
151 | );
152 | path = AsyncSocket;
153 | sourceTree = "";
154 | };
155 | A22186181A649E4A00E4BF87 /* GCD */ = {
156 | isa = PBXGroup;
157 | children = (
158 | A22186191A649E4A00E4BF87 /* Documentation.html */,
159 | A221861A1A649E4A00E4BF87 /* GCDAsyncSocket.h */,
160 | A221861B1A649E4A00E4BF87 /* GCDAsyncSocket.m */,
161 | A221861C1A649E4A00E4BF87 /* GCDAsyncUdpSocket.h */,
162 | A221861D1A649E4A00E4BF87 /* GCDAsyncUdpSocket.m */,
163 | );
164 | path = GCD;
165 | sourceTree = "";
166 | };
167 | A221861E1A649E4A00E4BF87 /* RunLoop */ = {
168 | isa = PBXGroup;
169 | children = (
170 | A221861F1A649E4A00E4BF87 /* AsyncSocket.h */,
171 | A22186201A649E4A00E4BF87 /* AsyncSocket.m */,
172 | A22186211A649E4A00E4BF87 /* AsyncUdpSocket.h */,
173 | A22186221A649E4A00E4BF87 /* AsyncUdpSocket.m */,
174 | A22186231A649E4A00E4BF87 /* Documentation.html */,
175 | );
176 | path = RunLoop;
177 | sourceTree = "";
178 | };
179 | A22186381A64B13300E4BF87 /* TCP */ = {
180 | isa = PBXGroup;
181 | children = (
182 | A221862A1A649E5200E4BF87 /* TCPViewController.h */,
183 | A221862B1A649E5200E4BF87 /* TCPViewController.m */,
184 | A22186311A649E8A00E4BF87 /* tcp.py */,
185 | );
186 | name = TCP;
187 | sourceTree = "";
188 | };
189 | A22186391A64B14000E4BF87 /* UDP */ = {
190 | isa = PBXGroup;
191 | children = (
192 | A22186321A649E8A00E4BF87 /* udp.py */,
193 | A221862C1A649E5200E4BF87 /* UDPViewController.h */,
194 | A221862D1A649E5200E4BF87 /* UDPViewController.m */,
195 | );
196 | name = UDP;
197 | sourceTree = "";
198 | };
199 | /* End PBXGroup section */
200 |
201 | /* Begin PBXNativeTarget section */
202 | A22185F31A649E3D00E4BF87 /* SocketClient-Demo */ = {
203 | isa = PBXNativeTarget;
204 | buildConfigurationList = A22186111A649E3D00E4BF87 /* Build configuration list for PBXNativeTarget "SocketClient-Demo" */;
205 | buildPhases = (
206 | A22185F01A649E3D00E4BF87 /* Sources */,
207 | A22185F11A649E3D00E4BF87 /* Frameworks */,
208 | A22185F21A649E3D00E4BF87 /* Resources */,
209 | );
210 | buildRules = (
211 | );
212 | dependencies = (
213 | );
214 | name = "SocketClient-Demo";
215 | productName = "SocketClient-Demo";
216 | productReference = A22185F41A649E3D00E4BF87 /* SocketClient-Demo.app */;
217 | productType = "com.apple.product-type.application";
218 | };
219 | A22186061A649E3D00E4BF87 /* SocketClient-DemoTests */ = {
220 | isa = PBXNativeTarget;
221 | buildConfigurationList = A22186141A649E3D00E4BF87 /* Build configuration list for PBXNativeTarget "SocketClient-DemoTests" */;
222 | buildPhases = (
223 | A22186031A649E3D00E4BF87 /* Sources */,
224 | A22186041A649E3D00E4BF87 /* Frameworks */,
225 | A22186051A649E3D00E4BF87 /* Resources */,
226 | );
227 | buildRules = (
228 | );
229 | dependencies = (
230 | A22186091A649E3D00E4BF87 /* PBXTargetDependency */,
231 | );
232 | name = "SocketClient-DemoTests";
233 | productName = "SocketClient-DemoTests";
234 | productReference = A22186071A649E3D00E4BF87 /* SocketClient-DemoTests.xctest */;
235 | productType = "com.apple.product-type.bundle.unit-test";
236 | };
237 | /* End PBXNativeTarget section */
238 |
239 | /* Begin PBXProject section */
240 | A22185EC1A649E3D00E4BF87 /* Project object */ = {
241 | isa = PBXProject;
242 | attributes = {
243 | LastUpgradeCheck = 0610;
244 | ORGANIZATIONNAME = www.skyfox.org;
245 | TargetAttributes = {
246 | A22185F31A649E3D00E4BF87 = {
247 | CreatedOnToolsVersion = 6.1;
248 | };
249 | A22186061A649E3D00E4BF87 = {
250 | CreatedOnToolsVersion = 6.1;
251 | TestTargetID = A22185F31A649E3D00E4BF87;
252 | };
253 | };
254 | };
255 | buildConfigurationList = A22185EF1A649E3D00E4BF87 /* Build configuration list for PBXProject "SocketClient-Demo" */;
256 | compatibilityVersion = "Xcode 3.2";
257 | developmentRegion = English;
258 | hasScannedForEncodings = 0;
259 | knownRegions = (
260 | en,
261 | Base,
262 | );
263 | mainGroup = A22185EB1A649E3D00E4BF87;
264 | productRefGroup = A22185F51A649E3D00E4BF87 /* Products */;
265 | projectDirPath = "";
266 | projectRoot = "";
267 | targets = (
268 | A22185F31A649E3D00E4BF87 /* SocketClient-Demo */,
269 | A22186061A649E3D00E4BF87 /* SocketClient-DemoTests */,
270 | );
271 | };
272 | /* End PBXProject section */
273 |
274 | /* Begin PBXResourcesBuildPhase section */
275 | A22185F21A649E3D00E4BF87 /* Resources */ = {
276 | isa = PBXResourcesBuildPhase;
277 | buildActionMask = 2147483647;
278 | files = (
279 | A22186021A649E3D00E4BF87 /* LaunchScreen.xib in Resources */,
280 | A22186241A649E4A00E4BF87 /* Documentation.html in Resources */,
281 | A22186341A649E8A00E4BF87 /* tcp.py in Resources */,
282 | A22186371A649ED800E4BF87 /* Main.storyboard in Resources */,
283 | A22185FF1A649E3D00E4BF87 /* Images.xcassets in Resources */,
284 | A22186291A649E4A00E4BF87 /* Documentation.html in Resources */,
285 | A22186351A649E8A00E4BF87 /* udp.py in Resources */,
286 | );
287 | runOnlyForDeploymentPostprocessing = 0;
288 | };
289 | A22186051A649E3D00E4BF87 /* Resources */ = {
290 | isa = PBXResourcesBuildPhase;
291 | buildActionMask = 2147483647;
292 | files = (
293 | );
294 | runOnlyForDeploymentPostprocessing = 0;
295 | };
296 | /* End PBXResourcesBuildPhase section */
297 |
298 | /* Begin PBXSourcesBuildPhase section */
299 | A22185F01A649E3D00E4BF87 /* Sources */ = {
300 | isa = PBXSourcesBuildPhase;
301 | buildActionMask = 2147483647;
302 | files = (
303 | A22186261A649E4A00E4BF87 /* GCDAsyncUdpSocket.m in Sources */,
304 | A22186271A649E4A00E4BF87 /* AsyncSocket.m in Sources */,
305 | A22186251A649E4A00E4BF87 /* GCDAsyncSocket.m in Sources */,
306 | A221862E1A649E5200E4BF87 /* TCPViewController.m in Sources */,
307 | A22186281A649E4A00E4BF87 /* AsyncUdpSocket.m in Sources */,
308 | A22185FD1A649E3D00E4BF87 /* AppDelegate.m in Sources */,
309 | A221862F1A649E5200E4BF87 /* UDPViewController.m in Sources */,
310 | A22185FA1A649E3D00E4BF87 /* main.m in Sources */,
311 | );
312 | runOnlyForDeploymentPostprocessing = 0;
313 | };
314 | A22186031A649E3D00E4BF87 /* Sources */ = {
315 | isa = PBXSourcesBuildPhase;
316 | buildActionMask = 2147483647;
317 | files = (
318 | A221860E1A649E3D00E4BF87 /* SocketClient_DemoTests.m in Sources */,
319 | );
320 | runOnlyForDeploymentPostprocessing = 0;
321 | };
322 | /* End PBXSourcesBuildPhase section */
323 |
324 | /* Begin PBXTargetDependency section */
325 | A22186091A649E3D00E4BF87 /* PBXTargetDependency */ = {
326 | isa = PBXTargetDependency;
327 | target = A22185F31A649E3D00E4BF87 /* SocketClient-Demo */;
328 | targetProxy = A22186081A649E3D00E4BF87 /* PBXContainerItemProxy */;
329 | };
330 | /* End PBXTargetDependency section */
331 |
332 | /* Begin PBXVariantGroup section */
333 | A22186001A649E3D00E4BF87 /* LaunchScreen.xib */ = {
334 | isa = PBXVariantGroup;
335 | children = (
336 | A22186011A649E3D00E4BF87 /* Base */,
337 | );
338 | name = LaunchScreen.xib;
339 | sourceTree = "";
340 | };
341 | /* End PBXVariantGroup section */
342 |
343 | /* Begin XCBuildConfiguration section */
344 | A221860F1A649E3D00E4BF87 /* Debug */ = {
345 | isa = XCBuildConfiguration;
346 | buildSettings = {
347 | ALWAYS_SEARCH_USER_PATHS = NO;
348 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
349 | CLANG_CXX_LIBRARY = "libc++";
350 | CLANG_ENABLE_MODULES = YES;
351 | CLANG_ENABLE_OBJC_ARC = YES;
352 | CLANG_WARN_BOOL_CONVERSION = YES;
353 | CLANG_WARN_CONSTANT_CONVERSION = YES;
354 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
355 | CLANG_WARN_EMPTY_BODY = YES;
356 | CLANG_WARN_ENUM_CONVERSION = YES;
357 | CLANG_WARN_INT_CONVERSION = YES;
358 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
359 | CLANG_WARN_UNREACHABLE_CODE = YES;
360 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
361 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
362 | COPY_PHASE_STRIP = NO;
363 | ENABLE_STRICT_OBJC_MSGSEND = YES;
364 | GCC_C_LANGUAGE_STANDARD = gnu99;
365 | GCC_DYNAMIC_NO_PIC = NO;
366 | GCC_OPTIMIZATION_LEVEL = 0;
367 | GCC_PREPROCESSOR_DEFINITIONS = (
368 | "DEBUG=1",
369 | "$(inherited)",
370 | );
371 | GCC_SYMBOLS_PRIVATE_EXTERN = NO;
372 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
373 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
374 | GCC_WARN_UNDECLARED_SELECTOR = YES;
375 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
376 | GCC_WARN_UNUSED_FUNCTION = YES;
377 | GCC_WARN_UNUSED_VARIABLE = YES;
378 | IPHONEOS_DEPLOYMENT_TARGET = 8.1;
379 | MTL_ENABLE_DEBUG_INFO = YES;
380 | ONLY_ACTIVE_ARCH = YES;
381 | SDKROOT = iphoneos;
382 | };
383 | name = Debug;
384 | };
385 | A22186101A649E3D00E4BF87 /* Release */ = {
386 | isa = XCBuildConfiguration;
387 | buildSettings = {
388 | ALWAYS_SEARCH_USER_PATHS = NO;
389 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
390 | CLANG_CXX_LIBRARY = "libc++";
391 | CLANG_ENABLE_MODULES = YES;
392 | CLANG_ENABLE_OBJC_ARC = YES;
393 | CLANG_WARN_BOOL_CONVERSION = YES;
394 | CLANG_WARN_CONSTANT_CONVERSION = YES;
395 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
396 | CLANG_WARN_EMPTY_BODY = YES;
397 | CLANG_WARN_ENUM_CONVERSION = YES;
398 | CLANG_WARN_INT_CONVERSION = YES;
399 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
400 | CLANG_WARN_UNREACHABLE_CODE = YES;
401 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
402 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
403 | COPY_PHASE_STRIP = YES;
404 | ENABLE_NS_ASSERTIONS = NO;
405 | ENABLE_STRICT_OBJC_MSGSEND = YES;
406 | GCC_C_LANGUAGE_STANDARD = gnu99;
407 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
408 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
409 | GCC_WARN_UNDECLARED_SELECTOR = YES;
410 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
411 | GCC_WARN_UNUSED_FUNCTION = YES;
412 | GCC_WARN_UNUSED_VARIABLE = YES;
413 | IPHONEOS_DEPLOYMENT_TARGET = 8.1;
414 | MTL_ENABLE_DEBUG_INFO = NO;
415 | SDKROOT = iphoneos;
416 | VALIDATE_PRODUCT = YES;
417 | };
418 | name = Release;
419 | };
420 | A22186121A649E3D00E4BF87 /* Debug */ = {
421 | isa = XCBuildConfiguration;
422 | buildSettings = {
423 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
424 | INFOPLIST_FILE = "SocketClient-Demo/Info.plist";
425 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
426 | PRODUCT_NAME = "$(TARGET_NAME)";
427 | };
428 | name = Debug;
429 | };
430 | A22186131A649E3D00E4BF87 /* Release */ = {
431 | isa = XCBuildConfiguration;
432 | buildSettings = {
433 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
434 | INFOPLIST_FILE = "SocketClient-Demo/Info.plist";
435 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
436 | PRODUCT_NAME = "$(TARGET_NAME)";
437 | };
438 | name = Release;
439 | };
440 | A22186151A649E3D00E4BF87 /* Debug */ = {
441 | isa = XCBuildConfiguration;
442 | buildSettings = {
443 | BUNDLE_LOADER = "$(TEST_HOST)";
444 | FRAMEWORK_SEARCH_PATHS = (
445 | "$(SDKROOT)/Developer/Library/Frameworks",
446 | "$(inherited)",
447 | );
448 | GCC_PREPROCESSOR_DEFINITIONS = (
449 | "DEBUG=1",
450 | "$(inherited)",
451 | );
452 | INFOPLIST_FILE = "SocketClient-DemoTests/Info.plist";
453 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
454 | PRODUCT_NAME = "$(TARGET_NAME)";
455 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/SocketClient-Demo.app/SocketClient-Demo";
456 | };
457 | name = Debug;
458 | };
459 | A22186161A649E3D00E4BF87 /* Release */ = {
460 | isa = XCBuildConfiguration;
461 | buildSettings = {
462 | BUNDLE_LOADER = "$(TEST_HOST)";
463 | FRAMEWORK_SEARCH_PATHS = (
464 | "$(SDKROOT)/Developer/Library/Frameworks",
465 | "$(inherited)",
466 | );
467 | INFOPLIST_FILE = "SocketClient-DemoTests/Info.plist";
468 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
469 | PRODUCT_NAME = "$(TARGET_NAME)";
470 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/SocketClient-Demo.app/SocketClient-Demo";
471 | };
472 | name = Release;
473 | };
474 | /* End XCBuildConfiguration section */
475 |
476 | /* Begin XCConfigurationList section */
477 | A22185EF1A649E3D00E4BF87 /* Build configuration list for PBXProject "SocketClient-Demo" */ = {
478 | isa = XCConfigurationList;
479 | buildConfigurations = (
480 | A221860F1A649E3D00E4BF87 /* Debug */,
481 | A22186101A649E3D00E4BF87 /* Release */,
482 | );
483 | defaultConfigurationIsVisible = 0;
484 | defaultConfigurationName = Release;
485 | };
486 | A22186111A649E3D00E4BF87 /* Build configuration list for PBXNativeTarget "SocketClient-Demo" */ = {
487 | isa = XCConfigurationList;
488 | buildConfigurations = (
489 | A22186121A649E3D00E4BF87 /* Debug */,
490 | A22186131A649E3D00E4BF87 /* Release */,
491 | );
492 | defaultConfigurationIsVisible = 0;
493 | };
494 | A22186141A649E3D00E4BF87 /* Build configuration list for PBXNativeTarget "SocketClient-DemoTests" */ = {
495 | isa = XCConfigurationList;
496 | buildConfigurations = (
497 | A22186151A649E3D00E4BF87 /* Debug */,
498 | A22186161A649E3D00E4BF87 /* Release */,
499 | );
500 | defaultConfigurationIsVisible = 0;
501 | };
502 | /* End XCConfigurationList section */
503 | };
504 | rootObject = A22185EC1A649E3D00E4BF87 /* Project object */;
505 | }
506 |
--------------------------------------------------------------------------------
/SocketClient-Demo/AsyncSocket/RunLoop/AsyncSocket.h:
--------------------------------------------------------------------------------
1 | //
2 | // AsyncSocket.h
3 | //
4 | // This class is in the public domain.
5 | // Originally created by Dustin Voss on Wed Jan 29 2003.
6 | // Updated and maintained by Deusty Designs and the Mac development community.
7 | //
8 | // http://code.google.com/p/cocoaasyncsocket/
9 | //
10 |
11 | #import
12 |
13 | @class AsyncSocket;
14 | @class AsyncReadPacket;
15 | @class AsyncWritePacket;
16 |
17 | extern NSString *const AsyncSocketException;
18 | extern NSString *const AsyncSocketErrorDomain;
19 |
20 | enum AsyncSocketError
21 | {
22 | AsyncSocketCFSocketError = kCFSocketError, // From CFSocketError enum.
23 | AsyncSocketNoError = 0, // Never used.
24 | AsyncSocketCanceledError, // onSocketWillConnect: returned NO.
25 | AsyncSocketConnectTimeoutError,
26 | AsyncSocketReadMaxedOutError, // Reached set maxLength without completing
27 | AsyncSocketReadTimeoutError,
28 | AsyncSocketWriteTimeoutError
29 | };
30 | typedef enum AsyncSocketError AsyncSocketError;
31 |
32 | @protocol AsyncSocketDelegate
33 | @optional
34 |
35 | /**
36 | * In the event of an error, the socket is closed.
37 | * You may call "unreadData" during this call-back to get the last bit of data off the socket.
38 | * When connecting, this delegate method may be called
39 | * before"onSocket:didAcceptNewSocket:" or "onSocket:didConnectToHost:".
40 | **/
41 | - (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err;
42 |
43 | /**
44 | * Called when a socket disconnects with or without error. If you want to release a socket after it disconnects,
45 | * do so here. It is not safe to do that during "onSocket:willDisconnectWithError:".
46 | *
47 | * If you call the disconnect method, and the socket wasn't already disconnected,
48 | * this delegate method will be called before the disconnect method returns.
49 | **/
50 | - (void)onSocketDidDisconnect:(AsyncSocket *)sock;
51 |
52 | /**
53 | * Called when a socket accepts a connection. Another socket is spawned to handle it. The new socket will have
54 | * the same delegate and will call "onSocket:didConnectToHost:port:".
55 | **/
56 | - (void)onSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket;
57 |
58 | /**
59 | * Called when a new socket is spawned to handle a connection. This method should return the run-loop of the
60 | * thread on which the new socket and its delegate should operate. If omitted, [NSRunLoop currentRunLoop] is used.
61 | **/
62 | - (NSRunLoop *)onSocket:(AsyncSocket *)sock wantsRunLoopForNewSocket:(AsyncSocket *)newSocket;
63 |
64 | /**
65 | * Called when a socket is about to connect. This method should return YES to continue, or NO to abort.
66 | * If aborted, will result in AsyncSocketCanceledError.
67 | *
68 | * If the connectToHost:onPort:error: method was called, the delegate will be able to access and configure the
69 | * CFReadStream and CFWriteStream as desired prior to connection.
70 | *
71 | * If the connectToAddress:error: method was called, the delegate will be able to access and configure the
72 | * CFSocket and CFSocketNativeHandle (BSD socket) as desired prior to connection. You will be able to access and
73 | * configure the CFReadStream and CFWriteStream in the onSocket:didConnectToHost:port: method.
74 | **/
75 | - (BOOL)onSocketWillConnect:(AsyncSocket *)sock;
76 |
77 | /**
78 | * Called when a socket connects and is ready for reading and writing.
79 | * The host parameter will be an IP address, not a DNS name.
80 | **/
81 | - (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port;
82 |
83 | /**
84 | * Called when a socket has completed reading the requested data into memory.
85 | * Not called if there is an error.
86 | **/
87 | - (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag;
88 |
89 | /**
90 | * Called when a socket has read in data, but has not yet completed the read.
91 | * This would occur if using readToData: or readToLength: methods.
92 | * It may be used to for things such as updating progress bars.
93 | **/
94 | - (void)onSocket:(AsyncSocket *)sock didReadPartialDataOfLength:(NSUInteger)partialLength tag:(long)tag;
95 |
96 | /**
97 | * Called when a socket has completed writing the requested data. Not called if there is an error.
98 | **/
99 | - (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag;
100 |
101 | /**
102 | * Called when a socket has written some data, but has not yet completed the entire write.
103 | * It may be used to for things such as updating progress bars.
104 | **/
105 | - (void)onSocket:(AsyncSocket *)sock didWritePartialDataOfLength:(NSUInteger)partialLength tag:(long)tag;
106 |
107 | /**
108 | * Called if a read operation has reached its timeout without completing.
109 | * This method allows you to optionally extend the timeout.
110 | * If you return a positive time interval (> 0) the read's timeout will be extended by the given amount.
111 | * If you don't implement this method, or return a non-positive time interval (<= 0) the read will timeout as usual.
112 | *
113 | * The elapsed parameter is the sum of the original timeout, plus any additions previously added via this method.
114 | * The length parameter is the number of bytes that have been read so far for the read operation.
115 | *
116 | * Note that this method may be called multiple times for a single read if you return positive numbers.
117 | **/
118 | - (NSTimeInterval)onSocket:(AsyncSocket *)sock
119 | shouldTimeoutReadWithTag:(long)tag
120 | elapsed:(NSTimeInterval)elapsed
121 | bytesDone:(NSUInteger)length;
122 |
123 | /**
124 | * Called if a write operation has reached its timeout without completing.
125 | * This method allows you to optionally extend the timeout.
126 | * If you return a positive time interval (> 0) the write's timeout will be extended by the given amount.
127 | * If you don't implement this method, or return a non-positive time interval (<= 0) the write will timeout as usual.
128 | *
129 | * The elapsed parameter is the sum of the original timeout, plus any additions previously added via this method.
130 | * The length parameter is the number of bytes that have been written so far for the write operation.
131 | *
132 | * Note that this method may be called multiple times for a single write if you return positive numbers.
133 | **/
134 | - (NSTimeInterval)onSocket:(AsyncSocket *)sock
135 | shouldTimeoutWriteWithTag:(long)tag
136 | elapsed:(NSTimeInterval)elapsed
137 | bytesDone:(NSUInteger)length;
138 |
139 | /**
140 | * Called after the socket has successfully completed SSL/TLS negotiation.
141 | * This method is not called unless you use the provided startTLS method.
142 | *
143 | * If a SSL/TLS negotiation fails (invalid certificate, etc) then the socket will immediately close,
144 | * and the onSocket:willDisconnectWithError: delegate method will be called with the specific SSL error code.
145 | **/
146 | - (void)onSocketDidSecure:(AsyncSocket *)sock;
147 |
148 | @end
149 |
150 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
151 | #pragma mark -
152 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
153 |
154 | @interface AsyncSocket : NSObject
155 | {
156 | CFSocketNativeHandle theNativeSocket4;
157 | CFSocketNativeHandle theNativeSocket6;
158 |
159 | CFSocketRef theSocket4; // IPv4 accept or connect socket
160 | CFSocketRef theSocket6; // IPv6 accept or connect socket
161 |
162 | CFReadStreamRef theReadStream;
163 | CFWriteStreamRef theWriteStream;
164 |
165 | CFRunLoopSourceRef theSource4; // For theSocket4
166 | CFRunLoopSourceRef theSource6; // For theSocket6
167 | CFRunLoopRef theRunLoop;
168 | CFSocketContext theContext;
169 | NSArray *theRunLoopModes;
170 |
171 | NSTimer *theConnectTimer;
172 |
173 | NSMutableArray *theReadQueue;
174 | AsyncReadPacket *theCurrentRead;
175 | NSTimer *theReadTimer;
176 | NSMutableData *partialReadBuffer;
177 |
178 | NSMutableArray *theWriteQueue;
179 | AsyncWritePacket *theCurrentWrite;
180 | NSTimer *theWriteTimer;
181 |
182 | id theDelegate;
183 | UInt16 theFlags;
184 |
185 | long theUserData;
186 | }
187 |
188 | - (id)init;
189 | - (id)initWithDelegate:(id)delegate;
190 | - (id)initWithDelegate:(id)delegate userData:(long)userData;
191 |
192 | /* String representation is long but has no "\n". */
193 | - (NSString *)description;
194 |
195 | /**
196 | * Use "canSafelySetDelegate" to see if there is any pending business (reads and writes) with the current delegate
197 | * before changing it. It is, of course, safe to change the delegate before connecting or accepting connections.
198 | **/
199 | - (id)delegate;
200 | - (BOOL)canSafelySetDelegate;
201 | - (void)setDelegate:(id)delegate;
202 |
203 | /* User data can be a long, or an id or void * cast to a long. */
204 | - (long)userData;
205 | - (void)setUserData:(long)userData;
206 |
207 | /* Don't use these to read or write. And don't close them either! */
208 | - (CFSocketRef)getCFSocket;
209 | - (CFReadStreamRef)getCFReadStream;
210 | - (CFWriteStreamRef)getCFWriteStream;
211 |
212 | // Once one of the accept or connect methods are called, the AsyncSocket instance is locked in
213 | // and the other accept/connect methods can't be called without disconnecting the socket first.
214 | // If the attempt fails or times out, these methods either return NO or
215 | // call "onSocket:willDisconnectWithError:" and "onSockedDidDisconnect:".
216 |
217 | // When an incoming connection is accepted, AsyncSocket invokes several delegate methods.
218 | // These methods are (in chronological order):
219 | // 1. onSocket:didAcceptNewSocket:
220 | // 2. onSocket:wantsRunLoopForNewSocket:
221 | // 3. onSocketWillConnect:
222 | //
223 | // Your server code will need to retain the accepted socket (if you want to accept it).
224 | // The best place to do this is probably in the onSocket:didAcceptNewSocket: method.
225 | //
226 | // After the read and write streams have been setup for the newly accepted socket,
227 | // the onSocket:didConnectToHost:port: method will be called on the proper run loop.
228 | //
229 | // Multithreading Note: If you're going to be moving the newly accepted socket to another run
230 | // loop by implementing onSocket:wantsRunLoopForNewSocket:, then you should wait until the
231 | // onSocket:didConnectToHost:port: method before calling read, write, or startTLS methods.
232 | // Otherwise read/write events are scheduled on the incorrect runloop, and chaos may ensue.
233 |
234 | /**
235 | * Tells the socket to begin listening and accepting connections on the given port.
236 | * When a connection comes in, the AsyncSocket instance will call the various delegate methods (see above).
237 | * The socket will listen on all available interfaces (e.g. wifi, ethernet, etc)
238 | **/
239 | - (BOOL)acceptOnPort:(UInt16)port error:(NSError **)errPtr;
240 |
241 | /**
242 | * This method is the same as acceptOnPort:error: with the additional option
243 | * of specifying which interface to listen on. So, for example, if you were writing code for a server that
244 | * has multiple IP addresses, you could specify which address you wanted to listen on. Or you could use it
245 | * to specify that the socket should only accept connections over ethernet, and not other interfaces such as wifi.
246 | * You may also use the special strings "localhost" or "loopback" to specify that
247 | * the socket only accept connections from the local machine.
248 | *
249 | * To accept connections on any interface pass nil, or simply use the acceptOnPort:error: method.
250 | **/
251 | - (BOOL)acceptOnInterface:(NSString *)interface port:(UInt16)port error:(NSError **)errPtr;
252 |
253 | /**
254 | * Connects to the given host and port.
255 | * The host may be a domain name (e.g. "deusty.com") or an IP address string (e.g. "192.168.0.2")
256 | **/
257 | - (BOOL)connectToHost:(NSString *)hostname onPort:(UInt16)port error:(NSError **)errPtr;
258 |
259 | /**
260 | * This method is the same as connectToHost:onPort:error: with an additional timeout option.
261 | * To not time out use a negative time interval, or simply use the connectToHost:onPort:error: method.
262 | **/
263 | - (BOOL)connectToHost:(NSString *)hostname
264 | onPort:(UInt16)port
265 | withTimeout:(NSTimeInterval)timeout
266 | error:(NSError **)errPtr;
267 |
268 | /**
269 | * Connects to the given address, specified as a sockaddr structure wrapped in a NSData object.
270 | * For example, a NSData object returned from NSNetService's addresses method.
271 | *
272 | * If you have an existing struct sockaddr you can convert it to a NSData object like so:
273 | * struct sockaddr sa -> NSData *dsa = [NSData dataWithBytes:&remoteAddr length:remoteAddr.sa_len];
274 | * struct sockaddr *sa -> NSData *dsa = [NSData dataWithBytes:remoteAddr length:remoteAddr->sa_len];
275 | **/
276 | - (BOOL)connectToAddress:(NSData *)remoteAddr error:(NSError **)errPtr;
277 |
278 | /**
279 | * This method is the same as connectToAddress:error: with an additional timeout option.
280 | * To not time out use a negative time interval, or simply use the connectToAddress:error: method.
281 | **/
282 | - (BOOL)connectToAddress:(NSData *)remoteAddr withTimeout:(NSTimeInterval)timeout error:(NSError **)errPtr;
283 |
284 | - (BOOL)connectToAddress:(NSData *)remoteAddr
285 | viaInterfaceAddress:(NSData *)interfaceAddr
286 | withTimeout:(NSTimeInterval)timeout
287 | error:(NSError **)errPtr;
288 |
289 | /**
290 | * Disconnects immediately. Any pending reads or writes are dropped.
291 | * If the socket is not already disconnected, the onSocketDidDisconnect delegate method
292 | * will be called immediately, before this method returns.
293 | *
294 | * Please note the recommended way of releasing an AsyncSocket instance (e.g. in a dealloc method)
295 | * [asyncSocket setDelegate:nil];
296 | * [asyncSocket disconnect];
297 | * [asyncSocket release];
298 | **/
299 | - (void)disconnect;
300 |
301 | /**
302 | * Disconnects after all pending reads have completed.
303 | * After calling this, the read and write methods will do nothing.
304 | * The socket will disconnect even if there are still pending writes.
305 | **/
306 | - (void)disconnectAfterReading;
307 |
308 | /**
309 | * Disconnects after all pending writes have completed.
310 | * After calling this, the read and write methods will do nothing.
311 | * The socket will disconnect even if there are still pending reads.
312 | **/
313 | - (void)disconnectAfterWriting;
314 |
315 | /**
316 | * Disconnects after all pending reads and writes have completed.
317 | * After calling this, the read and write methods will do nothing.
318 | **/
319 | - (void)disconnectAfterReadingAndWriting;
320 |
321 | /* Returns YES if the socket and streams are open, connected, and ready for reading and writing. */
322 | - (BOOL)isConnected;
323 |
324 | /**
325 | * Returns the local or remote host and port to which this socket is connected, or nil and 0 if not connected.
326 | * The host will be an IP address.
327 | **/
328 | - (NSString *)connectedHost;
329 | - (UInt16)connectedPort;
330 |
331 | - (NSString *)localHost;
332 | - (UInt16)localPort;
333 |
334 | /**
335 | * Returns the local or remote address to which this socket is connected,
336 | * specified as a sockaddr structure wrapped in a NSData object.
337 | *
338 | * See also the connectedHost, connectedPort, localHost and localPort methods.
339 | **/
340 | - (NSData *)connectedAddress;
341 | - (NSData *)localAddress;
342 |
343 | /**
344 | * Returns whether the socket is IPv4 or IPv6.
345 | * An accepting socket may be both.
346 | **/
347 | - (BOOL)isIPv4;
348 | - (BOOL)isIPv6;
349 |
350 | // The readData and writeData methods won't block (they are asynchronous).
351 | //
352 | // When a read is complete the onSocket:didReadData:withTag: delegate method is called.
353 | // When a write is complete the onSocket:didWriteDataWithTag: delegate method is called.
354 | //
355 | // You may optionally set a timeout for any read/write operation. (To not timeout, use a negative time interval.)
356 | // If a read/write opertion times out, the corresponding "onSocket:shouldTimeout..." delegate method
357 | // is called to optionally allow you to extend the timeout.
358 | // Upon a timeout, the "onSocket:willDisconnectWithError:" method is called, followed by "onSocketDidDisconnect".
359 | //
360 | // The tag is for your convenience.
361 | // You can use it as an array index, step number, state id, pointer, etc.
362 |
363 | /**
364 | * Reads the first available bytes that become available on the socket.
365 | *
366 | * If the timeout value is negative, the read operation will not use a timeout.
367 | **/
368 | - (void)readDataWithTimeout:(NSTimeInterval)timeout tag:(long)tag;
369 |
370 | /**
371 | * Reads the first available bytes that become available on the socket.
372 | * The bytes will be appended to the given byte buffer starting at the given offset.
373 | * The given buffer will automatically be increased in size if needed.
374 | *
375 | * If the timeout value is negative, the read operation will not use a timeout.
376 | * If the buffer if nil, the socket will create a buffer for you.
377 | *
378 | * If the bufferOffset is greater than the length of the given buffer,
379 | * the method will do nothing, and the delegate will not be called.
380 | *
381 | * If you pass a buffer, you must not alter it in any way while AsyncSocket is using it.
382 | * After completion, the data returned in onSocket:didReadData:withTag: will be a subset of the given buffer.
383 | * That is, it will reference the bytes that were appended to the given buffer.
384 | **/
385 | - (void)readDataWithTimeout:(NSTimeInterval)timeout
386 | buffer:(NSMutableData *)buffer
387 | bufferOffset:(NSUInteger)offset
388 | tag:(long)tag;
389 |
390 | /**
391 | * Reads the first available bytes that become available on the socket.
392 | * The bytes will be appended to the given byte buffer starting at the given offset.
393 | * The given buffer will automatically be increased in size if needed.
394 | * A maximum of length bytes will be read.
395 | *
396 | * If the timeout value is negative, the read operation will not use a timeout.
397 | * If the buffer if nil, a buffer will automatically be created for you.
398 | * If maxLength is zero, no length restriction is enforced.
399 | *
400 | * If the bufferOffset is greater than the length of the given buffer,
401 | * the method will do nothing, and the delegate will not be called.
402 | *
403 | * If you pass a buffer, you must not alter it in any way while AsyncSocket is using it.
404 | * After completion, the data returned in onSocket:didReadData:withTag: will be a subset of the given buffer.
405 | * That is, it will reference the bytes that were appended to the given buffer.
406 | **/
407 | - (void)readDataWithTimeout:(NSTimeInterval)timeout
408 | buffer:(NSMutableData *)buffer
409 | bufferOffset:(NSUInteger)offset
410 | maxLength:(NSUInteger)length
411 | tag:(long)tag;
412 |
413 | /**
414 | * Reads the given number of bytes.
415 | *
416 | * If the timeout value is negative, the read operation will not use a timeout.
417 | *
418 | * If the length is 0, this method does nothing and the delegate is not called.
419 | **/
420 | - (void)readDataToLength:(NSUInteger)length withTimeout:(NSTimeInterval)timeout tag:(long)tag;
421 |
422 | /**
423 | * Reads the given number of bytes.
424 | * The bytes will be appended to the given byte buffer starting at the given offset.
425 | * The given buffer will automatically be increased in size if needed.
426 | *
427 | * If the timeout value is negative, the read operation will not use a timeout.
428 | * If the buffer if nil, a buffer will automatically be created for you.
429 | *
430 | * If the length is 0, this method does nothing and the delegate is not called.
431 | * If the bufferOffset is greater than the length of the given buffer,
432 | * the method will do nothing, and the delegate will not be called.
433 | *
434 | * If you pass a buffer, you must not alter it in any way while AsyncSocket is using it.
435 | * After completion, the data returned in onSocket:didReadData:withTag: will be a subset of the given buffer.
436 | * That is, it will reference the bytes that were appended to the given buffer.
437 | **/
438 | - (void)readDataToLength:(NSUInteger)length
439 | withTimeout:(NSTimeInterval)timeout
440 | buffer:(NSMutableData *)buffer
441 | bufferOffset:(NSUInteger)offset
442 | tag:(long)tag;
443 |
444 | /**
445 | * Reads bytes until (and including) the passed "data" parameter, which acts as a separator.
446 | *
447 | * If the timeout value is negative, the read operation will not use a timeout.
448 | *
449 | * If you pass nil or zero-length data as the "data" parameter,
450 | * the method will do nothing, and the delegate will not be called.
451 | *
452 | * To read a line from the socket, use the line separator (e.g. CRLF for HTTP, see below) as the "data" parameter.
453 | * Note that this method is not character-set aware, so if a separator can occur naturally as part of the encoding for
454 | * a character, the read will prematurely end.
455 | **/
456 | - (void)readDataToData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;
457 |
458 | /**
459 | * Reads bytes until (and including) the passed "data" parameter, which acts as a separator.
460 | * The bytes will be appended to the given byte buffer starting at the given offset.
461 | * The given buffer will automatically be increased in size if needed.
462 | *
463 | * If the timeout value is negative, the read operation will not use a timeout.
464 | * If the buffer if nil, a buffer will automatically be created for you.
465 | *
466 | * If the bufferOffset is greater than the length of the given buffer,
467 | * the method will do nothing, and the delegate will not be called.
468 | *
469 | * If you pass a buffer, you must not alter it in any way while AsyncSocket is using it.
470 | * After completion, the data returned in onSocket:didReadData:withTag: will be a subset of the given buffer.
471 | * That is, it will reference the bytes that were appended to the given buffer.
472 | *
473 | * To read a line from the socket, use the line separator (e.g. CRLF for HTTP, see below) as the "data" parameter.
474 | * Note that this method is not character-set aware, so if a separator can occur naturally as part of the encoding for
475 | * a character, the read will prematurely end.
476 | **/
477 | - (void)readDataToData:(NSData *)data
478 | withTimeout:(NSTimeInterval)timeout
479 | buffer:(NSMutableData *)buffer
480 | bufferOffset:(NSUInteger)offset
481 | tag:(long)tag;
482 |
483 | /**
484 | * Reads bytes until (and including) the passed "data" parameter, which acts as a separator.
485 | *
486 | * If the timeout value is negative, the read operation will not use a timeout.
487 | *
488 | * If maxLength is zero, no length restriction is enforced.
489 | * Otherwise if maxLength bytes are read without completing the read,
490 | * it is treated similarly to a timeout - the socket is closed with a AsyncSocketReadMaxedOutError.
491 | * The read will complete successfully if exactly maxLength bytes are read and the given data is found at the end.
492 | *
493 | * If you pass nil or zero-length data as the "data" parameter,
494 | * the method will do nothing, and the delegate will not be called.
495 | * If you pass a maxLength parameter that is less than the length of the data parameter,
496 | * the method will do nothing, and the delegate will not be called.
497 | *
498 | * To read a line from the socket, use the line separator (e.g. CRLF for HTTP, see below) as the "data" parameter.
499 | * Note that this method is not character-set aware, so if a separator can occur naturally as part of the encoding for
500 | * a character, the read will prematurely end.
501 | **/
502 | - (void)readDataToData:(NSData *)data withTimeout:(NSTimeInterval)timeout maxLength:(NSUInteger)length tag:(long)tag;
503 |
504 | /**
505 | * Reads bytes until (and including) the passed "data" parameter, which acts as a separator.
506 | * The bytes will be appended to the given byte buffer starting at the given offset.
507 | * The given buffer will automatically be increased in size if needed.
508 | * A maximum of length bytes will be read.
509 | *
510 | * If the timeout value is negative, the read operation will not use a timeout.
511 | * If the buffer if nil, a buffer will automatically be created for you.
512 | *
513 | * If maxLength is zero, no length restriction is enforced.
514 | * Otherwise if maxLength bytes are read without completing the read,
515 | * it is treated similarly to a timeout - the socket is closed with a AsyncSocketReadMaxedOutError.
516 | * The read will complete successfully if exactly maxLength bytes are read and the given data is found at the end.
517 | *
518 | * If you pass a maxLength parameter that is less than the length of the data parameter,
519 | * the method will do nothing, and the delegate will not be called.
520 | * If the bufferOffset is greater than the length of the given buffer,
521 | * the method will do nothing, and the delegate will not be called.
522 | *
523 | * If you pass a buffer, you must not alter it in any way while AsyncSocket is using it.
524 | * After completion, the data returned in onSocket:didReadData:withTag: will be a subset of the given buffer.
525 | * That is, it will reference the bytes that were appended to the given buffer.
526 | *
527 | * To read a line from the socket, use the line separator (e.g. CRLF for HTTP, see below) as the "data" parameter.
528 | * Note that this method is not character-set aware, so if a separator can occur naturally as part of the encoding for
529 | * a character, the read will prematurely end.
530 | **/
531 | - (void)readDataToData:(NSData *)data
532 | withTimeout:(NSTimeInterval)timeout
533 | buffer:(NSMutableData *)buffer
534 | bufferOffset:(NSUInteger)offset
535 | maxLength:(NSUInteger)length
536 | tag:(long)tag;
537 |
538 | /**
539 | * Writes data to the socket, and calls the delegate when finished.
540 | *
541 | * If you pass in nil or zero-length data, this method does nothing and the delegate will not be called.
542 | * If the timeout value is negative, the write operation will not use a timeout.
543 | **/
544 | - (void)writeData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;
545 |
546 | /**
547 | * Returns progress of current read or write, from 0.0 to 1.0, or NaN if no read/write (use isnan() to check).
548 | * "tag", "done" and "total" will be filled in if they aren't NULL.
549 | **/
550 | - (float)progressOfReadReturningTag:(long *)tag bytesDone:(NSUInteger *)done total:(NSUInteger *)total;
551 | - (float)progressOfWriteReturningTag:(long *)tag bytesDone:(NSUInteger *)done total:(NSUInteger *)total;
552 |
553 | /**
554 | * Secures the connection using SSL/TLS.
555 | *
556 | * This method may be called at any time, and the TLS handshake will occur after all pending reads and writes
557 | * are finished. This allows one the option of sending a protocol dependent StartTLS message, and queuing
558 | * the upgrade to TLS at the same time, without having to wait for the write to finish.
559 | * Any reads or writes scheduled after this method is called will occur over the secured connection.
560 | *
561 | * The possible keys and values for the TLS settings are well documented.
562 | * Some possible keys are:
563 | * - kCFStreamSSLLevel
564 | * - kCFStreamSSLAllowsExpiredCertificates
565 | * - kCFStreamSSLAllowsExpiredRoots
566 | * - kCFStreamSSLAllowsAnyRoot
567 | * - kCFStreamSSLValidatesCertificateChain
568 | * - kCFStreamSSLPeerName
569 | * - kCFStreamSSLCertificates
570 | * - kCFStreamSSLIsServer
571 | *
572 | * Please refer to Apple's documentation for associated values, as well as other possible keys.
573 | *
574 | * If you pass in nil or an empty dictionary, the default settings will be used.
575 | *
576 | * The default settings will check to make sure the remote party's certificate is signed by a
577 | * trusted 3rd party certificate agency (e.g. verisign) and that the certificate is not expired.
578 | * However it will not verify the name on the certificate unless you
579 | * give it a name to verify against via the kCFStreamSSLPeerName key.
580 | * The security implications of this are important to understand.
581 | * Imagine you are attempting to create a secure connection to MySecureServer.com,
582 | * but your socket gets directed to MaliciousServer.com because of a hacked DNS server.
583 | * If you simply use the default settings, and MaliciousServer.com has a valid certificate,
584 | * the default settings will not detect any problems since the certificate is valid.
585 | * To properly secure your connection in this particular scenario you
586 | * should set the kCFStreamSSLPeerName property to "MySecureServer.com".
587 | * If you do not know the peer name of the remote host in advance (for example, you're not sure
588 | * if it will be "domain.com" or "www.domain.com"), then you can use the default settings to validate the
589 | * certificate, and then use the X509Certificate class to verify the issuer after the socket has been secured.
590 | * The X509Certificate class is part of the CocoaAsyncSocket open source project.
591 | **/
592 | - (void)startTLS:(NSDictionary *)tlsSettings;
593 |
594 | /**
595 | * For handling readDataToData requests, data is necessarily read from the socket in small increments.
596 | * The performance can be much improved by allowing AsyncSocket to read larger chunks at a time and
597 | * store any overflow in a small internal buffer.
598 | * This is termed pre-buffering, as some data may be read for you before you ask for it.
599 | * If you use readDataToData a lot, enabling pre-buffering will result in better performance, especially on the iPhone.
600 | *
601 | * The default pre-buffering state is controlled by the DEFAULT_PREBUFFERING definition.
602 | * It is highly recommended one leave this set to YES.
603 | *
604 | * This method exists in case pre-buffering needs to be disabled by default for some unforeseen reason.
605 | * In that case, this method exists to allow one to easily enable pre-buffering when ready.
606 | **/
607 | - (void)enablePreBuffering;
608 |
609 | /**
610 | * When you create an AsyncSocket, it is added to the runloop of the current thread.
611 | * So for manually created sockets, it is easiest to simply create the socket on the thread you intend to use it.
612 | *
613 | * If a new socket is accepted, the delegate method onSocket:wantsRunLoopForNewSocket: is called to
614 | * allow you to place the socket on a separate thread. This works best in conjunction with a thread pool design.
615 | *
616 | * If, however, you need to move the socket to a separate thread at a later time, this
617 | * method may be used to accomplish the task.
618 | *
619 | * This method must be called from the thread/runloop the socket is currently running on.
620 | *
621 | * Note: After calling this method, all further method calls to this object should be done from the given runloop.
622 | * Also, all delegate calls will be sent on the given runloop.
623 | **/
624 | - (BOOL)moveToRunLoop:(NSRunLoop *)runLoop;
625 |
626 | /**
627 | * Allows you to configure which run loop modes the socket uses.
628 | * The default set of run loop modes is NSDefaultRunLoopMode.
629 | *
630 | * If you'd like your socket to continue operation during other modes, you may want to add modes such as
631 | * NSModalPanelRunLoopMode or NSEventTrackingRunLoopMode. Or you may simply want to use NSRunLoopCommonModes.
632 | *
633 | * Accepted sockets will automatically inherit the same run loop modes as the listening socket.
634 | *
635 | * Note: NSRunLoopCommonModes is defined in 10.5. For previous versions one can use kCFRunLoopCommonModes.
636 | **/
637 | - (BOOL)setRunLoopModes:(NSArray *)runLoopModes;
638 | - (BOOL)addRunLoopMode:(NSString *)runLoopMode;
639 | - (BOOL)removeRunLoopMode:(NSString *)runLoopMode;
640 |
641 | /**
642 | * Returns the current run loop modes the AsyncSocket instance is operating in.
643 | * The default set of run loop modes is NSDefaultRunLoopMode.
644 | **/
645 | - (NSArray *)runLoopModes;
646 |
647 | /**
648 | * In the event of an error, this method may be called during onSocket:willDisconnectWithError: to read
649 | * any data that's left on the socket.
650 | **/
651 | - (NSData *)unreadData;
652 |
653 | /* A few common line separators, for use with the readDataToData:... methods. */
654 | + (NSData *)CRLFData; // 0x0D0A
655 | + (NSData *)CRData; // 0x0D
656 | + (NSData *)LFData; // 0x0A
657 | + (NSData *)ZeroData; // 0x00
658 |
659 | @end
660 |
--------------------------------------------------------------------------------
/SocketClient-Demo/AsyncSocket/GCD/GCDAsyncUdpSocket.h:
--------------------------------------------------------------------------------
1 | //
2 | // GCDAsyncUdpSocket
3 | //
4 | // This class is in the public domain.
5 | // Originally created by Robbie Hanson of Deusty LLC.
6 | // Updated and maintained by Deusty LLC and the Apple development community.
7 | //
8 | // https://github.com/robbiehanson/CocoaAsyncSocket
9 | //
10 |
11 | #import
12 | #import
13 | #import
14 | #import
15 |
16 | extern NSString *const GCDAsyncUdpSocketException;
17 | extern NSString *const GCDAsyncUdpSocketErrorDomain;
18 |
19 | extern NSString *const GCDAsyncUdpSocketQueueName;
20 | extern NSString *const GCDAsyncUdpSocketThreadName;
21 |
22 | enum GCDAsyncUdpSocketError
23 | {
24 | GCDAsyncUdpSocketNoError = 0, // Never used
25 | GCDAsyncUdpSocketBadConfigError, // Invalid configuration
26 | GCDAsyncUdpSocketBadParamError, // Invalid parameter was passed
27 | GCDAsyncUdpSocketSendTimeoutError, // A send operation timed out
28 | GCDAsyncUdpSocketClosedError, // The socket was closed
29 | GCDAsyncUdpSocketOtherError, // Description provided in userInfo
30 | };
31 | typedef enum GCDAsyncUdpSocketError GCDAsyncUdpSocketError;
32 |
33 | /**
34 | * You may optionally set a receive filter for the socket.
35 | * A filter can provide several useful features:
36 | *
37 | * 1. Many times udp packets need to be parsed.
38 | * Since the filter can run in its own independent queue, you can parallelize this parsing quite easily.
39 | * The end result is a parallel socket io, datagram parsing, and packet processing.
40 | *
41 | * 2. Many times udp packets are discarded because they are duplicate/unneeded/unsolicited.
42 | * The filter can prevent such packets from arriving at the delegate.
43 | * And because the filter can run in its own independent queue, this doesn't slow down the delegate.
44 | *
45 | * - Since the udp protocol does not guarantee delivery, udp packets may be lost.
46 | * Many protocols built atop udp thus provide various resend/re-request algorithms.
47 | * This sometimes results in duplicate packets arriving.
48 | * A filter may allow you to architect the duplicate detection code to run in parallel to normal processing.
49 | *
50 | * - Since the udp socket may be connectionless, its possible for unsolicited packets to arrive.
51 | * Such packets need to be ignored.
52 | *
53 | * 3. Sometimes traffic shapers are needed to simulate real world environments.
54 | * A filter allows you to write custom code to simulate such environments.
55 | * The ability to code this yourself is especially helpful when your simulated environment
56 | * is more complicated than simple traffic shaping (e.g. simulating a cone port restricted router),
57 | * or the system tools to handle this aren't available (e.g. on a mobile device).
58 | *
59 | * @param data - The packet that was received.
60 | * @param address - The address the data was received from.
61 | * See utilities section for methods to extract info from address.
62 | * @param context - Out parameter you may optionally set, which will then be passed to the delegate method.
63 | * For example, filter block can parse the data and then,
64 | * pass the parsed data to the delegate.
65 | *
66 | * @returns - YES if the received packet should be passed onto the delegate.
67 | * NO if the received packet should be discarded, and not reported to the delegete.
68 | *
69 | * Example:
70 | *
71 | * GCDAsyncUdpSocketReceiveFilterBlock filter = ^BOOL (NSData *data, NSData *address, id *context) {
72 | *
73 | * MyProtocolMessage *msg = [MyProtocol parseMessage:data];
74 | *
75 | * *context = response;
76 | * return (response != nil);
77 | * };
78 | * [udpSocket setReceiveFilter:filter withQueue:myParsingQueue];
79 | *
80 | **/
81 | typedef BOOL (^GCDAsyncUdpSocketReceiveFilterBlock)(NSData *data, NSData *address, id *context);
82 |
83 | /**
84 | * You may optionally set a send filter for the socket.
85 | * A filter can provide several interesting possibilities:
86 | *
87 | * 1. Optional caching of resolved addresses for domain names.
88 | * The cache could later be consulted, resulting in fewer system calls to getaddrinfo.
89 | *
90 | * 2. Reusable modules of code for bandwidth monitoring.
91 | *
92 | * 3. Sometimes traffic shapers are needed to simulate real world environments.
93 | * A filter allows you to write custom code to simulate such environments.
94 | * The ability to code this yourself is especially helpful when your simulated environment
95 | * is more complicated than simple traffic shaping (e.g. simulating a cone port restricted router),
96 | * or the system tools to handle this aren't available (e.g. on a mobile device).
97 | *
98 | * @param data - The packet that was received.
99 | * @param address - The address the data was received from.
100 | * See utilities section for methods to extract info from address.
101 | * @param tag - The tag that was passed in the send method.
102 | *
103 | * @returns - YES if the packet should actually be sent over the socket.
104 | * NO if the packet should be silently dropped (not sent over the socket).
105 | *
106 | * Regardless of the return value, the delegate will be informed that the packet was successfully sent.
107 | *
108 | **/
109 | typedef BOOL (^GCDAsyncUdpSocketSendFilterBlock)(NSData *data, NSData *address, long tag);
110 |
111 |
112 | @interface GCDAsyncUdpSocket : NSObject
113 |
114 | /**
115 | * GCDAsyncUdpSocket uses the standard delegate paradigm,
116 | * but executes all delegate callbacks on a given delegate dispatch queue.
117 | * This allows for maximum concurrency, while at the same time providing easy thread safety.
118 | *
119 | * You MUST set a delegate AND delegate dispatch queue before attempting to
120 | * use the socket, or you will get an error.
121 | *
122 | * The socket queue is optional.
123 | * If you pass NULL, GCDAsyncSocket will automatically create its own socket queue.
124 | * If you choose to provide a socket queue, the socket queue must not be a concurrent queue,
125 | * then please see the discussion for the method markSocketQueueTargetQueue.
126 | *
127 | * The delegate queue and socket queue can optionally be the same.
128 | **/
129 | - (id)init;
130 | - (id)initWithSocketQueue:(dispatch_queue_t)sq;
131 | - (id)initWithDelegate:(id)aDelegate delegateQueue:(dispatch_queue_t)dq;
132 | - (id)initWithDelegate:(id)aDelegate delegateQueue:(dispatch_queue_t)dq socketQueue:(dispatch_queue_t)sq;
133 |
134 | #pragma mark Configuration
135 |
136 | - (id)delegate;
137 | - (void)setDelegate:(id)delegate;
138 | - (void)synchronouslySetDelegate:(id)delegate;
139 |
140 | - (dispatch_queue_t)delegateQueue;
141 | - (void)setDelegateQueue:(dispatch_queue_t)delegateQueue;
142 | - (void)synchronouslySetDelegateQueue:(dispatch_queue_t)delegateQueue;
143 |
144 | - (void)getDelegate:(id *)delegatePtr delegateQueue:(dispatch_queue_t *)delegateQueuePtr;
145 | - (void)setDelegate:(id)delegate delegateQueue:(dispatch_queue_t)delegateQueue;
146 | - (void)synchronouslySetDelegate:(id)delegate delegateQueue:(dispatch_queue_t)delegateQueue;
147 |
148 | /**
149 | * By default, both IPv4 and IPv6 are enabled.
150 | *
151 | * This means GCDAsyncUdpSocket automatically supports both protocols,
152 | * and can send to IPv4 or IPv6 addresses,
153 | * as well as receive over IPv4 and IPv6.
154 | *
155 | * For operations that require DNS resolution, GCDAsyncUdpSocket supports both IPv4 and IPv6.
156 | * If a DNS lookup returns only IPv4 results, GCDAsyncUdpSocket will automatically use IPv4.
157 | * If a DNS lookup returns only IPv6 results, GCDAsyncUdpSocket will automatically use IPv6.
158 | * If a DNS lookup returns both IPv4 and IPv6 results, then the protocol used depends on the configured preference.
159 | * If IPv4 is preferred, then IPv4 is used.
160 | * If IPv6 is preferred, then IPv6 is used.
161 | * If neutral, then the first IP version in the resolved array will be used.
162 | *
163 | * Starting with Mac OS X 10.7 Lion and iOS 5, the default IP preference is neutral.
164 | * On prior systems the default IP preference is IPv4.
165 | **/
166 | - (BOOL)isIPv4Enabled;
167 | - (void)setIPv4Enabled:(BOOL)flag;
168 |
169 | - (BOOL)isIPv6Enabled;
170 | - (void)setIPv6Enabled:(BOOL)flag;
171 |
172 | - (BOOL)isIPv4Preferred;
173 | - (BOOL)isIPv6Preferred;
174 | - (BOOL)isIPVersionNeutral;
175 |
176 | - (void)setPreferIPv4;
177 | - (void)setPreferIPv6;
178 | - (void)setIPVersionNeutral;
179 |
180 | /**
181 | * Gets/Sets the maximum size of the buffer that will be allocated for receive operations.
182 | * The default maximum size is 9216 bytes.
183 | *
184 | * The theoretical maximum size of any IPv4 UDP packet is UINT16_MAX = 65535.
185 | * The theoretical maximum size of any IPv6 UDP packet is UINT32_MAX = 4294967295.
186 | *
187 | * Since the OS/GCD notifies us of the size of each received UDP packet,
188 | * the actual allocated buffer size for each packet is exact.
189 | * And in practice the size of UDP packets is generally much smaller than the max.
190 | * Indeed most protocols will send and receive packets of only a few bytes,
191 | * or will set a limit on the size of packets to prevent fragmentation in the IP layer.
192 | *
193 | * If you set the buffer size too small, the sockets API in the OS will silently discard
194 | * any extra data, and you will not be notified of the error.
195 | **/
196 | - (uint16_t)maxReceiveIPv4BufferSize;
197 | - (void)setMaxReceiveIPv4BufferSize:(uint16_t)max;
198 |
199 | - (uint32_t)maxReceiveIPv6BufferSize;
200 | - (void)setMaxReceiveIPv6BufferSize:(uint32_t)max;
201 |
202 | /**
203 | * User data allows you to associate arbitrary information with the socket.
204 | * This data is not used internally in any way.
205 | **/
206 | - (id)userData;
207 | - (void)setUserData:(id)arbitraryUserData;
208 |
209 | #pragma mark Diagnostics
210 |
211 | /**
212 | * Returns the local address info for the socket.
213 | *
214 | * The localAddress method returns a sockaddr structure wrapped in a NSData object.
215 | * The localHost method returns the human readable IP address as a string.
216 | *
217 | * Note: Address info may not be available until after the socket has been binded, connected
218 | * or until after data has been sent.
219 | **/
220 | - (NSData *)localAddress;
221 | - (NSString *)localHost;
222 | - (uint16_t)localPort;
223 |
224 | - (NSData *)localAddress_IPv4;
225 | - (NSString *)localHost_IPv4;
226 | - (uint16_t)localPort_IPv4;
227 |
228 | - (NSData *)localAddress_IPv6;
229 | - (NSString *)localHost_IPv6;
230 | - (uint16_t)localPort_IPv6;
231 |
232 | /**
233 | * Returns the remote address info for the socket.
234 | *
235 | * The connectedAddress method returns a sockaddr structure wrapped in a NSData object.
236 | * The connectedHost method returns the human readable IP address as a string.
237 | *
238 | * Note: Since UDP is connectionless by design, connected address info
239 | * will not be available unless the socket is explicitly connected to a remote host/port.
240 | * If the socket is not connected, these methods will return nil / 0.
241 | **/
242 | - (NSData *)connectedAddress;
243 | - (NSString *)connectedHost;
244 | - (uint16_t)connectedPort;
245 |
246 | /**
247 | * Returns whether or not this socket has been connected to a single host.
248 | * By design, UDP is a connectionless protocol, and connecting is not needed.
249 | * If connected, the socket will only be able to send/receive data to/from the connected host.
250 | **/
251 | - (BOOL)isConnected;
252 |
253 | /**
254 | * Returns whether or not this socket has been closed.
255 | * The only way a socket can be closed is if you explicitly call one of the close methods.
256 | **/
257 | - (BOOL)isClosed;
258 |
259 | /**
260 | * Returns whether or not this socket is IPv4.
261 | *
262 | * By default this will be true, unless:
263 | * - IPv4 is disabled (via setIPv4Enabled:)
264 | * - The socket is explicitly bound to an IPv6 address
265 | * - The socket is connected to an IPv6 address
266 | **/
267 | - (BOOL)isIPv4;
268 |
269 | /**
270 | * Returns whether or not this socket is IPv6.
271 | *
272 | * By default this will be true, unless:
273 | * - IPv6 is disabled (via setIPv6Enabled:)
274 | * - The socket is explicitly bound to an IPv4 address
275 | * _ The socket is connected to an IPv4 address
276 | *
277 | * This method will also return false on platforms that do not support IPv6.
278 | * Note: The iPhone does not currently support IPv6.
279 | **/
280 | - (BOOL)isIPv6;
281 |
282 | #pragma mark Binding
283 |
284 | /**
285 | * Binds the UDP socket to the given port.
286 | * Binding should be done for server sockets that receive data prior to sending it.
287 | * Client sockets can skip binding,
288 | * as the OS will automatically assign the socket an available port when it starts sending data.
289 | *
290 | * You may optionally pass a port number of zero to immediately bind the socket,
291 | * yet still allow the OS to automatically assign an available port.
292 | *
293 | * You cannot bind a socket after its been connected.
294 | * You can only bind a socket once.
295 | * You can still connect a socket (if desired) after binding.
296 | *
297 | * On success, returns YES.
298 | * Otherwise returns NO, and sets errPtr. If you don't care about the error, you can pass NULL for errPtr.
299 | **/
300 | - (BOOL)bindToPort:(uint16_t)port error:(NSError **)errPtr;
301 |
302 | /**
303 | * Binds the UDP socket to the given port and optional interface.
304 | * Binding should be done for server sockets that receive data prior to sending it.
305 | * Client sockets can skip binding,
306 | * as the OS will automatically assign the socket an available port when it starts sending data.
307 | *
308 | * You may optionally pass a port number of zero to immediately bind the socket,
309 | * yet still allow the OS to automatically assign an available port.
310 | *
311 | * The interface may be a name (e.g. "en1" or "lo0") or the corresponding IP address (e.g. "192.168.4.35").
312 | * You may also use the special strings "localhost" or "loopback" to specify that
313 | * the socket only accept packets from the local machine.
314 | *
315 | * You cannot bind a socket after its been connected.
316 | * You can only bind a socket once.
317 | * You can still connect a socket (if desired) after binding.
318 | *
319 | * On success, returns YES.
320 | * Otherwise returns NO, and sets errPtr. If you don't care about the error, you can pass NULL for errPtr.
321 | **/
322 | - (BOOL)bindToPort:(uint16_t)port interface:(NSString *)interface error:(NSError **)errPtr;
323 |
324 | /**
325 | * Binds the UDP socket to the given address, specified as a sockaddr structure wrapped in a NSData object.
326 | *
327 | * If you have an existing struct sockaddr you can convert it to a NSData object like so:
328 | * struct sockaddr sa -> NSData *dsa = [NSData dataWithBytes:&remoteAddr length:remoteAddr.sa_len];
329 | * struct sockaddr *sa -> NSData *dsa = [NSData dataWithBytes:remoteAddr length:remoteAddr->sa_len];
330 | *
331 | * Binding should be done for server sockets that receive data prior to sending it.
332 | * Client sockets can skip binding,
333 | * as the OS will automatically assign the socket an available port when it starts sending data.
334 | *
335 | * You cannot bind a socket after its been connected.
336 | * You can only bind a socket once.
337 | * You can still connect a socket (if desired) after binding.
338 | *
339 | * On success, returns YES.
340 | * Otherwise returns NO, and sets errPtr. If you don't care about the error, you can pass NULL for errPtr.
341 | **/
342 | - (BOOL)bindToAddress:(NSData *)localAddr error:(NSError **)errPtr;
343 |
344 | #pragma mark Connecting
345 |
346 | /**
347 | * Connects the UDP socket to the given host and port.
348 | * By design, UDP is a connectionless protocol, and connecting is not needed.
349 | *
350 | * Choosing to connect to a specific host/port has the following effect:
351 | * - You will only be able to send data to the connected host/port.
352 | * - You will only be able to receive data from the connected host/port.
353 | * - You will receive ICMP messages that come from the connected host/port, such as "connection refused".
354 | *
355 | * The actual process of connecting a UDP socket does not result in any communication on the socket.
356 | * It simply changes the internal state of the socket.
357 | *
358 | * You cannot bind a socket after it has been connected.
359 | * You can only connect a socket once.
360 | *
361 | * The host may be a domain name (e.g. "deusty.com") or an IP address string (e.g. "192.168.0.2").
362 | *
363 | * This method is asynchronous as it requires a DNS lookup to resolve the given host name.
364 | * If an obvious error is detected, this method immediately returns NO and sets errPtr.
365 | * If you don't care about the error, you can pass nil for errPtr.
366 | * Otherwise, this method returns YES and begins the asynchronous connection process.
367 | * The result of the asynchronous connection process will be reported via the delegate methods.
368 | **/
369 | - (BOOL)connectToHost:(NSString *)host onPort:(uint16_t)port error:(NSError **)errPtr;
370 |
371 | /**
372 | * Connects the UDP socket to the given address, specified as a sockaddr structure wrapped in a NSData object.
373 | *
374 | * If you have an existing struct sockaddr you can convert it to a NSData object like so:
375 | * struct sockaddr sa -> NSData *dsa = [NSData dataWithBytes:&remoteAddr length:remoteAddr.sa_len];
376 | * struct sockaddr *sa -> NSData *dsa = [NSData dataWithBytes:remoteAddr length:remoteAddr->sa_len];
377 | *
378 | * By design, UDP is a connectionless protocol, and connecting is not needed.
379 | *
380 | * Choosing to connect to a specific address has the following effect:
381 | * - You will only be able to send data to the connected address.
382 | * - You will only be able to receive data from the connected address.
383 | * - You will receive ICMP messages that come from the connected address, such as "connection refused".
384 | *
385 | * Connecting a UDP socket does not result in any communication on the socket.
386 | * It simply changes the internal state of the socket.
387 | *
388 | * You cannot bind a socket after its been connected.
389 | * You can only connect a socket once.
390 | *
391 | * On success, returns YES.
392 | * Otherwise returns NO, and sets errPtr. If you don't care about the error, you can pass nil for errPtr.
393 | *
394 | * Note: Unlike the connectToHost:onPort:error: method, this method does not require a DNS lookup.
395 | * Thus when this method returns, the connection has either failed or fully completed.
396 | * In other words, this method is synchronous, unlike the asynchronous connectToHost::: method.
397 | * However, for compatibility and simplification of delegate code, if this method returns YES
398 | * then the corresponding delegate method (udpSocket:didConnectToHost:port:) is still invoked.
399 | **/
400 | - (BOOL)connectToAddress:(NSData *)remoteAddr error:(NSError **)errPtr;
401 |
402 | #pragma mark Multicast
403 |
404 | /**
405 | * Join multicast group.
406 | * Group should be an IP address (eg @"225.228.0.1").
407 | *
408 | * On success, returns YES.
409 | * Otherwise returns NO, and sets errPtr. If you don't care about the error, you can pass nil for errPtr.
410 | **/
411 | - (BOOL)joinMulticastGroup:(NSString *)group error:(NSError **)errPtr;
412 |
413 | /**
414 | * Join multicast group.
415 | * Group should be an IP address (eg @"225.228.0.1").
416 | * The interface may be a name (e.g. "en1" or "lo0") or the corresponding IP address (e.g. "192.168.4.35").
417 | *
418 | * On success, returns YES.
419 | * Otherwise returns NO, and sets errPtr. If you don't care about the error, you can pass nil for errPtr.
420 | **/
421 | - (BOOL)joinMulticastGroup:(NSString *)group onInterface:(NSString *)interface error:(NSError **)errPtr;
422 |
423 | - (BOOL)leaveMulticastGroup:(NSString *)group error:(NSError **)errPtr;
424 | - (BOOL)leaveMulticastGroup:(NSString *)group onInterface:(NSString *)interface error:(NSError **)errPtr;
425 |
426 | #pragma mark Broadcast
427 |
428 | /**
429 | * By default, the underlying socket in the OS will not allow you to send broadcast messages.
430 | * In order to send broadcast messages, you need to enable this functionality in the socket.
431 | *
432 | * A broadcast is a UDP message to addresses like "192.168.255.255" or "255.255.255.255" that is
433 | * delivered to every host on the network.
434 | * The reason this is generally disabled by default (by the OS) is to prevent
435 | * accidental broadcast messages from flooding the network.
436 | **/
437 | - (BOOL)enableBroadcast:(BOOL)flag error:(NSError **)errPtr;
438 |
439 | #pragma mark Sending
440 |
441 | /**
442 | * Asynchronously sends the given data, with the given timeout and tag.
443 | *
444 | * This method may only be used with a connected socket.
445 | * Recall that connecting is optional for a UDP socket.
446 | * For connected sockets, data can only be sent to the connected address.
447 | * For non-connected sockets, the remote destination is specified for each packet.
448 | * For more information about optionally connecting udp sockets, see the documentation for the connect methods above.
449 | *
450 | * @param data
451 | * The data to send.
452 | * If data is nil or zero-length, this method does nothing.
453 | * If passing NSMutableData, please read the thread-safety notice below.
454 | *
455 | * @param timeout
456 | * The timeout for the send opeartion.
457 | * If the timeout value is negative, the send operation will not use a timeout.
458 | *
459 | * @param tag
460 | * The tag is for your convenience.
461 | * It is not sent or received over the socket in any manner what-so-ever.
462 | * It is reported back as a parameter in the udpSocket:didSendDataWithTag:
463 | * or udpSocket:didNotSendDataWithTag:dueToError: methods.
464 | * You can use it as an array index, state id, type constant, etc.
465 | *
466 | *
467 | * Thread-Safety Note:
468 | * If the given data parameter is mutable (NSMutableData) then you MUST NOT alter the data while
469 | * the socket is sending it. In other words, it's not safe to alter the data until after the delegate method
470 | * udpSocket:didSendDataWithTag: or udpSocket:didNotSendDataWithTag:dueToError: is invoked signifying
471 | * that this particular send operation has completed.
472 | * This is due to the fact that GCDAsyncUdpSocket does NOT copy the data.
473 | * It simply retains it for performance reasons.
474 | * Often times, if NSMutableData is passed, it is because a request/response was built up in memory.
475 | * Copying this data adds an unwanted/unneeded overhead.
476 | * If you need to write data from an immutable buffer, and you need to alter the buffer before the socket
477 | * completes sending the bytes (which is NOT immediately after this method returns, but rather at a later time
478 | * when the delegate method notifies you), then you should first copy the bytes, and pass the copy to this method.
479 | **/
480 | - (void)sendData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;
481 |
482 | /**
483 | * Asynchronously sends the given data, with the given timeout and tag, to the given host and port.
484 | *
485 | * This method cannot be used with a connected socket.
486 | * Recall that connecting is optional for a UDP socket.
487 | * For connected sockets, data can only be sent to the connected address.
488 | * For non-connected sockets, the remote destination is specified for each packet.
489 | * For more information about optionally connecting udp sockets, see the documentation for the connect methods above.
490 | *
491 | * @param data
492 | * The data to send.
493 | * If data is nil or zero-length, this method does nothing.
494 | * If passing NSMutableData, please read the thread-safety notice below.
495 | *
496 | * @param host
497 | * The destination to send the udp packet to.
498 | * May be specified as a domain name (e.g. "deusty.com") or an IP address string (e.g. "192.168.0.2").
499 | * You may also use the convenience strings of "loopback" or "localhost".
500 | *
501 | * @param port
502 | * The port of the host to send to.
503 | *
504 | * @param timeout
505 | * The timeout for the send opeartion.
506 | * If the timeout value is negative, the send operation will not use a timeout.
507 | *
508 | * @param tag
509 | * The tag is for your convenience.
510 | * It is not sent or received over the socket in any manner what-so-ever.
511 | * It is reported back as a parameter in the udpSocket:didSendDataWithTag:
512 | * or udpSocket:didNotSendDataWithTag:dueToError: methods.
513 | * You can use it as an array index, state id, type constant, etc.
514 | *
515 | *
516 | * Thread-Safety Note:
517 | * If the given data parameter is mutable (NSMutableData) then you MUST NOT alter the data while
518 | * the socket is sending it. In other words, it's not safe to alter the data until after the delegate method
519 | * udpSocket:didSendDataWithTag: or udpSocket:didNotSendDataWithTag:dueToError: is invoked signifying
520 | * that this particular send operation has completed.
521 | * This is due to the fact that GCDAsyncUdpSocket does NOT copy the data.
522 | * It simply retains it for performance reasons.
523 | * Often times, if NSMutableData is passed, it is because a request/response was built up in memory.
524 | * Copying this data adds an unwanted/unneeded overhead.
525 | * If you need to write data from an immutable buffer, and you need to alter the buffer before the socket
526 | * completes sending the bytes (which is NOT immediately after this method returns, but rather at a later time
527 | * when the delegate method notifies you), then you should first copy the bytes, and pass the copy to this method.
528 | **/
529 | - (void)sendData:(NSData *)data
530 | toHost:(NSString *)host
531 | port:(uint16_t)port
532 | withTimeout:(NSTimeInterval)timeout
533 | tag:(long)tag;
534 |
535 | /**
536 | * Asynchronously sends the given data, with the given timeout and tag, to the given address.
537 | *
538 | * This method cannot be used with a connected socket.
539 | * Recall that connecting is optional for a UDP socket.
540 | * For connected sockets, data can only be sent to the connected address.
541 | * For non-connected sockets, the remote destination is specified for each packet.
542 | * For more information about optionally connecting udp sockets, see the documentation for the connect methods above.
543 | *
544 | * @param data
545 | * The data to send.
546 | * If data is nil or zero-length, this method does nothing.
547 | * If passing NSMutableData, please read the thread-safety notice below.
548 | *
549 | * @param remoteAddr
550 | * The address to send the data to (specified as a sockaddr structure wrapped in a NSData object).
551 | *
552 | * @param timeout
553 | * The timeout for the send opeartion.
554 | * If the timeout value is negative, the send operation will not use a timeout.
555 | *
556 | * @param tag
557 | * The tag is for your convenience.
558 | * It is not sent or received over the socket in any manner what-so-ever.
559 | * It is reported back as a parameter in the udpSocket:didSendDataWithTag:
560 | * or udpSocket:didNotSendDataWithTag:dueToError: methods.
561 | * You can use it as an array index, state id, type constant, etc.
562 | *
563 | *
564 | * Thread-Safety Note:
565 | * If the given data parameter is mutable (NSMutableData) then you MUST NOT alter the data while
566 | * the socket is sending it. In other words, it's not safe to alter the data until after the delegate method
567 | * udpSocket:didSendDataWithTag: or udpSocket:didNotSendDataWithTag:dueToError: is invoked signifying
568 | * that this particular send operation has completed.
569 | * This is due to the fact that GCDAsyncUdpSocket does NOT copy the data.
570 | * It simply retains it for performance reasons.
571 | * Often times, if NSMutableData is passed, it is because a request/response was built up in memory.
572 | * Copying this data adds an unwanted/unneeded overhead.
573 | * If you need to write data from an immutable buffer, and you need to alter the buffer before the socket
574 | * completes sending the bytes (which is NOT immediately after this method returns, but rather at a later time
575 | * when the delegate method notifies you), then you should first copy the bytes, and pass the copy to this method.
576 | **/
577 | - (void)sendData:(NSData *)data toAddress:(NSData *)remoteAddr withTimeout:(NSTimeInterval)timeout tag:(long)tag;
578 |
579 | /**
580 | * You may optionally set a send filter for the socket.
581 | * A filter can provide several interesting possibilities:
582 | *
583 | * 1. Optional caching of resolved addresses for domain names.
584 | * The cache could later be consulted, resulting in fewer system calls to getaddrinfo.
585 | *
586 | * 2. Reusable modules of code for bandwidth monitoring.
587 | *
588 | * 3. Sometimes traffic shapers are needed to simulate real world environments.
589 | * A filter allows you to write custom code to simulate such environments.
590 | * The ability to code this yourself is especially helpful when your simulated environment
591 | * is more complicated than simple traffic shaping (e.g. simulating a cone port restricted router),
592 | * or the system tools to handle this aren't available (e.g. on a mobile device).
593 | *
594 | * For more information about GCDAsyncUdpSocketSendFilterBlock, see the documentation for its typedef.
595 | * To remove a previously set filter, invoke this method and pass a nil filterBlock and NULL filterQueue.
596 | *
597 | * Note: This method invokes setSendFilter:withQueue:isAsynchronous: (documented below),
598 | * passing YES for the isAsynchronous parameter.
599 | **/
600 | - (void)setSendFilter:(GCDAsyncUdpSocketSendFilterBlock)filterBlock withQueue:(dispatch_queue_t)filterQueue;
601 |
602 | /**
603 | * The receive filter can be run via dispatch_async or dispatch_sync.
604 | * Most typical situations call for asynchronous operation.
605 | *
606 | * However, there are a few situations in which synchronous operation is preferred.
607 | * Such is the case when the filter is extremely minimal and fast.
608 | * This is because dispatch_sync is faster than dispatch_async.
609 | *
610 | * If you choose synchronous operation, be aware of possible deadlock conditions.
611 | * Since the socket queue is executing your block via dispatch_sync,
612 | * then you cannot perform any tasks which may invoke dispatch_sync on the socket queue.
613 | * For example, you can't query properties on the socket.
614 | **/
615 | - (void)setSendFilter:(GCDAsyncUdpSocketSendFilterBlock)filterBlock
616 | withQueue:(dispatch_queue_t)filterQueue
617 | isAsynchronous:(BOOL)isAsynchronous;
618 |
619 | #pragma mark Receiving
620 |
621 | /**
622 | * There are two modes of operation for receiving packets: one-at-a-time & continuous.
623 | *
624 | * In one-at-a-time mode, you call receiveOnce everytime your delegate is ready to process an incoming udp packet.
625 | * Receiving packets one-at-a-time may be better suited for implementing certain state machine code,
626 | * where your state machine may not always be ready to process incoming packets.
627 | *
628 | * In continuous mode, the delegate is invoked immediately everytime incoming udp packets are received.
629 | * Receiving packets continuously is better suited to real-time streaming applications.
630 | *
631 | * You may switch back and forth between one-at-a-time mode and continuous mode.
632 | * If the socket is currently in continuous mode, calling this method will switch it to one-at-a-time mode.
633 | *
634 | * When a packet is received (and not filtered by the optional receive filter),
635 | * the delegate method (udpSocket:didReceiveData:fromAddress:withFilterContext:) is invoked.
636 | *
637 | * If the socket is able to begin receiving packets, this method returns YES.
638 | * Otherwise it returns NO, and sets the errPtr with appropriate error information.
639 | *
640 | * An example error:
641 | * You created a udp socket to act as a server, and immediately called receive.
642 | * You forgot to first bind the socket to a port number, and received a error with a message like:
643 | * "Must bind socket before you can receive data."
644 | **/
645 | - (BOOL)receiveOnce:(NSError **)errPtr;
646 |
647 | /**
648 | * There are two modes of operation for receiving packets: one-at-a-time & continuous.
649 | *
650 | * In one-at-a-time mode, you call receiveOnce everytime your delegate is ready to process an incoming udp packet.
651 | * Receiving packets one-at-a-time may be better suited for implementing certain state machine code,
652 | * where your state machine may not always be ready to process incoming packets.
653 | *
654 | * In continuous mode, the delegate is invoked immediately everytime incoming udp packets are received.
655 | * Receiving packets continuously is better suited to real-time streaming applications.
656 | *
657 | * You may switch back and forth between one-at-a-time mode and continuous mode.
658 | * If the socket is currently in one-at-a-time mode, calling this method will switch it to continuous mode.
659 | *
660 | * For every received packet (not filtered by the optional receive filter),
661 | * the delegate method (udpSocket:didReceiveData:fromAddress:withFilterContext:) is invoked.
662 | *
663 | * If the socket is able to begin receiving packets, this method returns YES.
664 | * Otherwise it returns NO, and sets the errPtr with appropriate error information.
665 | *
666 | * An example error:
667 | * You created a udp socket to act as a server, and immediately called receive.
668 | * You forgot to first bind the socket to a port number, and received a error with a message like:
669 | * "Must bind socket before you can receive data."
670 | **/
671 | - (BOOL)beginReceiving:(NSError **)errPtr;
672 |
673 | /**
674 | * If the socket is currently receiving (beginReceiving has been called), this method pauses the receiving.
675 | * That is, it won't read any more packets from the underlying OS socket until beginReceiving is called again.
676 | *
677 | * Important Note:
678 | * GCDAsyncUdpSocket may be running in parallel with your code.
679 | * That is, your delegate is likely running on a separate thread/dispatch_queue.
680 | * When you invoke this method, GCDAsyncUdpSocket may have already dispatched delegate methods to be invoked.
681 | * Thus, if those delegate methods have already been dispatch_async'd,
682 | * your didReceive delegate method may still be invoked after this method has been called.
683 | * You should be aware of this, and program defensively.
684 | **/
685 | - (void)pauseReceiving;
686 |
687 | /**
688 | * You may optionally set a receive filter for the socket.
689 | * This receive filter may be set to run in its own queue (independent of delegate queue).
690 | *
691 | * A filter can provide several useful features.
692 | *
693 | * 1. Many times udp packets need to be parsed.
694 | * Since the filter can run in its own independent queue, you can parallelize this parsing quite easily.
695 | * The end result is a parallel socket io, datagram parsing, and packet processing.
696 | *
697 | * 2. Many times udp packets are discarded because they are duplicate/unneeded/unsolicited.
698 | * The filter can prevent such packets from arriving at the delegate.
699 | * And because the filter can run in its own independent queue, this doesn't slow down the delegate.
700 | *
701 | * - Since the udp protocol does not guarantee delivery, udp packets may be lost.
702 | * Many protocols built atop udp thus provide various resend/re-request algorithms.
703 | * This sometimes results in duplicate packets arriving.
704 | * A filter may allow you to architect the duplicate detection code to run in parallel to normal processing.
705 | *
706 | * - Since the udp socket may be connectionless, its possible for unsolicited packets to arrive.
707 | * Such packets need to be ignored.
708 | *
709 | * 3. Sometimes traffic shapers are needed to simulate real world environments.
710 | * A filter allows you to write custom code to simulate such environments.
711 | * The ability to code this yourself is especially helpful when your simulated environment
712 | * is more complicated than simple traffic shaping (e.g. simulating a cone port restricted router),
713 | * or the system tools to handle this aren't available (e.g. on a mobile device).
714 | *
715 | * Example:
716 | *
717 | * GCDAsyncUdpSocketReceiveFilterBlock filter = ^BOOL (NSData *data, NSData *address, id *context) {
718 | *
719 | * MyProtocolMessage *msg = [MyProtocol parseMessage:data];
720 | *
721 | * *context = response;
722 | * return (response != nil);
723 | * };
724 | * [udpSocket setReceiveFilter:filter withQueue:myParsingQueue];
725 | *
726 | * For more information about GCDAsyncUdpSocketReceiveFilterBlock, see the documentation for its typedef.
727 | * To remove a previously set filter, invoke this method and pass a nil filterBlock and NULL filterQueue.
728 | *
729 | * Note: This method invokes setReceiveFilter:withQueue:isAsynchronous: (documented below),
730 | * passing YES for the isAsynchronous parameter.
731 | **/
732 | - (void)setReceiveFilter:(GCDAsyncUdpSocketReceiveFilterBlock)filterBlock withQueue:(dispatch_queue_t)filterQueue;
733 |
734 | /**
735 | * The receive filter can be run via dispatch_async or dispatch_sync.
736 | * Most typical situations call for asynchronous operation.
737 | *
738 | * However, there are a few situations in which synchronous operation is preferred.
739 | * Such is the case when the filter is extremely minimal and fast.
740 | * This is because dispatch_sync is faster than dispatch_async.
741 | *
742 | * If you choose synchronous operation, be aware of possible deadlock conditions.
743 | * Since the socket queue is executing your block via dispatch_sync,
744 | * then you cannot perform any tasks which may invoke dispatch_sync on the socket queue.
745 | * For example, you can't query properties on the socket.
746 | **/
747 | - (void)setReceiveFilter:(GCDAsyncUdpSocketReceiveFilterBlock)filterBlock
748 | withQueue:(dispatch_queue_t)filterQueue
749 | isAsynchronous:(BOOL)isAsynchronous;
750 |
751 | #pragma mark Closing
752 |
753 | /**
754 | * Immediately closes the underlying socket.
755 | * Any pending send operations are discarded.
756 | *
757 | * The GCDAsyncUdpSocket instance may optionally be used again.
758 | * (it will setup/configure/use another unnderlying BSD socket).
759 | **/
760 | - (void)close;
761 |
762 | /**
763 | * Closes the underlying socket after all pending send operations have been sent.
764 | *
765 | * The GCDAsyncUdpSocket instance may optionally be used again.
766 | * (it will setup/configure/use another unnderlying BSD socket).
767 | **/
768 | - (void)closeAfterSending;
769 |
770 | #pragma mark Advanced
771 | /**
772 | * GCDAsyncSocket maintains thread safety by using an internal serial dispatch_queue.
773 | * In most cases, the instance creates this queue itself.
774 | * However, to allow for maximum flexibility, the internal queue may be passed in the init method.
775 | * This allows for some advanced options such as controlling socket priority via target queues.
776 | * However, when one begins to use target queues like this, they open the door to some specific deadlock issues.
777 | *
778 | * For example, imagine there are 2 queues:
779 | * dispatch_queue_t socketQueue;
780 | * dispatch_queue_t socketTargetQueue;
781 | *
782 | * If you do this (pseudo-code):
783 | * socketQueue.targetQueue = socketTargetQueue;
784 | *
785 | * Then all socketQueue operations will actually get run on the given socketTargetQueue.
786 | * This is fine and works great in most situations.
787 | * But if you run code directly from within the socketTargetQueue that accesses the socket,
788 | * you could potentially get deadlock. Imagine the following code:
789 | *
790 | * - (BOOL)socketHasSomething
791 | * {
792 | * __block BOOL result = NO;
793 | * dispatch_block_t block = ^{
794 | * result = [self someInternalMethodToBeRunOnlyOnSocketQueue];
795 | * }
796 | * if (is_executing_on_queue(socketQueue))
797 | * block();
798 | * else
799 | * dispatch_sync(socketQueue, block);
800 | *
801 | * return result;
802 | * }
803 | *
804 | * What happens if you call this method from the socketTargetQueue? The result is deadlock.
805 | * This is because the GCD API offers no mechanism to discover a queue's targetQueue.
806 | * Thus we have no idea if our socketQueue is configured with a targetQueue.
807 | * If we had this information, we could easily avoid deadlock.
808 | * But, since these API's are missing or unfeasible, you'll have to explicitly set it.
809 | *
810 | * IF you pass a socketQueue via the init method,
811 | * AND you've configured the passed socketQueue with a targetQueue,
812 | * THEN you should pass the end queue in the target hierarchy.
813 | *
814 | * For example, consider the following queue hierarchy:
815 | * socketQueue -> ipQueue -> moduleQueue
816 | *
817 | * This example demonstrates priority shaping within some server.
818 | * All incoming client connections from the same IP address are executed on the same target queue.
819 | * And all connections for a particular module are executed on the same target queue.
820 | * Thus, the priority of all networking for the entire module can be changed on the fly.
821 | * Additionally, networking traffic from a single IP cannot monopolize the module.
822 | *
823 | * Here's how you would accomplish something like that:
824 | * - (dispatch_queue_t)newSocketQueueForConnectionFromAddress:(NSData *)address onSocket:(GCDAsyncSocket *)sock
825 | * {
826 | * dispatch_queue_t socketQueue = dispatch_queue_create("", NULL);
827 | * dispatch_queue_t ipQueue = [self ipQueueForAddress:address];
828 | *
829 | * dispatch_set_target_queue(socketQueue, ipQueue);
830 | * dispatch_set_target_queue(iqQueue, moduleQueue);
831 | *
832 | * return socketQueue;
833 | * }
834 | * - (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket
835 | * {
836 | * [clientConnections addObject:newSocket];
837 | * [newSocket markSocketQueueTargetQueue:moduleQueue];
838 | * }
839 | *
840 | * Note: This workaround is ONLY needed if you intend to execute code directly on the ipQueue or moduleQueue.
841 | * This is often NOT the case, as such queues are used solely for execution shaping.
842 | **/
843 | - (void)markSocketQueueTargetQueue:(dispatch_queue_t)socketQueuesPreConfiguredTargetQueue;
844 | - (void)unmarkSocketQueueTargetQueue:(dispatch_queue_t)socketQueuesPreviouslyConfiguredTargetQueue;
845 |
846 | /**
847 | * It's not thread-safe to access certain variables from outside the socket's internal queue.
848 | *
849 | * For example, the socket file descriptor.
850 | * File descriptors are simply integers which reference an index in the per-process file table.
851 | * However, when one requests a new file descriptor (by opening a file or socket),
852 | * the file descriptor returned is guaranteed to be the lowest numbered unused descriptor.
853 | * So if we're not careful, the following could be possible:
854 | *
855 | * - Thread A invokes a method which returns the socket's file descriptor.
856 | * - The socket is closed via the socket's internal queue on thread B.
857 | * - Thread C opens a file, and subsequently receives the file descriptor that was previously the socket's FD.
858 | * - Thread A is now accessing/altering the file instead of the socket.
859 | *
860 | * In addition to this, other variables are not actually objects,
861 | * and thus cannot be retained/released or even autoreleased.
862 | * An example is the sslContext, of type SSLContextRef, which is actually a malloc'd struct.
863 | *
864 | * Although there are internal variables that make it difficult to maintain thread-safety,
865 | * it is important to provide access to these variables
866 | * to ensure this class can be used in a wide array of environments.
867 | * This method helps to accomplish this by invoking the current block on the socket's internal queue.
868 | * The methods below can be invoked from within the block to access
869 | * those generally thread-unsafe internal variables in a thread-safe manner.
870 | * The given block will be invoked synchronously on the socket's internal queue.
871 | *
872 | * If you save references to any protected variables and use them outside the block, you do so at your own peril.
873 | **/
874 | - (void)performBlock:(dispatch_block_t)block;
875 |
876 | /**
877 | * These methods are only available from within the context of a performBlock: invocation.
878 | * See the documentation for the performBlock: method above.
879 | *
880 | * Provides access to the socket's file descriptor(s).
881 | * If the socket isn't connected, or explicity bound to a particular interface,
882 | * it might actually have multiple internal socket file descriptors - one for IPv4 and one for IPv6.
883 | **/
884 | - (int)socketFD;
885 | - (int)socket4FD;
886 | - (int)socket6FD;
887 |
888 | #if TARGET_OS_IPHONE
889 |
890 | /**
891 | * These methods are only available from within the context of a performBlock: invocation.
892 | * See the documentation for the performBlock: method above.
893 | *
894 | * Returns (creating if necessary) a CFReadStream/CFWriteStream for the internal socket.
895 | *
896 | * Generally GCDAsyncUdpSocket doesn't use CFStream. (It uses the faster GCD API's.)
897 | * However, if you need one for any reason,
898 | * these methods are a convenient way to get access to a safe instance of one.
899 | **/
900 | - (CFReadStreamRef)readStream;
901 | - (CFWriteStreamRef)writeStream;
902 |
903 | /**
904 | * This method is only available from within the context of a performBlock: invocation.
905 | * See the documentation for the performBlock: method above.
906 | *
907 | * Configures the socket to allow it to operate when the iOS application has been backgrounded.
908 | * In other words, this method creates a read & write stream, and invokes:
909 | *
910 | * CFReadStreamSetProperty(readStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
911 | * CFWriteStreamSetProperty(writeStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
912 | *
913 | * Returns YES if successful, NO otherwise.
914 | *
915 | * Example usage:
916 | *
917 | * [asyncUdpSocket performBlock:^{
918 | * [asyncUdpSocket enableBackgroundingOnSocket];
919 | * }];
920 | *
921 | *
922 | * NOTE : Apple doesn't currently support backgrounding UDP sockets. (Only TCP for now).
923 | **/
924 | //- (BOOL)enableBackgroundingOnSockets;
925 |
926 | #endif
927 |
928 | #pragma mark Utilities
929 |
930 | /**
931 | * Extracting host/port/family information from raw address data.
932 | **/
933 |
934 | + (NSString *)hostFromAddress:(NSData *)address;
935 | + (uint16_t)portFromAddress:(NSData *)address;
936 | + (int)familyFromAddress:(NSData *)address;
937 |
938 | + (BOOL)isIPv4Address:(NSData *)address;
939 | + (BOOL)isIPv6Address:(NSData *)address;
940 |
941 | + (BOOL)getHost:(NSString **)hostPtr port:(uint16_t *)portPtr fromAddress:(NSData *)address;
942 | + (BOOL)getHost:(NSString **)hostPtr port:(uint16_t *)portPtr family:(int *)afPtr fromAddress:(NSData *)address;
943 |
944 | @end
945 |
946 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
947 | #pragma mark -
948 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
949 |
950 | @protocol GCDAsyncUdpSocketDelegate
951 | @optional
952 |
953 | /**
954 | * By design, UDP is a connectionless protocol, and connecting is not needed.
955 | * However, you may optionally choose to connect to a particular host for reasons
956 | * outlined in the documentation for the various connect methods listed above.
957 | *
958 | * This method is called if one of the connect methods are invoked, and the connection is successful.
959 | **/
960 | - (void)udpSocket:(GCDAsyncUdpSocket *)sock didConnectToAddress:(NSData *)address;
961 |
962 | /**
963 | * By design, UDP is a connectionless protocol, and connecting is not needed.
964 | * However, you may optionally choose to connect to a particular host for reasons
965 | * outlined in the documentation for the various connect methods listed above.
966 | *
967 | * This method is called if one of the connect methods are invoked, and the connection fails.
968 | * This may happen, for example, if a domain name is given for the host and the domain name is unable to be resolved.
969 | **/
970 | - (void)udpSocket:(GCDAsyncUdpSocket *)sock didNotConnect:(NSError *)error;
971 |
972 | /**
973 | * Called when the datagram with the given tag has been sent.
974 | **/
975 | - (void)udpSocket:(GCDAsyncUdpSocket *)sock didSendDataWithTag:(long)tag;
976 |
977 | /**
978 | * Called if an error occurs while trying to send a datagram.
979 | * This could be due to a timeout, or something more serious such as the data being too large to fit in a sigle packet.
980 | **/
981 | - (void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error;
982 |
983 | /**
984 | * Called when the socket has received the requested datagram.
985 | **/
986 | - (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
987 | fromAddress:(NSData *)address
988 | withFilterContext:(id)filterContext;
989 |
990 | /**
991 | * Called when the socket is closed.
992 | **/
993 | - (void)udpSocketDidClose:(GCDAsyncUdpSocket *)sock withError:(NSError *)error;
994 |
995 | @end
996 |
997 |
--------------------------------------------------------------------------------
/SocketClient-Demo/AsyncSocket/GCD/GCDAsyncSocket.h:
--------------------------------------------------------------------------------
1 | //
2 | // GCDAsyncSocket.h
3 | //
4 | // This class is in the public domain.
5 | // Originally created by Robbie Hanson in Q3 2010.
6 | // Updated and maintained by Deusty LLC and the Apple development community.
7 | //
8 | // https://github.com/robbiehanson/CocoaAsyncSocket
9 | //
10 |
11 | #import
12 | #import
13 | #import
14 | #import
15 | #import
16 |
17 | #include // AF_INET, AF_INET6
18 |
19 | @class GCDAsyncReadPacket;
20 | @class GCDAsyncWritePacket;
21 | @class GCDAsyncSocketPreBuffer;
22 |
23 | extern NSString *const GCDAsyncSocketException;
24 | extern NSString *const GCDAsyncSocketErrorDomain;
25 |
26 | extern NSString *const GCDAsyncSocketQueueName;
27 | extern NSString *const GCDAsyncSocketThreadName;
28 |
29 | extern NSString *const GCDAsyncSocketManuallyEvaluateTrust;
30 | #if TARGET_OS_IPHONE
31 | extern NSString *const GCDAsyncSocketUseCFStreamForTLS;
32 | #endif
33 | #define GCDAsyncSocketSSLPeerName (NSString *)kCFStreamSSLPeerName
34 | #define GCDAsyncSocketSSLCertificates (NSString *)kCFStreamSSLCertificates
35 | #define GCDAsyncSocketSSLIsServer (NSString *)kCFStreamSSLIsServer
36 | extern NSString *const GCDAsyncSocketSSLPeerID;
37 | extern NSString *const GCDAsyncSocketSSLProtocolVersionMin;
38 | extern NSString *const GCDAsyncSocketSSLProtocolVersionMax;
39 | extern NSString *const GCDAsyncSocketSSLSessionOptionFalseStart;
40 | extern NSString *const GCDAsyncSocketSSLSessionOptionSendOneByteRecord;
41 | extern NSString *const GCDAsyncSocketSSLCipherSuites;
42 | #if !TARGET_OS_IPHONE
43 | extern NSString *const GCDAsyncSocketSSLDiffieHellmanParameters;
44 | #endif
45 |
46 | #define GCDAsyncSocketLoggingContext 65535
47 |
48 |
49 | enum GCDAsyncSocketError
50 | {
51 | GCDAsyncSocketNoError = 0, // Never used
52 | GCDAsyncSocketBadConfigError, // Invalid configuration
53 | GCDAsyncSocketBadParamError, // Invalid parameter was passed
54 | GCDAsyncSocketConnectTimeoutError, // A connect operation timed out
55 | GCDAsyncSocketReadTimeoutError, // A read operation timed out
56 | GCDAsyncSocketWriteTimeoutError, // A write operation timed out
57 | GCDAsyncSocketReadMaxedOutError, // Reached set maxLength without completing
58 | GCDAsyncSocketClosedError, // The remote peer closed the connection
59 | GCDAsyncSocketOtherError, // Description provided in userInfo
60 | };
61 | typedef enum GCDAsyncSocketError GCDAsyncSocketError;
62 |
63 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
64 | #pragma mark -
65 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
66 |
67 | @interface GCDAsyncSocket : NSObject
68 |
69 | /**
70 | * GCDAsyncSocket uses the standard delegate paradigm,
71 | * but executes all delegate callbacks on a given delegate dispatch queue.
72 | * This allows for maximum concurrency, while at the same time providing easy thread safety.
73 | *
74 | * You MUST set a delegate AND delegate dispatch queue before attempting to
75 | * use the socket, or you will get an error.
76 | *
77 | * The socket queue is optional.
78 | * If you pass NULL, GCDAsyncSocket will automatically create it's own socket queue.
79 | * If you choose to provide a socket queue, the socket queue must not be a concurrent queue.
80 | * If you choose to provide a socket queue, and the socket queue has a configured target queue,
81 | * then please see the discussion for the method markSocketQueueTargetQueue.
82 | *
83 | * The delegate queue and socket queue can optionally be the same.
84 | **/
85 | - (id)init;
86 | - (id)initWithSocketQueue:(dispatch_queue_t)sq;
87 | - (id)initWithDelegate:(id)aDelegate delegateQueue:(dispatch_queue_t)dq;
88 | - (id)initWithDelegate:(id)aDelegate delegateQueue:(dispatch_queue_t)dq socketQueue:(dispatch_queue_t)sq;
89 |
90 | #pragma mark Configuration
91 |
92 | @property (atomic, weak, readwrite) id delegate;
93 | #if OS_OBJECT_USE_OBJC
94 | @property (atomic, strong, readwrite) dispatch_queue_t delegateQueue;
95 | #else
96 | @property (atomic, assign, readwrite) dispatch_queue_t delegateQueue;
97 | #endif
98 |
99 | - (void)getDelegate:(id *)delegatePtr delegateQueue:(dispatch_queue_t *)delegateQueuePtr;
100 | - (void)setDelegate:(id)delegate delegateQueue:(dispatch_queue_t)delegateQueue;
101 |
102 | /**
103 | * If you are setting the delegate to nil within the delegate's dealloc method,
104 | * you may need to use the synchronous versions below.
105 | **/
106 | - (void)synchronouslySetDelegate:(id)delegate;
107 | - (void)synchronouslySetDelegateQueue:(dispatch_queue_t)delegateQueue;
108 | - (void)synchronouslySetDelegate:(id)delegate delegateQueue:(dispatch_queue_t)delegateQueue;
109 |
110 | /**
111 | * By default, both IPv4 and IPv6 are enabled.
112 | *
113 | * For accepting incoming connections, this means GCDAsyncSocket automatically supports both protocols,
114 | * and can simulataneously accept incoming connections on either protocol.
115 | *
116 | * For outgoing connections, this means GCDAsyncSocket can connect to remote hosts running either protocol.
117 | * If a DNS lookup returns only IPv4 results, GCDAsyncSocket will automatically use IPv4.
118 | * If a DNS lookup returns only IPv6 results, GCDAsyncSocket will automatically use IPv6.
119 | * If a DNS lookup returns both IPv4 and IPv6 results, the preferred protocol will be chosen.
120 | * By default, the preferred protocol is IPv4, but may be configured as desired.
121 | **/
122 |
123 | @property (atomic, assign, readwrite, getter=isIPv4Enabled) BOOL IPv4Enabled;
124 | @property (atomic, assign, readwrite, getter=isIPv6Enabled) BOOL IPv6Enabled;
125 |
126 | @property (atomic, assign, readwrite, getter=isIPv4PreferredOverIPv6) BOOL IPv4PreferredOverIPv6;
127 |
128 | /**
129 | * User data allows you to associate arbitrary information with the socket.
130 | * This data is not used internally by socket in any way.
131 | **/
132 | @property (atomic, strong, readwrite) id userData;
133 |
134 | #pragma mark Accepting
135 |
136 | /**
137 | * Tells the socket to begin listening and accepting connections on the given port.
138 | * When a connection is accepted, a new instance of GCDAsyncSocket will be spawned to handle it,
139 | * and the socket:didAcceptNewSocket: delegate method will be invoked.
140 | *
141 | * The socket will listen on all available interfaces (e.g. wifi, ethernet, etc)
142 | **/
143 | - (BOOL)acceptOnPort:(uint16_t)port error:(NSError **)errPtr;
144 |
145 | /**
146 | * This method is the same as acceptOnPort:error: with the
147 | * additional option of specifying which interface to listen on.
148 | *
149 | * For example, you could specify that the socket should only accept connections over ethernet,
150 | * and not other interfaces such as wifi.
151 | *
152 | * The interface may be specified by name (e.g. "en1" or "lo0") or by IP address (e.g. "192.168.4.34").
153 | * You may also use the special strings "localhost" or "loopback" to specify that
154 | * the socket only accept connections from the local machine.
155 | *
156 | * You can see the list of interfaces via the command line utility "ifconfig",
157 | * or programmatically via the getifaddrs() function.
158 | *
159 | * To accept connections on any interface pass nil, or simply use the acceptOnPort:error: method.
160 | **/
161 | - (BOOL)acceptOnInterface:(NSString *)interface port:(uint16_t)port error:(NSError **)errPtr;
162 |
163 | #pragma mark Connecting
164 |
165 | /**
166 | * Connects to the given host and port.
167 | *
168 | * This method invokes connectToHost:onPort:viaInterface:withTimeout:error:
169 | * and uses the default interface, and no timeout.
170 | **/
171 | - (BOOL)connectToHost:(NSString *)host onPort:(uint16_t)port error:(NSError **)errPtr;
172 |
173 | /**
174 | * Connects to the given host and port with an optional timeout.
175 | *
176 | * This method invokes connectToHost:onPort:viaInterface:withTimeout:error: and uses the default interface.
177 | **/
178 | - (BOOL)connectToHost:(NSString *)host
179 | onPort:(uint16_t)port
180 | withTimeout:(NSTimeInterval)timeout
181 | error:(NSError **)errPtr;
182 |
183 | /**
184 | * Connects to the given host & port, via the optional interface, with an optional timeout.
185 | *
186 | * The host may be a domain name (e.g. "deusty.com") or an IP address string (e.g. "192.168.0.2").
187 | * The host may also be the special strings "localhost" or "loopback" to specify connecting
188 | * to a service on the local machine.
189 | *
190 | * The interface may be a name (e.g. "en1" or "lo0") or the corresponding IP address (e.g. "192.168.4.35").
191 | * The interface may also be used to specify the local port (see below).
192 | *
193 | * To not time out use a negative time interval.
194 | *
195 | * This method will return NO if an error is detected, and set the error pointer (if one was given).
196 | * Possible errors would be a nil host, invalid interface, or socket is already connected.
197 | *
198 | * If no errors are detected, this method will start a background connect operation and immediately return YES.
199 | * The delegate callbacks are used to notify you when the socket connects, or if the host was unreachable.
200 | *
201 | * Since this class supports queued reads and writes, you can immediately start reading and/or writing.
202 | * All read/write operations will be queued, and upon socket connection,
203 | * the operations will be dequeued and processed in order.
204 | *
205 | * The interface may optionally contain a port number at the end of the string, separated by a colon.
206 | * This allows you to specify the local port that should be used for the outgoing connection. (read paragraph to end)
207 | * To specify both interface and local port: "en1:8082" or "192.168.4.35:2424".
208 | * To specify only local port: ":8082".
209 | * Please note this is an advanced feature, and is somewhat hidden on purpose.
210 | * You should understand that 99.999% of the time you should NOT specify the local port for an outgoing connection.
211 | * If you think you need to, there is a very good chance you have a fundamental misunderstanding somewhere.
212 | * Local ports do NOT need to match remote ports. In fact, they almost never do.
213 | * This feature is here for networking professionals using very advanced techniques.
214 | **/
215 | - (BOOL)connectToHost:(NSString *)host
216 | onPort:(uint16_t)port
217 | viaInterface:(NSString *)interface
218 | withTimeout:(NSTimeInterval)timeout
219 | error:(NSError **)errPtr;
220 |
221 | /**
222 | * Connects to the given address, specified as a sockaddr structure wrapped in a NSData object.
223 | * For example, a NSData object returned from NSNetService's addresses method.
224 | *
225 | * If you have an existing struct sockaddr you can convert it to a NSData object like so:
226 | * struct sockaddr sa -> NSData *dsa = [NSData dataWithBytes:&remoteAddr length:remoteAddr.sa_len];
227 | * struct sockaddr *sa -> NSData *dsa = [NSData dataWithBytes:remoteAddr length:remoteAddr->sa_len];
228 | *
229 | * This method invokes connectToAdd
230 | **/
231 | - (BOOL)connectToAddress:(NSData *)remoteAddr error:(NSError **)errPtr;
232 |
233 | /**
234 | * This method is the same as connectToAddress:error: with an additional timeout option.
235 | * To not time out use a negative time interval, or simply use the connectToAddress:error: method.
236 | **/
237 | - (BOOL)connectToAddress:(NSData *)remoteAddr withTimeout:(NSTimeInterval)timeout error:(NSError **)errPtr;
238 |
239 | /**
240 | * Connects to the given address, using the specified interface and timeout.
241 | *
242 | * The address is specified as a sockaddr structure wrapped in a NSData object.
243 | * For example, a NSData object returned from NSNetService's addresses method.
244 | *
245 | * If you have an existing struct sockaddr you can convert it to a NSData object like so:
246 | * struct sockaddr sa -> NSData *dsa = [NSData dataWithBytes:&remoteAddr length:remoteAddr.sa_len];
247 | * struct sockaddr *sa -> NSData *dsa = [NSData dataWithBytes:remoteAddr length:remoteAddr->sa_len];
248 | *
249 | * The interface may be a name (e.g. "en1" or "lo0") or the corresponding IP address (e.g. "192.168.4.35").
250 | * The interface may also be used to specify the local port (see below).
251 | *
252 | * The timeout is optional. To not time out use a negative time interval.
253 | *
254 | * This method will return NO if an error is detected, and set the error pointer (if one was given).
255 | * Possible errors would be a nil host, invalid interface, or socket is already connected.
256 | *
257 | * If no errors are detected, this method will start a background connect operation and immediately return YES.
258 | * The delegate callbacks are used to notify you when the socket connects, or if the host was unreachable.
259 | *
260 | * Since this class supports queued reads and writes, you can immediately start reading and/or writing.
261 | * All read/write operations will be queued, and upon socket connection,
262 | * the operations will be dequeued and processed in order.
263 | *
264 | * The interface may optionally contain a port number at the end of the string, separated by a colon.
265 | * This allows you to specify the local port that should be used for the outgoing connection. (read paragraph to end)
266 | * To specify both interface and local port: "en1:8082" or "192.168.4.35:2424".
267 | * To specify only local port: ":8082".
268 | * Please note this is an advanced feature, and is somewhat hidden on purpose.
269 | * You should understand that 99.999% of the time you should NOT specify the local port for an outgoing connection.
270 | * If you think you need to, there is a very good chance you have a fundamental misunderstanding somewhere.
271 | * Local ports do NOT need to match remote ports. In fact, they almost never do.
272 | * This feature is here for networking professionals using very advanced techniques.
273 | **/
274 | - (BOOL)connectToAddress:(NSData *)remoteAddr
275 | viaInterface:(NSString *)interface
276 | withTimeout:(NSTimeInterval)timeout
277 | error:(NSError **)errPtr;
278 |
279 | #pragma mark Disconnecting
280 |
281 | /**
282 | * Disconnects immediately (synchronously). Any pending reads or writes are dropped.
283 | *
284 | * If the socket is not already disconnected, an invocation to the socketDidDisconnect:withError: delegate method
285 | * will be queued onto the delegateQueue asynchronously (behind any previously queued delegate methods).
286 | * In other words, the disconnected delegate method will be invoked sometime shortly after this method returns.
287 | *
288 | * Please note the recommended way of releasing a GCDAsyncSocket instance (e.g. in a dealloc method)
289 | * [asyncSocket setDelegate:nil];
290 | * [asyncSocket disconnect];
291 | * [asyncSocket release];
292 | *
293 | * If you plan on disconnecting the socket, and then immediately asking it to connect again,
294 | * you'll likely want to do so like this:
295 | * [asyncSocket setDelegate:nil];
296 | * [asyncSocket disconnect];
297 | * [asyncSocket setDelegate:self];
298 | * [asyncSocket connect...];
299 | **/
300 | - (void)disconnect;
301 |
302 | /**
303 | * Disconnects after all pending reads have completed.
304 | * After calling this, the read and write methods will do nothing.
305 | * The socket will disconnect even if there are still pending writes.
306 | **/
307 | - (void)disconnectAfterReading;
308 |
309 | /**
310 | * Disconnects after all pending writes have completed.
311 | * After calling this, the read and write methods will do nothing.
312 | * The socket will disconnect even if there are still pending reads.
313 | **/
314 | - (void)disconnectAfterWriting;
315 |
316 | /**
317 | * Disconnects after all pending reads and writes have completed.
318 | * After calling this, the read and write methods will do nothing.
319 | **/
320 | - (void)disconnectAfterReadingAndWriting;
321 |
322 | #pragma mark Diagnostics
323 |
324 | /**
325 | * Returns whether the socket is disconnected or connected.
326 | *
327 | * A disconnected socket may be recycled.
328 | * That is, it can used again for connecting or listening.
329 | *
330 | * If a socket is in the process of connecting, it may be neither disconnected nor connected.
331 | **/
332 | @property (atomic, readonly) BOOL isDisconnected;
333 | @property (atomic, readonly) BOOL isConnected;
334 |
335 | /**
336 | * Returns the local or remote host and port to which this socket is connected, or nil and 0 if not connected.
337 | * The host will be an IP address.
338 | **/
339 | @property (atomic, readonly) NSString *connectedHost;
340 | @property (atomic, readonly) uint16_t connectedPort;
341 |
342 | @property (atomic, readonly) NSString *localHost;
343 | @property (atomic, readonly) uint16_t localPort;
344 |
345 | /**
346 | * Returns the local or remote address to which this socket is connected,
347 | * specified as a sockaddr structure wrapped in a NSData object.
348 | *
349 | * @seealso connectedHost
350 | * @seealso connectedPort
351 | * @seealso localHost
352 | * @seealso localPort
353 | **/
354 | @property (atomic, readonly) NSData *connectedAddress;
355 | @property (atomic, readonly) NSData *localAddress;
356 |
357 | /**
358 | * Returns whether the socket is IPv4 or IPv6.
359 | * An accepting socket may be both.
360 | **/
361 | @property (atomic, readonly) BOOL isIPv4;
362 | @property (atomic, readonly) BOOL isIPv6;
363 |
364 | /**
365 | * Returns whether or not the socket has been secured via SSL/TLS.
366 | *
367 | * See also the startTLS method.
368 | **/
369 | @property (atomic, readonly) BOOL isSecure;
370 |
371 | #pragma mark Reading
372 |
373 | // The readData and writeData methods won't block (they are asynchronous).
374 | //
375 | // When a read is complete the socket:didReadData:withTag: delegate method is dispatched on the delegateQueue.
376 | // When a write is complete the socket:didWriteDataWithTag: delegate method is dispatched on the delegateQueue.
377 | //
378 | // You may optionally set a timeout for any read/write operation. (To not timeout, use a negative time interval.)
379 | // If a read/write opertion times out, the corresponding "socket:shouldTimeout..." delegate method
380 | // is called to optionally allow you to extend the timeout.
381 | // Upon a timeout, the "socket:didDisconnectWithError:" method is called
382 | //
383 | // The tag is for your convenience.
384 | // You can use it as an array index, step number, state id, pointer, etc.
385 |
386 | /**
387 | * Reads the first available bytes that become available on the socket.
388 | *
389 | * If the timeout value is negative, the read operation will not use a timeout.
390 | **/
391 | - (void)readDataWithTimeout:(NSTimeInterval)timeout tag:(long)tag;
392 |
393 | /**
394 | * Reads the first available bytes that become available on the socket.
395 | * The bytes will be appended to the given byte buffer starting at the given offset.
396 | * The given buffer will automatically be increased in size if needed.
397 | *
398 | * If the timeout value is negative, the read operation will not use a timeout.
399 | * If the buffer if nil, the socket will create a buffer for you.
400 | *
401 | * If the bufferOffset is greater than the length of the given buffer,
402 | * the method will do nothing, and the delegate will not be called.
403 | *
404 | * If you pass a buffer, you must not alter it in any way while the socket is using it.
405 | * After completion, the data returned in socket:didReadData:withTag: will be a subset of the given buffer.
406 | * That is, it will reference the bytes that were appended to the given buffer via
407 | * the method [NSData dataWithBytesNoCopy:length:freeWhenDone:NO].
408 | **/
409 | - (void)readDataWithTimeout:(NSTimeInterval)timeout
410 | buffer:(NSMutableData *)buffer
411 | bufferOffset:(NSUInteger)offset
412 | tag:(long)tag;
413 |
414 | /**
415 | * Reads the first available bytes that become available on the socket.
416 | * The bytes will be appended to the given byte buffer starting at the given offset.
417 | * The given buffer will automatically be increased in size if needed.
418 | * A maximum of length bytes will be read.
419 | *
420 | * If the timeout value is negative, the read operation will not use a timeout.
421 | * If the buffer if nil, a buffer will automatically be created for you.
422 | * If maxLength is zero, no length restriction is enforced.
423 | *
424 | * If the bufferOffset is greater than the length of the given buffer,
425 | * the method will do nothing, and the delegate will not be called.
426 | *
427 | * If you pass a buffer, you must not alter it in any way while the socket is using it.
428 | * After completion, the data returned in socket:didReadData:withTag: will be a subset of the given buffer.
429 | * That is, it will reference the bytes that were appended to the given buffer via
430 | * the method [NSData dataWithBytesNoCopy:length:freeWhenDone:NO].
431 | **/
432 | - (void)readDataWithTimeout:(NSTimeInterval)timeout
433 | buffer:(NSMutableData *)buffer
434 | bufferOffset:(NSUInteger)offset
435 | maxLength:(NSUInteger)length
436 | tag:(long)tag;
437 |
438 | /**
439 | * Reads the given number of bytes.
440 | *
441 | * If the timeout value is negative, the read operation will not use a timeout.
442 | *
443 | * If the length is 0, this method does nothing and the delegate is not called.
444 | **/
445 | - (void)readDataToLength:(NSUInteger)length withTimeout:(NSTimeInterval)timeout tag:(long)tag;
446 |
447 | /**
448 | * Reads the given number of bytes.
449 | * The bytes will be appended to the given byte buffer starting at the given offset.
450 | * The given buffer will automatically be increased in size if needed.
451 | *
452 | * If the timeout value is negative, the read operation will not use a timeout.
453 | * If the buffer if nil, a buffer will automatically be created for you.
454 | *
455 | * If the length is 0, this method does nothing and the delegate is not called.
456 | * If the bufferOffset is greater than the length of the given buffer,
457 | * the method will do nothing, and the delegate will not be called.
458 | *
459 | * If you pass a buffer, you must not alter it in any way while AsyncSocket is using it.
460 | * After completion, the data returned in socket:didReadData:withTag: will be a subset of the given buffer.
461 | * That is, it will reference the bytes that were appended to the given buffer via
462 | * the method [NSData dataWithBytesNoCopy:length:freeWhenDone:NO].
463 | **/
464 | - (void)readDataToLength:(NSUInteger)length
465 | withTimeout:(NSTimeInterval)timeout
466 | buffer:(NSMutableData *)buffer
467 | bufferOffset:(NSUInteger)offset
468 | tag:(long)tag;
469 |
470 | /**
471 | * Reads bytes until (and including) the passed "data" parameter, which acts as a separator.
472 | *
473 | * If the timeout value is negative, the read operation will not use a timeout.
474 | *
475 | * If you pass nil or zero-length data as the "data" parameter,
476 | * the method will do nothing (except maybe print a warning), and the delegate will not be called.
477 | *
478 | * To read a line from the socket, use the line separator (e.g. CRLF for HTTP, see below) as the "data" parameter.
479 | * If you're developing your own custom protocol, be sure your separator can not occur naturally as
480 | * part of the data between separators.
481 | * For example, imagine you want to send several small documents over a socket.
482 | * Using CRLF as a separator is likely unwise, as a CRLF could easily exist within the documents.
483 | * In this particular example, it would be better to use a protocol similar to HTTP with
484 | * a header that includes the length of the document.
485 | * Also be careful that your separator cannot occur naturally as part of the encoding for a character.
486 | *
487 | * The given data (separator) parameter should be immutable.
488 | * For performance reasons, the socket will retain it, not copy it.
489 | * So if it is immutable, don't modify it while the socket is using it.
490 | **/
491 | - (void)readDataToData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;
492 |
493 | /**
494 | * Reads bytes until (and including) the passed "data" parameter, which acts as a separator.
495 | * The bytes will be appended to the given byte buffer starting at the given offset.
496 | * The given buffer will automatically be increased in size if needed.
497 | *
498 | * If the timeout value is negative, the read operation will not use a timeout.
499 | * If the buffer if nil, a buffer will automatically be created for you.
500 | *
501 | * If the bufferOffset is greater than the length of the given buffer,
502 | * the method will do nothing (except maybe print a warning), and the delegate will not be called.
503 | *
504 | * If you pass a buffer, you must not alter it in any way while the socket is using it.
505 | * After completion, the data returned in socket:didReadData:withTag: will be a subset of the given buffer.
506 | * That is, it will reference the bytes that were appended to the given buffer via
507 | * the method [NSData dataWithBytesNoCopy:length:freeWhenDone:NO].
508 | *
509 | * To read a line from the socket, use the line separator (e.g. CRLF for HTTP, see below) as the "data" parameter.
510 | * If you're developing your own custom protocol, be sure your separator can not occur naturally as
511 | * part of the data between separators.
512 | * For example, imagine you want to send several small documents over a socket.
513 | * Using CRLF as a separator is likely unwise, as a CRLF could easily exist within the documents.
514 | * In this particular example, it would be better to use a protocol similar to HTTP with
515 | * a header that includes the length of the document.
516 | * Also be careful that your separator cannot occur naturally as part of the encoding for a character.
517 | *
518 | * The given data (separator) parameter should be immutable.
519 | * For performance reasons, the socket will retain it, not copy it.
520 | * So if it is immutable, don't modify it while the socket is using it.
521 | **/
522 | - (void)readDataToData:(NSData *)data
523 | withTimeout:(NSTimeInterval)timeout
524 | buffer:(NSMutableData *)buffer
525 | bufferOffset:(NSUInteger)offset
526 | tag:(long)tag;
527 |
528 | /**
529 | * Reads bytes until (and including) the passed "data" parameter, which acts as a separator.
530 | *
531 | * If the timeout value is negative, the read operation will not use a timeout.
532 | *
533 | * If maxLength is zero, no length restriction is enforced.
534 | * Otherwise if maxLength bytes are read without completing the read,
535 | * it is treated similarly to a timeout - the socket is closed with a GCDAsyncSocketReadMaxedOutError.
536 | * The read will complete successfully if exactly maxLength bytes are read and the given data is found at the end.
537 | *
538 | * If you pass nil or zero-length data as the "data" parameter,
539 | * the method will do nothing (except maybe print a warning), and the delegate will not be called.
540 | * If you pass a maxLength parameter that is less than the length of the data parameter,
541 | * the method will do nothing (except maybe print a warning), and the delegate will not be called.
542 | *
543 | * To read a line from the socket, use the line separator (e.g. CRLF for HTTP, see below) as the "data" parameter.
544 | * If you're developing your own custom protocol, be sure your separator can not occur naturally as
545 | * part of the data between separators.
546 | * For example, imagine you want to send several small documents over a socket.
547 | * Using CRLF as a separator is likely unwise, as a CRLF could easily exist within the documents.
548 | * In this particular example, it would be better to use a protocol similar to HTTP with
549 | * a header that includes the length of the document.
550 | * Also be careful that your separator cannot occur naturally as part of the encoding for a character.
551 | *
552 | * The given data (separator) parameter should be immutable.
553 | * For performance reasons, the socket will retain it, not copy it.
554 | * So if it is immutable, don't modify it while the socket is using it.
555 | **/
556 | - (void)readDataToData:(NSData *)data withTimeout:(NSTimeInterval)timeout maxLength:(NSUInteger)length tag:(long)tag;
557 |
558 | /**
559 | * Reads bytes until (and including) the passed "data" parameter, which acts as a separator.
560 | * The bytes will be appended to the given byte buffer starting at the given offset.
561 | * The given buffer will automatically be increased in size if needed.
562 | *
563 | * If the timeout value is negative, the read operation will not use a timeout.
564 | * If the buffer if nil, a buffer will automatically be created for you.
565 | *
566 | * If maxLength is zero, no length restriction is enforced.
567 | * Otherwise if maxLength bytes are read without completing the read,
568 | * it is treated similarly to a timeout - the socket is closed with a GCDAsyncSocketReadMaxedOutError.
569 | * The read will complete successfully if exactly maxLength bytes are read and the given data is found at the end.
570 | *
571 | * If you pass a maxLength parameter that is less than the length of the data (separator) parameter,
572 | * the method will do nothing (except maybe print a warning), and the delegate will not be called.
573 | * If the bufferOffset is greater than the length of the given buffer,
574 | * the method will do nothing (except maybe print a warning), and the delegate will not be called.
575 | *
576 | * If you pass a buffer, you must not alter it in any way while the socket is using it.
577 | * After completion, the data returned in socket:didReadData:withTag: will be a subset of the given buffer.
578 | * That is, it will reference the bytes that were appended to the given buffer via
579 | * the method [NSData dataWithBytesNoCopy:length:freeWhenDone:NO].
580 | *
581 | * To read a line from the socket, use the line separator (e.g. CRLF for HTTP, see below) as the "data" parameter.
582 | * If you're developing your own custom protocol, be sure your separator can not occur naturally as
583 | * part of the data between separators.
584 | * For example, imagine you want to send several small documents over a socket.
585 | * Using CRLF as a separator is likely unwise, as a CRLF could easily exist within the documents.
586 | * In this particular example, it would be better to use a protocol similar to HTTP with
587 | * a header that includes the length of the document.
588 | * Also be careful that your separator cannot occur naturally as part of the encoding for a character.
589 | *
590 | * The given data (separator) parameter should be immutable.
591 | * For performance reasons, the socket will retain it, not copy it.
592 | * So if it is immutable, don't modify it while the socket is using it.
593 | **/
594 | - (void)readDataToData:(NSData *)data
595 | withTimeout:(NSTimeInterval)timeout
596 | buffer:(NSMutableData *)buffer
597 | bufferOffset:(NSUInteger)offset
598 | maxLength:(NSUInteger)length
599 | tag:(long)tag;
600 |
601 | /**
602 | * Returns progress of the current read, from 0.0 to 1.0, or NaN if no current read (use isnan() to check).
603 | * The parameters "tag", "done" and "total" will be filled in if they aren't NULL.
604 | **/
605 | - (float)progressOfReadReturningTag:(long *)tagPtr bytesDone:(NSUInteger *)donePtr total:(NSUInteger *)totalPtr;
606 |
607 | #pragma mark Writing
608 |
609 | /**
610 | * Writes data to the socket, and calls the delegate when finished.
611 | *
612 | * If you pass in nil or zero-length data, this method does nothing and the delegate will not be called.
613 | * If the timeout value is negative, the write operation will not use a timeout.
614 | *
615 | * Thread-Safety Note:
616 | * If the given data parameter is mutable (NSMutableData) then you MUST NOT alter the data while
617 | * the socket is writing it. In other words, it's not safe to alter the data until after the delegate method
618 | * socket:didWriteDataWithTag: is invoked signifying that this particular write operation has completed.
619 | * This is due to the fact that GCDAsyncSocket does NOT copy the data. It simply retains it.
620 | * This is for performance reasons. Often times, if NSMutableData is passed, it is because
621 | * a request/response was built up in memory. Copying this data adds an unwanted/unneeded overhead.
622 | * If you need to write data from an immutable buffer, and you need to alter the buffer before the socket
623 | * completes writing the bytes (which is NOT immediately after this method returns, but rather at a later time
624 | * when the delegate method notifies you), then you should first copy the bytes, and pass the copy to this method.
625 | **/
626 | - (void)writeData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;
627 |
628 | /**
629 | * Returns progress of the current write, from 0.0 to 1.0, or NaN if no current write (use isnan() to check).
630 | * The parameters "tag", "done" and "total" will be filled in if they aren't NULL.
631 | **/
632 | - (float)progressOfWriteReturningTag:(long *)tagPtr bytesDone:(NSUInteger *)donePtr total:(NSUInteger *)totalPtr;
633 |
634 | #pragma mark Security
635 |
636 | /**
637 | * Secures the connection using SSL/TLS.
638 | *
639 | * This method may be called at any time, and the TLS handshake will occur after all pending reads and writes
640 | * are finished. This allows one the option of sending a protocol dependent StartTLS message, and queuing
641 | * the upgrade to TLS at the same time, without having to wait for the write to finish.
642 | * Any reads or writes scheduled after this method is called will occur over the secured connection.
643 | *
644 | * ==== The available TOP-LEVEL KEYS are:
645 | *
646 | * - GCDAsyncSocketManuallyEvaluateTrust
647 | * The value must be of type NSNumber, encapsulating a BOOL value.
648 | * If you set this to YES, then the underlying SecureTransport system will not evaluate the SecTrustRef of the peer.
649 | * Instead it will pause at the moment evaulation would typically occur,
650 | * and allow us to handle the security evaluation however we see fit.
651 | * So GCDAsyncSocket will invoke the delegate method socket:shouldTrustPeer: passing the SecTrustRef.
652 | *
653 | * Note that if you set this option, then all other configuration keys are ignored.
654 | * Evaluation will be completely up to you during the socket:didReceiveTrust:completionHandler: delegate method.
655 | *
656 | * For more information on trust evaluation see:
657 | * Apple's Technical Note TN2232 - HTTPS Server Trust Evaluation
658 | * https://developer.apple.com/library/ios/technotes/tn2232/_index.html
659 | *
660 | * If unspecified, the default value is NO.
661 | *
662 | * - GCDAsyncSocketUseCFStreamForTLS (iOS only)
663 | * The value must be of type NSNumber, encapsulating a BOOL value.
664 | * By default GCDAsyncSocket will use the SecureTransport layer to perform encryption.
665 | * This gives us more control over the security protocol (many more configuration options),
666 | * plus it allows us to optimize things like sys calls and buffer allocation.
667 | *
668 | * However, if you absolutely must, you can instruct GCDAsyncSocket to use the old-fashioned encryption
669 | * technique by going through the CFStream instead. So instead of using SecureTransport, GCDAsyncSocket
670 | * will instead setup a CFRead/CFWriteStream. And then set the kCFStreamPropertySSLSettings property
671 | * (via CFReadStreamSetProperty / CFWriteStreamSetProperty) and will pass the given options to this method.
672 | *
673 | * Thus all the other keys in the given dictionary will be ignored by GCDAsyncSocket,
674 | * and will passed directly CFReadStreamSetProperty / CFWriteStreamSetProperty.
675 | * For more infomation on these keys, please see the documentation for kCFStreamPropertySSLSettings.
676 | *
677 | * If unspecified, the default value is NO.
678 | *
679 | * ==== The available CONFIGURATION KEYS are:
680 | *
681 | * - kCFStreamSSLPeerName
682 | * The value must be of type NSString.
683 | * It should match the name in the X.509 certificate given by the remote party.
684 | * See Apple's documentation for SSLSetPeerDomainName.
685 | *
686 | * - kCFStreamSSLCertificates
687 | * The value must be of type NSArray.
688 | * See Apple's documentation for SSLSetCertificate.
689 | *
690 | * - kCFStreamSSLIsServer
691 | * The value must be of type NSNumber, encapsulationg a BOOL value.
692 | * See Apple's documentation for SSLCreateContext for iOS.
693 | * This is optional for iOS. If not supplied, a NO value is the default.
694 | * This is not needed for Mac OS X, and the value is ignored.
695 | *
696 | * - GCDAsyncSocketSSLPeerID
697 | * The value must be of type NSData.
698 | * You must set this value if you want to use TLS session resumption.
699 | * See Apple's documentation for SSLSetPeerID.
700 | *
701 | * - GCDAsyncSocketSSLProtocolVersionMin
702 | * - GCDAsyncSocketSSLProtocolVersionMax
703 | * The value(s) must be of type NSNumber, encapsulting a SSLProtocol value.
704 | * See Apple's documentation for SSLSetProtocolVersionMin & SSLSetProtocolVersionMax.
705 | * See also the SSLProtocol typedef.
706 | *
707 | * - GCDAsyncSocketSSLSessionOptionFalseStart
708 | * The value must be of type NSNumber, encapsulating a BOOL value.
709 | * See Apple's documentation for kSSLSessionOptionFalseStart.
710 | *
711 | * - GCDAsyncSocketSSLSessionOptionSendOneByteRecord
712 | * The value must be of type NSNumber, encapsulating a BOOL value.
713 | * See Apple's documentation for kSSLSessionOptionSendOneByteRecord.
714 | *
715 | * - GCDAsyncSocketSSLCipherSuites
716 | * The values must be of type NSArray.
717 | * Each item within the array must be a NSNumber, encapsulating
718 | * See Apple's documentation for SSLSetEnabledCiphers.
719 | * See also the SSLCipherSuite typedef.
720 | *
721 | * - GCDAsyncSocketSSLDiffieHellmanParameters (Mac OS X only)
722 | * The value must be of type NSData.
723 | * See Apple's documentation for SSLSetDiffieHellmanParams.
724 | *
725 | * ==== The following UNAVAILABLE KEYS are: (with throw an exception)
726 | *
727 | * - kCFStreamSSLAllowsAnyRoot (UNAVAILABLE)
728 | * You MUST use manual trust evaluation instead (see GCDAsyncSocketManuallyEvaluateTrust).
729 | * Corresponding deprecated method: SSLSetAllowsAnyRoot
730 | *
731 | * - kCFStreamSSLAllowsExpiredRoots (UNAVAILABLE)
732 | * You MUST use manual trust evaluation instead (see GCDAsyncSocketManuallyEvaluateTrust).
733 | * Corresponding deprecated method: SSLSetAllowsExpiredRoots
734 | *
735 | * - kCFStreamSSLAllowsExpiredCertificates (UNAVAILABLE)
736 | * You MUST use manual trust evaluation instead (see GCDAsyncSocketManuallyEvaluateTrust).
737 | * Corresponding deprecated method: SSLSetAllowsExpiredCerts
738 | *
739 | * - kCFStreamSSLValidatesCertificateChain (UNAVAILABLE)
740 | * You MUST use manual trust evaluation instead (see GCDAsyncSocketManuallyEvaluateTrust).
741 | * Corresponding deprecated method: SSLSetEnableCertVerify
742 | *
743 | * - kCFStreamSSLLevel (UNAVAILABLE)
744 | * You MUST use GCDAsyncSocketSSLProtocolVersionMin & GCDAsyncSocketSSLProtocolVersionMin instead.
745 | * Corresponding deprecated method: SSLSetProtocolVersionEnabled
746 | *
747 | *
748 | * Please refer to Apple's documentation for corresponding SSLFunctions.
749 | *
750 | * If you pass in nil or an empty dictionary, the default settings will be used.
751 | *
752 | * IMPORTANT SECURITY NOTE:
753 | * The default settings will check to make sure the remote party's certificate is signed by a
754 | * trusted 3rd party certificate agency (e.g. verisign) and that the certificate is not expired.
755 | * However it will not verify the name on the certificate unless you
756 | * give it a name to verify against via the kCFStreamSSLPeerName key.
757 | * The security implications of this are important to understand.
758 | * Imagine you are attempting to create a secure connection to MySecureServer.com,
759 | * but your socket gets directed to MaliciousServer.com because of a hacked DNS server.
760 | * If you simply use the default settings, and MaliciousServer.com has a valid certificate,
761 | * the default settings will not detect any problems since the certificate is valid.
762 | * To properly secure your connection in this particular scenario you
763 | * should set the kCFStreamSSLPeerName property to "MySecureServer.com".
764 | *
765 | * You can also perform additional validation in socketDidSecure.
766 | **/
767 | - (void)startTLS:(NSDictionary *)tlsSettings;
768 |
769 | #pragma mark Advanced
770 |
771 | /**
772 | * Traditionally sockets are not closed until the conversation is over.
773 | * However, it is technically possible for the remote enpoint to close its write stream.
774 | * Our socket would then be notified that there is no more data to be read,
775 | * but our socket would still be writeable and the remote endpoint could continue to receive our data.
776 | *
777 | * The argument for this confusing functionality stems from the idea that a client could shut down its
778 | * write stream after sending a request to the server, thus notifying the server there are to be no further requests.
779 | * In practice, however, this technique did little to help server developers.
780 | *
781 | * To make matters worse, from a TCP perspective there is no way to tell the difference from a read stream close
782 | * and a full socket close. They both result in the TCP stack receiving a FIN packet. The only way to tell
783 | * is by continuing to write to the socket. If it was only a read stream close, then writes will continue to work.
784 | * Otherwise an error will be occur shortly (when the remote end sends us a RST packet).
785 | *
786 | * In addition to the technical challenges and confusion, many high level socket/stream API's provide
787 | * no support for dealing with the problem. If the read stream is closed, the API immediately declares the
788 | * socket to be closed, and shuts down the write stream as well. In fact, this is what Apple's CFStream API does.
789 | * It might sound like poor design at first, but in fact it simplifies development.
790 | *
791 | * The vast majority of the time if the read stream is closed it's because the remote endpoint closed its socket.
792 | * Thus it actually makes sense to close the socket at this point.
793 | * And in fact this is what most networking developers want and expect to happen.
794 | * However, if you are writing a server that interacts with a plethora of clients,
795 | * you might encounter a client that uses the discouraged technique of shutting down its write stream.
796 | * If this is the case, you can set this property to NO,
797 | * and make use of the socketDidCloseReadStream delegate method.
798 | *
799 | * The default value is YES.
800 | **/
801 | @property (atomic, assign, readwrite) BOOL autoDisconnectOnClosedReadStream;
802 |
803 | /**
804 | * GCDAsyncSocket maintains thread safety by using an internal serial dispatch_queue.
805 | * In most cases, the instance creates this queue itself.
806 | * However, to allow for maximum flexibility, the internal queue may be passed in the init method.
807 | * This allows for some advanced options such as controlling socket priority via target queues.
808 | * However, when one begins to use target queues like this, they open the door to some specific deadlock issues.
809 | *
810 | * For example, imagine there are 2 queues:
811 | * dispatch_queue_t socketQueue;
812 | * dispatch_queue_t socketTargetQueue;
813 | *
814 | * If you do this (pseudo-code):
815 | * socketQueue.targetQueue = socketTargetQueue;
816 | *
817 | * Then all socketQueue operations will actually get run on the given socketTargetQueue.
818 | * This is fine and works great in most situations.
819 | * But if you run code directly from within the socketTargetQueue that accesses the socket,
820 | * you could potentially get deadlock. Imagine the following code:
821 | *
822 | * - (BOOL)socketHasSomething
823 | * {
824 | * __block BOOL result = NO;
825 | * dispatch_block_t block = ^{
826 | * result = [self someInternalMethodToBeRunOnlyOnSocketQueue];
827 | * }
828 | * if (is_executing_on_queue(socketQueue))
829 | * block();
830 | * else
831 | * dispatch_sync(socketQueue, block);
832 | *
833 | * return result;
834 | * }
835 | *
836 | * What happens if you call this method from the socketTargetQueue? The result is deadlock.
837 | * This is because the GCD API offers no mechanism to discover a queue's targetQueue.
838 | * Thus we have no idea if our socketQueue is configured with a targetQueue.
839 | * If we had this information, we could easily avoid deadlock.
840 | * But, since these API's are missing or unfeasible, you'll have to explicitly set it.
841 | *
842 | * IF you pass a socketQueue via the init method,
843 | * AND you've configured the passed socketQueue with a targetQueue,
844 | * THEN you should pass the end queue in the target hierarchy.
845 | *
846 | * For example, consider the following queue hierarchy:
847 | * socketQueue -> ipQueue -> moduleQueue
848 | *
849 | * This example demonstrates priority shaping within some server.
850 | * All incoming client connections from the same IP address are executed on the same target queue.
851 | * And all connections for a particular module are executed on the same target queue.
852 | * Thus, the priority of all networking for the entire module can be changed on the fly.
853 | * Additionally, networking traffic from a single IP cannot monopolize the module.
854 | *
855 | * Here's how you would accomplish something like that:
856 | * - (dispatch_queue_t)newSocketQueueForConnectionFromAddress:(NSData *)address onSocket:(GCDAsyncSocket *)sock
857 | * {
858 | * dispatch_queue_t socketQueue = dispatch_queue_create("", NULL);
859 | * dispatch_queue_t ipQueue = [self ipQueueForAddress:address];
860 | *
861 | * dispatch_set_target_queue(socketQueue, ipQueue);
862 | * dispatch_set_target_queue(iqQueue, moduleQueue);
863 | *
864 | * return socketQueue;
865 | * }
866 | * - (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket
867 | * {
868 | * [clientConnections addObject:newSocket];
869 | * [newSocket markSocketQueueTargetQueue:moduleQueue];
870 | * }
871 | *
872 | * Note: This workaround is ONLY needed if you intend to execute code directly on the ipQueue or moduleQueue.
873 | * This is often NOT the case, as such queues are used solely for execution shaping.
874 | **/
875 | - (void)markSocketQueueTargetQueue:(dispatch_queue_t)socketQueuesPreConfiguredTargetQueue;
876 | - (void)unmarkSocketQueueTargetQueue:(dispatch_queue_t)socketQueuesPreviouslyConfiguredTargetQueue;
877 |
878 | /**
879 | * It's not thread-safe to access certain variables from outside the socket's internal queue.
880 | *
881 | * For example, the socket file descriptor.
882 | * File descriptors are simply integers which reference an index in the per-process file table.
883 | * However, when one requests a new file descriptor (by opening a file or socket),
884 | * the file descriptor returned is guaranteed to be the lowest numbered unused descriptor.
885 | * So if we're not careful, the following could be possible:
886 | *
887 | * - Thread A invokes a method which returns the socket's file descriptor.
888 | * - The socket is closed via the socket's internal queue on thread B.
889 | * - Thread C opens a file, and subsequently receives the file descriptor that was previously the socket's FD.
890 | * - Thread A is now accessing/altering the file instead of the socket.
891 | *
892 | * In addition to this, other variables are not actually objects,
893 | * and thus cannot be retained/released or even autoreleased.
894 | * An example is the sslContext, of type SSLContextRef, which is actually a malloc'd struct.
895 | *
896 | * Although there are internal variables that make it difficult to maintain thread-safety,
897 | * it is important to provide access to these variables
898 | * to ensure this class can be used in a wide array of environments.
899 | * This method helps to accomplish this by invoking the current block on the socket's internal queue.
900 | * The methods below can be invoked from within the block to access
901 | * those generally thread-unsafe internal variables in a thread-safe manner.
902 | * The given block will be invoked synchronously on the socket's internal queue.
903 | *
904 | * If you save references to any protected variables and use them outside the block, you do so at your own peril.
905 | **/
906 | - (void)performBlock:(dispatch_block_t)block;
907 |
908 | /**
909 | * These methods are only available from within the context of a performBlock: invocation.
910 | * See the documentation for the performBlock: method above.
911 | *
912 | * Provides access to the socket's file descriptor(s).
913 | * If the socket is a server socket (is accepting incoming connections),
914 | * it might actually have multiple internal socket file descriptors - one for IPv4 and one for IPv6.
915 | **/
916 | - (int)socketFD;
917 | - (int)socket4FD;
918 | - (int)socket6FD;
919 |
920 | #if TARGET_OS_IPHONE
921 |
922 | /**
923 | * These methods are only available from within the context of a performBlock: invocation.
924 | * See the documentation for the performBlock: method above.
925 | *
926 | * Provides access to the socket's internal CFReadStream/CFWriteStream.
927 | *
928 | * These streams are only used as workarounds for specific iOS shortcomings:
929 | *
930 | * - Apple has decided to keep the SecureTransport framework private is iOS.
931 | * This means the only supplied way to do SSL/TLS is via CFStream or some other API layered on top of it.
932 | * Thus, in order to provide SSL/TLS support on iOS we are forced to rely on CFStream,
933 | * instead of the preferred and faster and more powerful SecureTransport.
934 | *
935 | * - If a socket doesn't have backgrounding enabled, and that socket is closed while the app is backgrounded,
936 | * Apple only bothers to notify us via the CFStream API.
937 | * The faster and more powerful GCD API isn't notified properly in this case.
938 | *
939 | * See also: (BOOL)enableBackgroundingOnSocket
940 | **/
941 | - (CFReadStreamRef)readStream;
942 | - (CFWriteStreamRef)writeStream;
943 |
944 | /**
945 | * This method is only available from within the context of a performBlock: invocation.
946 | * See the documentation for the performBlock: method above.
947 | *
948 | * Configures the socket to allow it to operate when the iOS application has been backgrounded.
949 | * In other words, this method creates a read & write stream, and invokes:
950 | *
951 | * CFReadStreamSetProperty(readStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
952 | * CFWriteStreamSetProperty(writeStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
953 | *
954 | * Returns YES if successful, NO otherwise.
955 | *
956 | * Note: Apple does not officially support backgrounding server sockets.
957 | * That is, if your socket is accepting incoming connections, Apple does not officially support
958 | * allowing iOS applications to accept incoming connections while an app is backgrounded.
959 | *
960 | * Example usage:
961 | *
962 | * - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port
963 | * {
964 | * [asyncSocket performBlock:^{
965 | * [asyncSocket enableBackgroundingOnSocket];
966 | * }];
967 | * }
968 | **/
969 | - (BOOL)enableBackgroundingOnSocket;
970 |
971 | #endif
972 |
973 | /**
974 | * This method is only available from within the context of a performBlock: invocation.
975 | * See the documentation for the performBlock: method above.
976 | *
977 | * Provides access to the socket's SSLContext, if SSL/TLS has been started on the socket.
978 | **/
979 | - (SSLContextRef)sslContext;
980 |
981 | #pragma mark Utilities
982 |
983 | /**
984 | * The address lookup utility used by the class.
985 | * This method is synchronous, so it's recommended you use it on a background thread/queue.
986 | *
987 | * The special strings "localhost" and "loopback" return the loopback address for IPv4 and IPv6.
988 | *
989 | * @returns
990 | * A mutable array with all IPv4 and IPv6 addresses returned by getaddrinfo.
991 | * The addresses are specifically for TCP connections.
992 | * You can filter the addresses, if needed, using the other utility methods provided by the class.
993 | **/
994 | + (NSMutableArray *)lookupHost:(NSString *)host port:(uint16_t)port error:(NSError **)errPtr;
995 |
996 | /**
997 | * Extracting host and port information from raw address data.
998 | **/
999 |
1000 | + (NSString *)hostFromAddress:(NSData *)address;
1001 | + (uint16_t)portFromAddress:(NSData *)address;
1002 |
1003 | + (BOOL)isIPv4Address:(NSData *)address;
1004 | + (BOOL)isIPv6Address:(NSData *)address;
1005 |
1006 | + (BOOL)getHost:(NSString **)hostPtr port:(uint16_t *)portPtr fromAddress:(NSData *)address;
1007 |
1008 | + (BOOL)getHost:(NSString **)hostPtr port:(uint16_t *)portPtr family:(sa_family_t *)afPtr fromAddress:(NSData *)address;
1009 |
1010 | /**
1011 | * A few common line separators, for use with the readDataToData:... methods.
1012 | **/
1013 | + (NSData *)CRLFData; // 0x0D0A
1014 | + (NSData *)CRData; // 0x0D
1015 | + (NSData *)LFData; // 0x0A
1016 | + (NSData *)ZeroData; // 0x00
1017 |
1018 | @end
1019 |
1020 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1021 | #pragma mark -
1022 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1023 |
1024 | @protocol GCDAsyncSocketDelegate
1025 | @optional
1026 |
1027 | /**
1028 | * This method is called immediately prior to socket:didAcceptNewSocket:.
1029 | * It optionally allows a listening socket to specify the socketQueue for a new accepted socket.
1030 | * If this method is not implemented, or returns NULL, the new accepted socket will create its own default queue.
1031 | *
1032 | * Since you cannot autorelease a dispatch_queue,
1033 | * this method uses the "new" prefix in its name to specify that the returned queue has been retained.
1034 | *
1035 | * Thus you could do something like this in the implementation:
1036 | * return dispatch_queue_create("MyQueue", NULL);
1037 | *
1038 | * If you are placing multiple sockets on the same queue,
1039 | * then care should be taken to increment the retain count each time this method is invoked.
1040 | *
1041 | * For example, your implementation might look something like this:
1042 | * dispatch_retain(myExistingQueue);
1043 | * return myExistingQueue;
1044 | **/
1045 | - (dispatch_queue_t)newSocketQueueForConnectionFromAddress:(NSData *)address onSocket:(GCDAsyncSocket *)sock;
1046 |
1047 | /**
1048 | * Called when a socket accepts a connection.
1049 | * Another socket is automatically spawned to handle it.
1050 | *
1051 | * You must retain the newSocket if you wish to handle the connection.
1052 | * Otherwise the newSocket instance will be released and the spawned connection will be closed.
1053 | *
1054 | * By default the new socket will have the same delegate and delegateQueue.
1055 | * You may, of course, change this at any time.
1056 | **/
1057 | - (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket;
1058 |
1059 | /**
1060 | * Called when a socket connects and is ready for reading and writing.
1061 | * The host parameter will be an IP address, not a DNS name.
1062 | **/
1063 | - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port;
1064 |
1065 | /**
1066 | * Called when a socket has completed reading the requested data into memory.
1067 | * Not called if there is an error.
1068 | **/
1069 | - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag;
1070 |
1071 | /**
1072 | * Called when a socket has read in data, but has not yet completed the read.
1073 | * This would occur if using readToData: or readToLength: methods.
1074 | * It may be used to for things such as updating progress bars.
1075 | **/
1076 | - (void)socket:(GCDAsyncSocket *)sock didReadPartialDataOfLength:(NSUInteger)partialLength tag:(long)tag;
1077 |
1078 | /**
1079 | * Called when a socket has completed writing the requested data. Not called if there is an error.
1080 | **/
1081 | - (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag;
1082 |
1083 | /**
1084 | * Called when a socket has written some data, but has not yet completed the entire write.
1085 | * It may be used to for things such as updating progress bars.
1086 | **/
1087 | - (void)socket:(GCDAsyncSocket *)sock didWritePartialDataOfLength:(NSUInteger)partialLength tag:(long)tag;
1088 |
1089 | /**
1090 | * Called if a read operation has reached its timeout without completing.
1091 | * This method allows you to optionally extend the timeout.
1092 | * If you return a positive time interval (> 0) the read's timeout will be extended by the given amount.
1093 | * If you don't implement this method, or return a non-positive time interval (<= 0) the read will timeout as usual.
1094 | *
1095 | * The elapsed parameter is the sum of the original timeout, plus any additions previously added via this method.
1096 | * The length parameter is the number of bytes that have been read so far for the read operation.
1097 | *
1098 | * Note that this method may be called multiple times for a single read if you return positive numbers.
1099 | **/
1100 | - (NSTimeInterval)socket:(GCDAsyncSocket *)sock shouldTimeoutReadWithTag:(long)tag
1101 | elapsed:(NSTimeInterval)elapsed
1102 | bytesDone:(NSUInteger)length;
1103 |
1104 | /**
1105 | * Called if a write operation has reached its timeout without completing.
1106 | * This method allows you to optionally extend the timeout.
1107 | * If you return a positive time interval (> 0) the write's timeout will be extended by the given amount.
1108 | * If you don't implement this method, or return a non-positive time interval (<= 0) the write will timeout as usual.
1109 | *
1110 | * The elapsed parameter is the sum of the original timeout, plus any additions previously added via this method.
1111 | * The length parameter is the number of bytes that have been written so far for the write operation.
1112 | *
1113 | * Note that this method may be called multiple times for a single write if you return positive numbers.
1114 | **/
1115 | - (NSTimeInterval)socket:(GCDAsyncSocket *)sock shouldTimeoutWriteWithTag:(long)tag
1116 | elapsed:(NSTimeInterval)elapsed
1117 | bytesDone:(NSUInteger)length;
1118 |
1119 | /**
1120 | * Conditionally called if the read stream closes, but the write stream may still be writeable.
1121 | *
1122 | * This delegate method is only called if autoDisconnectOnClosedReadStream has been set to NO.
1123 | * See the discussion on the autoDisconnectOnClosedReadStream method for more information.
1124 | **/
1125 | - (void)socketDidCloseReadStream:(GCDAsyncSocket *)sock;
1126 |
1127 | /**
1128 | * Called when a socket disconnects with or without error.
1129 | *
1130 | * If you call the disconnect method, and the socket wasn't already disconnected,
1131 | * then an invocation of this delegate method will be enqueued on the delegateQueue
1132 | * before the disconnect method returns.
1133 | *
1134 | * Note: If the GCDAsyncSocket instance is deallocated while it is still connected,
1135 | * and the delegate is not also deallocated, then this method will be invoked,
1136 | * but the sock parameter will be nil. (It must necessarily be nil since it is no longer available.)
1137 | * This is a generally rare, but is possible if one writes code like this:
1138 | *
1139 | * asyncSocket = nil; // I'm implicitly disconnecting the socket
1140 | *
1141 | * In this case it may preferrable to nil the delegate beforehand, like this:
1142 | *
1143 | * asyncSocket.delegate = nil; // Don't invoke my delegate method
1144 | * asyncSocket = nil; // I'm implicitly disconnecting the socket
1145 | *
1146 | * Of course, this depends on how your state machine is configured.
1147 | **/
1148 | - (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err;
1149 |
1150 | /**
1151 | * Called after the socket has successfully completed SSL/TLS negotiation.
1152 | * This method is not called unless you use the provided startTLS method.
1153 | *
1154 | * If a SSL/TLS negotiation fails (invalid certificate, etc) then the socket will immediately close,
1155 | * and the socketDidDisconnect:withError: delegate method will be called with the specific SSL error code.
1156 | **/
1157 | - (void)socketDidSecure:(GCDAsyncSocket *)sock;
1158 |
1159 | /**
1160 | * Allows a socket delegate to hook into the TLS handshake and manually validate the peer it's connecting to.
1161 | *
1162 | * This is only called if startTLS is invoked with options that include:
1163 | * - GCDAsyncSocketManuallyEvaluateTrust == YES
1164 | *
1165 | * Typically the delegate will use SecTrustEvaluate (and related functions) to properly validate the peer.
1166 | *
1167 | * Note from Apple's documentation:
1168 | * Because [SecTrustEvaluate] might look on the network for certificates in the certificate chain,
1169 | * [it] might block while attempting network access. You should never call it from your main thread;
1170 | * call it only from within a function running on a dispatch queue or on a separate thread.
1171 | *
1172 | * Thus this method uses a completionHandler block rather than a normal return value.
1173 | * The completionHandler block is thread-safe, and may be invoked from a background queue/thread.
1174 | * It is safe to invoke the completionHandler block even if the socket has been closed.
1175 | **/
1176 | - (void)socket:(GCDAsyncSocket *)sock didReceiveTrust:(SecTrustRef)trust
1177 | completionHandler:(void (^)(BOOL shouldTrustPeer))completionHandler;
1178 |
1179 | @end
1180 |
--------------------------------------------------------------------------------