PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Pods/CocoaAsyncSocket/RunLoop/AsyncSocket.h

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