Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 516 lines (449 sloc) 22.595 kb
60938ca built beginnings of a demonstration project to go with Blackbox
Matt Patenaude authored
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 <Foundation/Foundation.h>
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 @interface NSObject (AsyncSocketDelegate)
33
34 /**
35 * In the event of an error, the socket is closed.
36 * You may call "unreadData" during this call-back to get the last bit of data off the socket.
37 * When connecting, this delegate method may be called
38 * before"onSocket:didAcceptNewSocket:" or "onSocket:didConnectToHost:".
39 **/
40 - (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err;
41
42 /**
43 * Called when a socket disconnects with or without error. If you want to release a socket after it disconnects,
44 * do so here. It is not safe to do that during "onSocket:willDisconnectWithError:".
a927cec @mattpat Updated to latest trunk version of CocoaHTTPServer
authored
45 *
46 * If you call the disconnect method, and the socket wasn't already disconnected,
47 * this delegate method will be called before the disconnect method returns.
60938ca built beginnings of a demonstration project to go with Blackbox
Matt Patenaude authored
48 **/
49 - (void)onSocketDidDisconnect:(AsyncSocket *)sock;
50
51 /**
52 * Called when a socket accepts a connection. Another socket is spawned to handle it. The new socket will have
53 * the same delegate and will call "onSocket:didConnectToHost:port:".
54 **/
55 - (void)onSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket;
56
57 /**
58 * Called when a new socket is spawned to handle a connection. This method should return the run-loop of the
59 * thread on which the new socket and its delegate should operate. If omitted, [NSRunLoop currentRunLoop] is used.
60 **/
61 - (NSRunLoop *)onSocket:(AsyncSocket *)sock wantsRunLoopForNewSocket:(AsyncSocket *)newSocket;
62
63 /**
64 * Called when a socket is about to connect. This method should return YES to continue, or NO to abort.
65 * If aborted, will result in AsyncSocketCanceledError.
66 *
67 * If the connectToHost:onPort:error: method was called, the delegate will be able to access and configure the
68 * CFReadStream and CFWriteStream as desired prior to connection.
69 *
70 * If the connectToAddress:error: method was called, the delegate will be able to access and configure the
71 * CFSocket and CFSocketNativeHandle (BSD socket) as desired prior to connection. You will be able to access and
72 * configure the CFReadStream and CFWriteStream in the onSocket:didConnectToHost:port: method.
73 **/
74 - (BOOL)onSocketWillConnect:(AsyncSocket *)sock;
75
76 /**
77 * Called when a socket connects and is ready for reading and writing.
78 * The host parameter will be an IP address, not a DNS name.
79 **/
80 - (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port;
81
82 /**
83 * Called when a socket has completed reading the requested data into memory.
84 * Not called if there is an error.
85 **/
86 - (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag;
87
88 /**
89 * Called when a socket has read in data, but has not yet completed the read.
90 * This would occur if using readToData: or readToLength: methods.
91 * It may be used to for things such as updating progress bars.
92 **/
93 - (void)onSocket:(AsyncSocket *)sock didReadPartialDataOfLength:(CFIndex)partialLength tag:(long)tag;
94
95 /**
96 * Called when a socket has completed writing the requested data. Not called if there is an error.
97 **/
98 - (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag;
99
100 /**
a927cec @mattpat Updated to latest trunk version of CocoaHTTPServer
authored
101 * Called when a socket has written some data, but has not yet completed the entire write.
102 * It may be used to for things such as updating progress bars.
103 **/
104 - (void)onSocket:(AsyncSocket *)sock didWritePartialDataOfLength:(CFIndex)partialLength tag:(long)tag;
105
106 /**
107 * Called if a read operation has reached its timeout without completing.
108 * This method allows you to optionally extend the timeout.
109 * If you return a positive time interval (> 0) the read's timeout will be extended by the given amount.
110 * If you don't implement this method, or return a non-positive time interval (<= 0) the read will timeout as usual.
111 *
112 * The elapsed parameter is the sum of the original timeout, plus any additions previously added via this method.
113 * The length parameter is the number of bytes that have been read so far for the read operation.
114 *
115 * Note that this method may be called multiple times for a single read if you return positive numbers.
116 **/
117 - (NSTimeInterval)onSocket:(AsyncSocket *)sock
118 shouldTimeoutReadWithTag:(long)tag
119 elapsed:(NSTimeInterval)elapsed
120 bytesDone:(CFIndex)length;
121
122 /**
123 * Called if a write operation has reached its timeout without completing.
124 * This method allows you to optionally extend the timeout.
125 * If you return a positive time interval (> 0) the write's timeout will be extended by the given amount.
126 * If you don't implement this method, or return a non-positive time interval (<= 0) the write will timeout as usual.
127 *
128 * The elapsed parameter is the sum of the original timeout, plus any additions previously added via this method.
129 * The length parameter is the number of bytes that have been written so far for the write operation.
130 *
131 * Note that this method may be called multiple times for a single write if you return positive numbers.
132 **/
133 - (NSTimeInterval)onSocket:(AsyncSocket *)sock
134 shouldTimeoutWriteWithTag:(long)tag
135 elapsed:(NSTimeInterval)elapsed
136 bytesDone:(CFIndex)length;
137
138 /**
139 * Called after the socket has successfully completed SSL/TLS negotiation.
60938ca built beginnings of a demonstration project to go with Blackbox
Matt Patenaude authored
140 * This method is not called unless you use the provided startTLS method.
a927cec @mattpat Updated to latest trunk version of CocoaHTTPServer
authored
141 *
142 * If a SSL/TLS negotiation fails (invalid certificate, etc) then the socket will immediately close,
143 * and the onSocket:willDisconnectWithError: delegate method will be called with the specific SSL error code.
60938ca built beginnings of a demonstration project to go with Blackbox
Matt Patenaude authored
144 **/
a927cec @mattpat Updated to latest trunk version of CocoaHTTPServer
authored
145 - (void)onSocketDidSecure:(AsyncSocket *)sock;
60938ca built beginnings of a demonstration project to go with Blackbox
Matt Patenaude authored
146
147 @end
148
149 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
150 #pragma mark -
151 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
152
153 @interface AsyncSocket : NSObject
154 {
a927cec @mattpat Updated to latest trunk version of CocoaHTTPServer
authored
155 CFSocketNativeHandle theNativeSocket4;
156 CFSocketNativeHandle theNativeSocket6;
157
60938ca built beginnings of a demonstration project to go with Blackbox
Matt Patenaude authored
158 CFSocketRef theSocket4; // IPv4 accept or connect socket
159 CFSocketRef theSocket6; // IPv6 accept or connect socket
a927cec @mattpat Updated to latest trunk version of CocoaHTTPServer
authored
160
60938ca built beginnings of a demonstration project to go with Blackbox
Matt Patenaude authored
161 CFReadStreamRef theReadStream;
162 CFWriteStreamRef theWriteStream;
163
164 CFRunLoopSourceRef theSource4; // For theSocket4
165 CFRunLoopSourceRef theSource6; // For theSocket6
166 CFRunLoopRef theRunLoop;
167 CFSocketContext theContext;
168 NSArray *theRunLoopModes;
169
170 NSTimer *theConnectTimer;
171
172 NSMutableArray *theReadQueue;
173 AsyncReadPacket *theCurrentRead;
174 NSTimer *theReadTimer;
175 NSMutableData *partialReadBuffer;
176
177 NSMutableArray *theWriteQueue;
178 AsyncWritePacket *theCurrentWrite;
179 NSTimer *theWriteTimer;
180
181 id theDelegate;
182 UInt16 theFlags;
183
184 long theUserData;
185 }
186
187 - (id)init;
188 - (id)initWithDelegate:(id)delegate;
189 - (id)initWithDelegate:(id)delegate userData:(long)userData;
190
191 /* String representation is long but has no "\n". */
192 - (NSString *)description;
193
194 /**
195 * Use "canSafelySetDelegate" to see if there is any pending business (reads and writes) with the current delegate
196 * before changing it. It is, of course, safe to change the delegate before connecting or accepting connections.
197 **/
198 - (id)delegate;
199 - (BOOL)canSafelySetDelegate;
200 - (void)setDelegate:(id)delegate;
201
202 /* User data can be a long, or an id or void * cast to a long. */
203 - (long)userData;
204 - (void)setUserData:(long)userData;
205
a927cec @mattpat Updated to latest trunk version of CocoaHTTPServer
authored
206 /* Don't use these to read or write. And don't close them either! */
60938ca built beginnings of a demonstration project to go with Blackbox
Matt Patenaude authored
207 - (CFSocketRef)getCFSocket;
208 - (CFReadStreamRef)getCFReadStream;
209 - (CFWriteStreamRef)getCFWriteStream;
210
211 // Once one of the accept or connect methods are called, the AsyncSocket instance is locked in
212 // and the other accept/connect methods can't be called without disconnecting the socket first.
213 // If the attempt fails or times out, these methods either return NO or
214 // call "onSocket:willDisconnectWithError:" and "onSockedDidDisconnect:".
215
216 // When an incoming connection is accepted, AsyncSocket invokes several delegate methods.
217 // These methods are (in chronological order):
218 // 1. onSocket:didAcceptNewSocket:
219 // 2. onSocket:wantsRunLoopForNewSocket:
220 // 3. onSocketWillConnect:
221 //
222 // Your server code will need to retain the accepted socket (if you want to accept it).
223 // The best place to do this is probably in the onSocket:didAcceptNewSocket: method.
224 //
225 // After the read and write streams have been setup for the newly accepted socket,
226 // the onSocket:didConnectToHost:port: method will be called on the proper run loop.
a927cec @mattpat Updated to latest trunk version of CocoaHTTPServer
authored
227 //
228 // Multithreading Note: If you're going to be moving the newly accepted socket to another run
229 // loop by implementing onSocket:wantsRunLoopForNewSocket:, then you should wait until the
230 // onSocket:didConnectToHost:port: method before calling read, write, or startTLS methods.
231 // Otherwise read/write events are scheduled on the incorrect runloop, and chaos may ensue.
60938ca built beginnings of a demonstration project to go with Blackbox
Matt Patenaude authored
232
233 /**
234 * Tells the socket to begin listening and accepting connections on the given port.
235 * When a connection comes in, the AsyncSocket instance will call the various delegate methods (see above).
236 * The socket will listen on all available interfaces (e.g. wifi, ethernet, etc)
237 **/
238 - (BOOL)acceptOnPort:(UInt16)port error:(NSError **)errPtr;
239
240 /**
241 * This method is the same as acceptOnPort:error: with the additional option
242 * of specifying which interface to listen on. So, for example, if you were writing code for a server that
243 * has multiple IP addresses, you could specify which address you wanted to listen on. Or you could use it
244 * to specify that the socket should only accept connections over ethernet, and not other interfaces such as wifi.
245 * You may also use the special strings "localhost" or "loopback" to specify that
246 * the socket only accept connections from the local machine.
247 *
248 * To accept connections on any interface pass nil, or simply use the acceptOnPort:error: method.
249 **/
a927cec @mattpat Updated to latest trunk version of CocoaHTTPServer
authored
250 - (BOOL)acceptOnInterface:(NSString *)interface port:(UInt16)port error:(NSError **)errPtr;
60938ca built beginnings of a demonstration project to go with Blackbox
Matt Patenaude authored
251
252 /**
253 * Connects to the given host and port.
254 * The host may be a domain name (e.g. "deusty.com") or an IP address string (e.g. "192.168.0.2")
255 **/
256 - (BOOL)connectToHost:(NSString *)hostname onPort:(UInt16)port error:(NSError **)errPtr;
257
258 /**
259 * This method is the same as connectToHost:onPort:error: with an additional timeout option.
260 * To not time out use a negative time interval, or simply use the connectToHost:onPort:error: method.
261 **/
262 - (BOOL)connectToHost:(NSString *)hostname
263 onPort:(UInt16)port
264 withTimeout:(NSTimeInterval)timeout
265 error:(NSError **)errPtr;
266
267 /**
268 * Connects to the given address, specified as a sockaddr structure wrapped in a NSData object.
269 * For example, a NSData object returned from NSNetservice's addresses method.
270 *
271 * If you have an existing struct sockaddr you can convert it to a NSData object like so:
272 * struct sockaddr sa -> NSData *dsa = [NSData dataWithBytes:&remoteAddr length:remoteAddr.sa_len];
273 * struct sockaddr *sa -> NSData *dsa = [NSData dataWithBytes:remoteAddr length:remoteAddr->sa_len];
274 **/
275 - (BOOL)connectToAddress:(NSData *)remoteAddr error:(NSError **)errPtr;
276
277 /**
278 * This method is the same as connectToAddress:error: with an additional timeout option.
279 * To not time out use a negative time interval, or simply use the connectToAddress:error: method.
280 **/
281 - (BOOL)connectToAddress:(NSData *)remoteAddr withTimeout:(NSTimeInterval)timeout error:(NSError **)errPtr;
282
283 /**
284 * Disconnects immediately. Any pending reads or writes are dropped.
a927cec @mattpat Updated to latest trunk version of CocoaHTTPServer
authored
285 * If the socket is not already disconnected, the onSocketDidDisconnect delegate method
286 * will be called immediately, before this method returns.
287 *
288 * Please note the recommended way of releasing an AsyncSocket instance (e.g. in a dealloc method)
289 * [asyncSocket setDelegate:nil];
290 * [asyncSocket disconnect];
291 * [asyncSocket release];
60938ca built beginnings of a demonstration project to go with Blackbox
Matt Patenaude authored
292 **/
293 - (void)disconnect;
294
295 /**
296 * Disconnects after all pending reads have completed.
297 * After calling this, the read and write methods will do nothing.
298 * The socket will disconnect even if there are still pending writes.
299 **/
300 - (void)disconnectAfterReading;
301
302 /**
303 * Disconnects after all pending writes 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 reads.
306 **/
307 - (void)disconnectAfterWriting;
308
309 /**
310 * Disconnects after all pending reads and writes have completed.
311 * After calling this, the read and write methods will do nothing.
312 **/
313 - (void)disconnectAfterReadingAndWriting;
314
315 /* Returns YES if the socket and streams are open, connected, and ready for reading and writing. */
316 - (BOOL)isConnected;
317
318 /**
319 * Returns the local or remote host and port to which this socket is connected, or nil and 0 if not connected.
320 * The host will be an IP address.
321 **/
322 - (NSString *)connectedHost;
323 - (UInt16)connectedPort;
324
325 - (NSString *)localHost;
326 - (UInt16)localPort;
327
a927cec @mattpat Updated to latest trunk version of CocoaHTTPServer
authored
328 /**
329 * Returns the local or remote address to which this socket is connected,
330 * specified as a sockaddr structure wrapped in a NSData object.
331 *
332 * See also the connectedHost, connectedPort, localHost and localPort methods.
333 **/
334 - (NSData *)connectedAddress;
335 - (NSData *)localAddress;
336
337 /**
338 * Returns whether the socket is IPv4 or IPv6.
339 * An accepting socket may be both.
340 **/
60938ca built beginnings of a demonstration project to go with Blackbox
Matt Patenaude authored
341 - (BOOL)isIPv4;
342 - (BOOL)isIPv6;
343
a927cec @mattpat Updated to latest trunk version of CocoaHTTPServer
authored
344 // The readData and writeData methods won't block.
345 //
346 // You may optionally set a timeout for any read/write operation. (To not timeout, use a negative time interval.)
347 // If a read/write opertion times out, the corresponding "onSocket:shouldTimeout..." delegate method
348 // is called to optionally allow you to extend the timeout.
349 // Upon a timeout, the "onSocket:willDisconnectWithError:" method is called, followed by "onSocketDidDisconnect".
350 //
351 // The tag is for your convenience.
60938ca built beginnings of a demonstration project to go with Blackbox
Matt Patenaude authored
352 // You can use it as an array index, step number, state id, pointer, etc., just like the socket's user data.
353
354 /**
355 * This will read a certain number of bytes into memory, and call the delegate method when those bytes have been read.
a927cec @mattpat Updated to latest trunk version of CocoaHTTPServer
authored
356 *
60938ca built beginnings of a demonstration project to go with Blackbox
Matt Patenaude authored
357 * If the length is 0, this method does nothing and the delegate is not called.
a927cec @mattpat Updated to latest trunk version of CocoaHTTPServer
authored
358 * If the timeout value is negative, the read operation will not use a timeout.
60938ca built beginnings of a demonstration project to go with Blackbox
Matt Patenaude authored
359 **/
360 - (void)readDataToLength:(CFIndex)length withTimeout:(NSTimeInterval)timeout tag:(long)tag;
361
362 /**
363 * This reads bytes until (and including) the passed "data" parameter, which acts as a separator.
364 * The bytes and the separator are returned by the delegate method.
365 *
366 * If you pass nil or zero-length data as the "data" parameter,
367 * the method will do nothing, and the delegate will not be called.
a927cec @mattpat Updated to latest trunk version of CocoaHTTPServer
authored
368 * If the timeout value is negative, the read operation will not use a timeout.
60938ca built beginnings of a demonstration project to go with Blackbox
Matt Patenaude authored
369 *
370 * To read a line from the socket, use the line separator (e.g. CRLF for HTTP, see below) as the "data" parameter.
371 * Note that this method is not character-set aware, so if a separator can occur naturally as part of the encoding for
372 * a character, the read will prematurely end.
373 **/
374 - (void)readDataToData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;
375
376 /**
377 * Same as readDataToData:withTimeout:tag, with the additional restriction that the amount of data read
378 * may not surpass the given maxLength (specified in bytes).
379 *
380 * If you pass a maxLength parameter that is less than the length of the data parameter,
381 * the method will do nothing, and the delegate will not be called.
382 *
383 * If the max length is surpassed, it is treated the same as a timeout - the socket is closed.
384 *
385 * Pass -1 as maxLength if no length restriction is desired, or simply use the readDataToData:withTimeout:tag method.
386 **/
387 - (void)readDataToData:(NSData *)data withTimeout:(NSTimeInterval)timeout maxLength:(CFIndex)length tag:(long)tag;
388
389 /**
390 * Reads the first available bytes that become available on the socket.
a927cec @mattpat Updated to latest trunk version of CocoaHTTPServer
authored
391 *
392 * If the timeout value is negative, the read operation will not use a timeout.
60938ca built beginnings of a demonstration project to go with Blackbox
Matt Patenaude authored
393 **/
394 - (void)readDataWithTimeout:(NSTimeInterval)timeout tag:(long)tag;
395
396 /**
397 * Writes data to the socket, and calls the delegate when finished.
398 *
399 * If you pass in nil or zero-length data, this method does nothing and the delegate will not be called.
a927cec @mattpat Updated to latest trunk version of CocoaHTTPServer
authored
400 * If the timeout value is negative, the write operation will not use a timeout.
60938ca built beginnings of a demonstration project to go with Blackbox
Matt Patenaude authored
401 **/
402 - (void)writeData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;
403
404 /**
405 * Returns progress of current read or write, from 0.0 to 1.0, or NaN if no read/write (use isnan() to check).
406 * "tag", "done" and "total" will be filled in if they aren't NULL.
407 **/
408 - (float)progressOfReadReturningTag:(long *)tag bytesDone:(CFIndex *)done total:(CFIndex *)total;
409 - (float)progressOfWriteReturningTag:(long *)tag bytesDone:(CFIndex *)done total:(CFIndex *)total;
410
411 /**
412 * Secures the connection using SSL/TLS.
413 *
414 * This method may be called at any time, and the TLS handshake will occur after all pending reads and writes
415 * are finished. This allows one the option of sending a protocol dependent StartTLS message, and queuing
416 * the upgrade to TLS at the same time, without having to wait for the write to finish.
417 * Any reads or writes scheduled after this method is called will occur over the secured connection.
418 *
419 * The possible keys and values for the TLS settings are well documented.
420 * Some possible keys are:
421 * - kCFStreamSSLLevel
422 * - kCFStreamSSLAllowsExpiredCertificates
423 * - kCFStreamSSLAllowsExpiredRoots
424 * - kCFStreamSSLAllowsAnyRoot
425 * - kCFStreamSSLValidatesCertificateChain
426 * - kCFStreamSSLPeerName
427 * - kCFStreamSSLCertificates
428 * - kCFStreamSSLIsServer
429 *
430 * Please refer to Apple's documentation for associated values, as well as other possible keys.
431 *
a927cec @mattpat Updated to latest trunk version of CocoaHTTPServer
authored
432 * If you pass in nil or an empty dictionary, the default settings will be used.
433 *
434 * The default settings will check to make sure the remote party's certificate is signed by a
435 * trusted 3rd party certificate agency (e.g. verisign) and that the certificate is not expired.
436 * However it will not verify the name on the certificate unless you
437 * give it a name to verify against via the kCFStreamSSLPeerName key.
438 * The security implications of this are important to understand.
439 * Imagine you are attempting to create a secure connection to MySecureServer.com,
440 * but your socket gets directed to MaliciousServer.com because of a hacked DNS server.
441 * If you simply use the default settings, and MaliciousServer.com has a valid certificate,
442 * the default settings will not detect any problems since the certificate is valid.
443 * To properly secure your connection in this particular scenario you
444 * should set the kCFStreamSSLPeerName property to "MySecureServer.com".
445 * If you do not know the peer name of the remote host in advance (for example, you're not sure
446 * if it will be "domain.com" or "www.domain.com"), then you can use the default settings to validate the
447 * certificate, and then use the X509Certificate class to verify the issuer after the socket has been secured.
448 * The X509Certificate class is part of the CocoaAsyncSocket open source project.
60938ca built beginnings of a demonstration project to go with Blackbox
Matt Patenaude authored
449 **/
450 - (void)startTLS:(NSDictionary *)tlsSettings;
451
452 /**
453 * For handling readDataToData requests, data is necessarily read from the socket in small increments.
454 * The performance can be much improved by allowing AsyncSocket to read larger chunks at a time and
455 * store any overflow in a small internal buffer.
456 * This is termed pre-buffering, as some data may be read for you before you ask for it.
457 * If you use readDataToData a lot, enabling pre-buffering will result in better performance, especially on the iPhone.
458 *
459 * The default pre-buffering state is controlled by the DEFAULT_PREBUFFERING definition.
460 * It is highly recommended one leave this set to YES.
461 *
a927cec @mattpat Updated to latest trunk version of CocoaHTTPServer
authored
462 * This method exists in case pre-buffering needs to be disabled by default for some unforeseen reason.
60938ca built beginnings of a demonstration project to go with Blackbox
Matt Patenaude authored
463 * In that case, this method exists to allow one to easily enable pre-buffering when ready.
464 **/
465 - (void)enablePreBuffering;
466
467 /**
468 * When you create an AsyncSocket, it is added to the runloop of the current thread.
469 * So for manually created sockets, it is easiest to simply create the socket on the thread you intend to use it.
470 *
471 * If a new socket is accepted, the delegate method onSocket:wantsRunLoopForNewSocket: is called to
472 * allow you to place the socket on a separate thread. This works best in conjunction with a thread pool design.
473 *
474 * If, however, you need to move the socket to a separate thread at a later time, this
475 * method may be used to accomplish the task.
476 *
477 * This method must be called from the thread/runloop the socket is currently running on.
478 *
479 * Note: After calling this method, all further method calls to this object should be done from the given runloop.
480 * Also, all delegate calls will be sent on the given runloop.
481 **/
482 - (BOOL)moveToRunLoop:(NSRunLoop *)runLoop;
483
484 /**
485 * Allows you to configure which run loop modes the socket uses.
486 * The default set of run loop modes is NSDefaultRunLoopMode.
487 *
488 * If you'd like your socket to continue operation during other modes, you may want to add modes such as
489 * NSModalPanelRunLoopMode or NSEventTrackingRunLoopMode. Or you may simply want to use NSRunLoopCommonModes.
490 *
491 * Accepted sockets will automatically inherit the same run loop modes as the listening socket.
492 *
493 * Note: NSRunLoopCommonModes is defined in 10.5. For previous versions one can use kCFRunLoopCommonModes.
494 **/
495 - (BOOL)setRunLoopModes:(NSArray *)runLoopModes;
496
497 /**
498 * Returns the current run loop modes the AsyncSocket instance is operating in.
499 * The default set of run loop modes is NSDefaultRunLoopMode.
500 **/
501 - (NSArray *)runLoopModes;
502
503 /**
504 * In the event of an error, this method may be called during onSocket:willDisconnectWithError: to read
505 * any data that's left on the socket.
506 **/
507 - (NSData *)unreadData;
508
509 /* A few common line separators, for use with the readDataToData:... methods. */
510 + (NSData *)CRLFData; // 0x0D0A
511 + (NSData *)CRData; // 0x0D
512 + (NSData *)LFData; // 0x0A
513 + (NSData *)ZeroData; // 0x00
514
515 @end
Something went wrong with that request. Please try again.