@@ -29,7 +29,8 @@ enum AsyncSocketError
29
29
};
30
30
typedef enum AsyncSocketError AsyncSocketError;
31
31
32
- @interface NSObject (AsyncSocketDelegate)
32
+ @protocol AsyncSocketDelegate
33
+ @optional
33
34
34
35
/* *
35
36
* In the event of an error, the socket is closed.
@@ -49,11 +50,17 @@ typedef enum AsyncSocketError AsyncSocketError;
49
50
- (void )onSocketDidDisconnect : (AsyncSocket *)sock ;
50
51
51
52
/* *
52
- * Called when a socket accepts a connection. Another socket is spawned to handle it. The new socket will have
53
+ * Called when a socket accepts a connection (listening w/autoaccept 'YES') . Another socket is spawned to handle it. The new socket will have
53
54
* the same delegate and will call "onSocket:didConnectToHost:port:".
54
55
**/
55
56
- (void )onSocket : (AsyncSocket *)sock didAcceptNewSocket : (AsyncSocket *)newSocket ;
56
57
58
+ /* *
59
+ * Called when there is a connection to accept (listening w/autoaccept 'NO'). Can retrieve the BSD socket
60
+ * handle from the passed socket and call accept() on it, guaranteed to nonblock
61
+ **/
62
+ - (void )onSocketHasConnectionToAccept : (AsyncSocket *)sock ;
63
+
57
64
/* *
58
65
* Called when a new socket is spawned to handle a connection. This method should return the run-loop of the
59
66
* thread on which the new socket and its delegate should operate. If omitted, [NSRunLoop currentRunLoop] is used.
@@ -90,7 +97,7 @@ typedef enum AsyncSocketError AsyncSocketError;
90
97
* This would occur if using readToData: or readToLength: methods.
91
98
* It may be used to for things such as updating progress bars.
92
99
**/
93
- - (void )onSocket : (AsyncSocket *)sock didReadPartialDataOfLength : (CFIndex )partialLength tag : (long )tag ;
100
+ - (void )onSocket : (AsyncSocket *)sock didReadPartialDataOfLength : (NSUInteger )partialLength tag : (long )tag ;
94
101
95
102
/* *
96
103
* Called when a socket has completed writing the requested data. Not called if there is an error.
@@ -101,7 +108,7 @@ typedef enum AsyncSocketError AsyncSocketError;
101
108
* Called when a socket has written some data, but has not yet completed the entire write.
102
109
* It may be used to for things such as updating progress bars.
103
110
**/
104
- - (void )onSocket : (AsyncSocket *)sock didWritePartialDataOfLength : (CFIndex )partialLength tag : (long )tag ;
111
+ - (void )onSocket : (AsyncSocket *)sock didWritePartialDataOfLength : (NSUInteger )partialLength tag : (long )tag ;
105
112
106
113
/* *
107
114
* Called if a read operation has reached its timeout without completing.
@@ -116,8 +123,8 @@ typedef enum AsyncSocketError AsyncSocketError;
116
123
**/
117
124
- (NSTimeInterval )onSocket : (AsyncSocket *)sock
118
125
shouldTimeoutReadWithTag : (long )tag
119
- elapsed : (NSTimeInterval )elapsed
120
- bytesDone : (CFIndex )length ;
126
+ elapsed : (NSTimeInterval )elapsed
127
+ bytesDone : (NSUInteger )length ;
121
128
122
129
/* *
123
130
* Called if a write operation has reached its timeout without completing.
@@ -132,8 +139,8 @@ typedef enum AsyncSocketError AsyncSocketError;
132
139
**/
133
140
- (NSTimeInterval )onSocket : (AsyncSocket *)sock
134
141
shouldTimeoutWriteWithTag : (long )tag
135
- elapsed : (NSTimeInterval )elapsed
136
- bytesDone : (CFIndex )length ;
142
+ elapsed : (NSTimeInterval )elapsed
143
+ bytesDone : (NSUInteger )length ;
137
144
138
145
/* *
139
146
* Called after the socket has successfully completed SSL/TLS negotiation.
@@ -249,6 +256,20 @@ typedef enum AsyncSocketError AsyncSocketError;
249
256
**/
250
257
- (BOOL )acceptOnInterface : (NSString *)interface port : (UInt16 )port error : (NSError **)errPtr ;
251
258
259
+ /* *
260
+ * This method is the same as acceptOnInterface:port:error: with the additional option
261
+ * of specifying whether or not the listening socket will autoaccept incoming connections. If 'YES',
262
+ * then the onSocket:didAcceptNewSocket: callback is called; otherwise, onSocketHasConnectionToAccept:
263
+ * is called.
264
+ **/
265
+ - (BOOL )acceptOnInterface : (NSString *)interface port : (UInt16 )port autoaccept : (BOOL )autoaccept error : (NSError **)errPtr ;
266
+
267
+ /* *
268
+ * This method is used to create a new async socket from an accepted socket, and fires the onSocket:didAcceptNewSocket: delegate callback.
269
+ * Returns the new async socket, or nil if the accept failed.
270
+ **/
271
+ - (AsyncSocket*)doAcceptFromSocket : (CFSocketRef )parentSocket withNewNativeSocket : (CFSocketNativeHandle )newNativeSocket ;
272
+
252
273
/* *
253
274
* Connects to the given host and port.
254
275
* The host may be a domain name (e.g. "deusty.com") or an IP address string (e.g. "192.168.0.2")
@@ -280,6 +301,11 @@ typedef enum AsyncSocketError AsyncSocketError;
280
301
**/
281
302
- (BOOL )connectToAddress : (NSData *)remoteAddr withTimeout : (NSTimeInterval )timeout error : (NSError **)errPtr ;
282
303
304
+ - (BOOL )connectToAddress : (NSData *)remoteAddr
305
+ viaInterfaceAddress : (NSData *)interfaceAddr
306
+ withTimeout : (NSTimeInterval )timeout
307
+ error : (NSError **)errPtr ;
308
+
283
309
/* *
284
310
* Disconnects immediately. Any pending reads or writes are dropped.
285
311
* If the socket is not already disconnected, the onSocketDidDisconnect delegate method
@@ -389,7 +415,7 @@ typedef enum AsyncSocketError AsyncSocketError;
389
415
*
390
416
* If the timeout value is negative, the read operation will not use a timeout.
391
417
* If the buffer if nil, a buffer will automatically be created for you.
392
- * If maxLength is negative , no length restriction is enforced.
418
+ * If maxLength is zero , no length restriction is enforced.
393
419
*
394
420
* If the bufferOffset is greater than the length of the given buffer,
395
421
* the method will do nothing, and the delegate will not be called.
@@ -401,7 +427,7 @@ typedef enum AsyncSocketError AsyncSocketError;
401
427
- (void )readDataWithTimeout : (NSTimeInterval )timeout
402
428
buffer : (NSMutableData *)buffer
403
429
bufferOffset : (NSUInteger )offset
404
- maxLength : (CFIndex )length
430
+ maxLength : (NSUInteger )length
405
431
tag : (long )tag ;
406
432
407
433
/* *
@@ -411,7 +437,7 @@ typedef enum AsyncSocketError AsyncSocketError;
411
437
*
412
438
* If the length is 0, this method does nothing and the delegate is not called.
413
439
**/
414
- - (void )readDataToLength : (CFIndex )length withTimeout : (NSTimeInterval )timeout tag : (long )tag ;
440
+ - (void )readDataToLength : (NSUInteger )length withTimeout : (NSTimeInterval )timeout tag : (long )tag ;
415
441
416
442
/* *
417
443
* Reads the given number of bytes.
@@ -429,7 +455,7 @@ typedef enum AsyncSocketError AsyncSocketError;
429
455
* After completion, the data returned in onSocket:didReadData:withTag: will be a subset of the given buffer.
430
456
* That is, it will reference the bytes that were appended to the given buffer.
431
457
**/
432
- - (void )readDataToLength : (CFIndex )length
458
+ - (void )readDataToLength : (NSUInteger )length
433
459
withTimeout : (NSTimeInterval )timeout
434
460
buffer : (NSMutableData *)buffer
435
461
bufferOffset : (NSUInteger )offset
@@ -478,9 +504,11 @@ typedef enum AsyncSocketError AsyncSocketError;
478
504
* Reads bytes until (and including) the passed "data" parameter, which acts as a separator.
479
505
*
480
506
* If the timeout value is negative, the read operation will not use a timeout.
481
- * If maxLength is negative, no length restriction is enforced.
482
507
*
483
- * If the max length is surpassed, it is treated the same as a timeout - the socket is closed.
508
+ * If maxLength is zero, no length restriction is enforced.
509
+ * Otherwise if maxLength bytes are read without completing the read,
510
+ * it is treated similarly to a timeout - the socket is closed with a AsyncSocketReadMaxedOutError.
511
+ * The read will complete successfully if exactly maxLength bytes are read and the given data is found at the end.
484
512
*
485
513
* If you pass nil or zero-length data as the "data" parameter,
486
514
* the method will do nothing, and the delegate will not be called.
@@ -491,7 +519,7 @@ typedef enum AsyncSocketError AsyncSocketError;
491
519
* Note that this method is not character-set aware, so if a separator can occur naturally as part of the encoding for
492
520
* a character, the read will prematurely end.
493
521
**/
494
- - (void )readDataToData : (NSData *)data withTimeout : (NSTimeInterval )timeout maxLength : (CFIndex )length tag : (long )tag ;
522
+ - (void )readDataToData : (NSData *)data withTimeout : (NSTimeInterval )timeout maxLength : (NSUInteger )length tag : (long )tag ;
495
523
496
524
/* *
497
525
* Reads bytes until (and including) the passed "data" parameter, which acts as a separator.
@@ -501,9 +529,11 @@ typedef enum AsyncSocketError AsyncSocketError;
501
529
*
502
530
* If the timeout value is negative, the read operation will not use a timeout.
503
531
* If the buffer if nil, a buffer will automatically be created for you.
504
- * If maxLength is negative, no length restriction is enforced.
505
532
*
506
- * If the max length is surpassed, it is treated the same as a timeout - the socket is closed.
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 AsyncSocketReadMaxedOutError.
536
+ * The read will complete successfully if exactly maxLength bytes are read and the given data is found at the end.
507
537
*
508
538
* If you pass a maxLength parameter that is less than the length of the data parameter,
509
539
* the method will do nothing, and the delegate will not be called.
@@ -522,7 +552,7 @@ typedef enum AsyncSocketError AsyncSocketError;
522
552
withTimeout : (NSTimeInterval )timeout
523
553
buffer : (NSMutableData *)buffer
524
554
bufferOffset : (NSUInteger )offset
525
- maxLength : (CFIndex )length
555
+ maxLength : (NSUInteger )length
526
556
tag : (long )tag ;
527
557
528
558
/* *
@@ -537,8 +567,8 @@ typedef enum AsyncSocketError AsyncSocketError;
537
567
* Returns progress of current read or write, from 0.0 to 1.0, or NaN if no read/write (use isnan() to check).
538
568
* "tag", "done" and "total" will be filled in if they aren't NULL.
539
569
**/
540
- - (float )progressOfReadReturningTag : (long *)tag bytesDone : (CFIndex *)done total : (CFIndex *)total ;
541
- - (float )progressOfWriteReturningTag : (long *)tag bytesDone : (CFIndex *)done total : (CFIndex *)total ;
570
+ - (float )progressOfReadReturningTag : (long *)tag bytesDone : (NSUInteger *)done total : (NSUInteger *)total ;
571
+ - (float )progressOfWriteReturningTag : (long *)tag bytesDone : (NSUInteger *)done total : (NSUInteger *)total ;
542
572
543
573
/* *
544
574
* Secures the connection using SSL/TLS.
@@ -625,6 +655,8 @@ typedef enum AsyncSocketError AsyncSocketError;
625
655
* Note: NSRunLoopCommonModes is defined in 10.5. For previous versions one can use kCFRunLoopCommonModes.
626
656
**/
627
657
- (BOOL )setRunLoopModes : (NSArray *)runLoopModes ;
658
+ - (BOOL )addRunLoopMode : (NSString *)runLoopMode ;
659
+ - (BOOL )removeRunLoopMode : (NSString *)runLoopMode ;
628
660
629
661
/* *
630
662
* Returns the current run loop modes the AsyncSocket instance is operating in.
0 commit comments