@@ -28,24 +28,29 @@ typedef enum GCDAsyncUdpSocketError GCDAsyncUdpSocketError;
28
28
29
29
/* *
30
30
* You may optionally set a receive filter for the socket.
31
- * This receive filter may be set to run in its own queue (independent of delegate queue).
32
- *
33
- * A filter can provide several useful features.
31
+ * A filter can provide several useful features:
32
+ *
33
+ * 1. Many times udp packets need to be parsed.
34
+ * Since the filter can run in its own independent queue, you can parallelize this parsing quite easily.
35
+ * The end result is a parallel socket io, datagram parsing, and packet processing.
34
36
*
35
- * 1 . Many times udp packets are discarded because they are duplicate/unneeded/unsolicited.
37
+ * 2 . Many times udp packets are discarded because they are duplicate/unneeded/unsolicited.
36
38
* The filter can prevent such packets from arriving at the delegate.
37
39
* And because the filter can run in its own independent queue, this doesn't slow down the delegate.
38
40
*
39
- * - Since the udp protocol does not guarnatee delivery, udp packets may be lost.
41
+ * - Since the udp protocol does not guarantee delivery, udp packets may be lost.
40
42
* Many protocols built atop udp thus provide various resend/re-request algorithms.
41
43
* This sometimes results in duplicate packets arriving.
44
+ * A filter may allow you to architect the duplicate detection code to run in parallel to normal processing.
42
45
*
43
46
* - Since the udp socket may be connectionless, its possible for unsolicited packets to arrive.
44
47
* Such packets need to be ignored.
45
- *
46
- * 2. Many times udp packets need to be parsed.
47
- * Since the filter can run in its own independent queue, you can parallelize this parsing quite easily.
48
48
*
49
+ * 3. Sometimes traffic shapers are needed to simulate real world environments.
50
+ * A filter allows you to write custom code to simulate such environments.
51
+ * The ability to code this yourself is especially helpful when your simulated environment
52
+ * is more complicated than simple traffic shaping (e.g. simulating a cone port restricted router),
53
+ * or the system tools to handle this aren't available (e.g. on a mobile device).
49
54
*
50
55
* @param data - The packet that was received.
51
56
* @param address - The address the data was received from.
@@ -403,32 +408,90 @@ typedef BOOL (^GCDAsyncUdpSocketReceiveFilterBlock)(NSData *data, NSData *addres
403
408
/* *
404
409
* Asynchronously sends the given data, with the given timeout and tag.
405
410
*
406
- * If the timeout value is negative, the receive operation will not use a timeout.
407
- *
408
- *
409
- *
410
411
* This method may only be used with a connected socket.
411
412
* Recall that connecting is optional for a UDP socket.
412
413
* For connected sockets, data can only be sent to the connected address.
413
414
* For non-connected sockets, the remote destination is specified for each packet.
415
+ * For more information about optionally connecting udp sockets, see the documentation for the connect methods above.
416
+ *
417
+ * @param data
418
+ * The data to send.
419
+ * If data is nil or zero-length, this method does nothing.
420
+ * If passing NSMutableData, please read the thread-safety notice below.
414
421
*
415
- * If data is nil or zero-length this method does nothing.
416
- * Otherwise the result is reported via the delegate methods.
422
+ * @param timeout
423
+ * The timeout for the send opeartion.
424
+ * If the timeout value is negative, the send operation will not use a timeout.
425
+ *
426
+ * @param tag
427
+ * The tag is for your convenience.
428
+ * It is not sent or received over the socket in any manner what-so-ever.
429
+ * It is reported back as a parameter in the udpSocket:didSendDataWithTag:
430
+ * or udpSocket:didNotSendDataWithTag:dueToError: methods.
431
+ * You can use it as an array index, state id, type constant, etc.
432
+ *
433
+ *
434
+ * Thread-Safety Note:
435
+ * If the given data parameter is mutable (NSMutableData) then you MUST NOT alter the data while
436
+ * the socket is sending it. In other words, it's not safe to alter the data until after the delegate method
437
+ * udpSocket:didSendDataWithTag: or udpSocket:didNotSendDataWithTag:dueToError: is invoked signifying
438
+ * that this particular send operation has completed.
439
+ * This is due to the fact that GCDAsyncUdpSocket does NOT copy the data.
440
+ * It simply retains it for performance reasons.
441
+ * Often times, if NSMutableData is passed, it is because a request/response was built up in memory.
442
+ * Copying this data adds an unwanted/unneeded overhead.
443
+ * If you need to write data from an immutable buffer, and you need to alter the buffer before the socket
444
+ * completes sending the bytes (which is NOT immediately after this method returns, but rather at a later time
445
+ * when the delegate method notifies you), then you should first copy the bytes, and pass the copy to this method.
417
446
**/
418
447
- (void )sendData : (NSData *)data withTimeout : (NSTimeInterval )timeout tag : (long )tag ;
419
448
420
449
/* *
421
450
* Asynchronously sends the given data, with the given timeout and tag, to the given host and port.
422
451
*
423
- * If the timeout value is negative, the receive operation will not use a timeout.
424
- *
425
452
* This method cannot be used with a connected socket.
426
453
* Recall that connecting is optional for a UDP socket.
427
454
* For connected sockets, data can only be sent to the connected address.
428
455
* For non-connected sockets, the remote destination is specified for each packet.
456
+ * For more information about optionally connecting udp sockets, see the documentation for the connect methods above.
429
457
*
430
- * If data is nil or zero-length this method does nothing.
431
- * Otherwise the result is reported via the delegate methods.
458
+ * @param data
459
+ * The data to send.
460
+ * If data is nil or zero-length, this method does nothing.
461
+ * If passing NSMutableData, please read the thread-safety notice below.
462
+ *
463
+ * @param host
464
+ * The destination to send the udp packet to.
465
+ * May be specified as a domain name (e.g. "deusty.com") or an IP address string (e.g. "192.168.0.2").
466
+ * You may also use the convenience strings of "loopback" or "localhost".
467
+ *
468
+ * @param port
469
+ * The port of the host to send to.
470
+ *
471
+ * @param timeout
472
+ * The timeout for the send opeartion.
473
+ * If the timeout value is negative, the send operation will not use a timeout.
474
+ *
475
+ * @param tag
476
+ * The tag is for your convenience.
477
+ * It is not sent or received over the socket in any manner what-so-ever.
478
+ * It is reported back as a parameter in the udpSocket:didSendDataWithTag:
479
+ * or udpSocket:didNotSendDataWithTag:dueToError: methods.
480
+ * You can use it as an array index, state id, type constant, etc.
481
+ *
482
+ *
483
+ * Thread-Safety Note:
484
+ * If the given data parameter is mutable (NSMutableData) then you MUST NOT alter the data while
485
+ * the socket is sending it. In other words, it's not safe to alter the data until after the delegate method
486
+ * udpSocket:didSendDataWithTag: or udpSocket:didNotSendDataWithTag:dueToError: is invoked signifying
487
+ * that this particular send operation has completed.
488
+ * This is due to the fact that GCDAsyncUdpSocket does NOT copy the data.
489
+ * It simply retains it for performance reasons.
490
+ * Often times, if NSMutableData is passed, it is because a request/response was built up in memory.
491
+ * Copying this data adds an unwanted/unneeded overhead.
492
+ * If you need to write data from an immutable buffer, and you need to alter the buffer before the socket
493
+ * completes sending the bytes (which is NOT immediately after this method returns, but rather at a later time
494
+ * when the delegate method notifies you), then you should first copy the bytes, and pass the copy to this method.
432
495
**/
433
496
- (void )sendData : (NSData *)data
434
497
toHost : (NSString *)host
@@ -443,10 +506,12 @@ typedef BOOL (^GCDAsyncUdpSocketReceiveFilterBlock)(NSData *data, NSData *addres
443
506
* Recall that connecting is optional for a UDP socket.
444
507
* For connected sockets, data can only be sent to the connected address.
445
508
* For non-connected sockets, the remote destination is specified for each packet.
509
+ * For more information about optionally connecting udp sockets, see the documentation for the connect methods above.
446
510
*
447
511
* @param data
448
512
* The data to send.
449
513
* If data is nil or zero-length, this method does nothing.
514
+ * If passing NSMutableData, please read the thread-safety notice below.
450
515
*
451
516
* @param address
452
517
* The address to send the data to (specified as a sockaddr structure wrapped in a NSData object).
@@ -458,11 +523,23 @@ typedef BOOL (^GCDAsyncUdpSocketReceiveFilterBlock)(NSData *data, NSData *addres
458
523
* @param tag
459
524
* The tag is for your convenience.
460
525
* It is not sent or received over the socket in any manner what-so-ever.
461
- * It is reported back as a parameter in the delegate methods.
526
+ * It is reported back as a parameter in the udpSocket:didSendDataWithTag:
527
+ * or udpSocket:didNotSendDataWithTag:dueToError: methods.
462
528
* You can use it as an array index, state id, type constant, etc.
463
529
*
464
- * If data is nil or zero-length, this method does nothing.
465
- * Otherwise the result is reported via the delegate methods.
530
+ *
531
+ * Thread-Safety Note:
532
+ * If the given data parameter is mutable (NSMutableData) then you MUST NOT alter the data while
533
+ * the socket is sending it. In other words, it's not safe to alter the data until after the delegate method
534
+ * udpSocket:didSendDataWithTag: or udpSocket:didNotSendDataWithTag:dueToError: is invoked signifying
535
+ * that this particular send operation has completed.
536
+ * This is due to the fact that GCDAsyncUdpSocket does NOT copy the data.
537
+ * It simply retains it for performance reasons.
538
+ * Often times, if NSMutableData is passed, it is because a request/response was built up in memory.
539
+ * Copying this data adds an unwanted/unneeded overhead.
540
+ * If you need to write data from an immutable buffer, and you need to alter the buffer before the socket
541
+ * completes sending the bytes (which is NOT immediately after this method returns, but rather at a later time
542
+ * when the delegate method notifies you), then you should first copy the bytes, and pass the copy to this method.
466
543
**/
467
544
- (void )sendData : (NSData *)data toAddress : (NSData *)remoteAddr withTimeout : (NSTimeInterval )timeout tag : (long )tag ;
468
545
@@ -540,23 +617,27 @@ typedef BOOL (^GCDAsyncUdpSocketReceiveFilterBlock)(NSData *data, NSData *addres
540
617
*
541
618
* A filter can provide several useful features.
542
619
*
543
- * 1. Many times udp packets are discarded because they are duplicate/unneeded/unsolicited.
620
+ * 1. Many times udp packets need to be parsed.
621
+ * Since the filter can run in its own independent queue, you can parallelize this parsing quite easily.
622
+ * The end result is a parallel socket io, datagram parsing, and packet processing.
623
+ *
624
+ * 2. Many times udp packets are discarded because they are duplicate/unneeded/unsolicited.
544
625
* The filter can prevent such packets from arriving at the delegate.
545
626
* And because the filter can run in its own independent queue, this doesn't slow down the delegate.
546
627
*
547
- * - Since the udp protocol does not guarnatee delivery, udp packets may be lost.
628
+ * - Since the udp protocol does not guarantee delivery, udp packets may be lost.
548
629
* Many protocols built atop udp thus provide various resend/re-request algorithms.
549
630
* This sometimes results in duplicate packets arriving.
631
+ * A filter may allow you to architect the duplicate detection code to run in parallel to normal processing.
550
632
*
551
633
* - Since the udp socket may be connectionless, its possible for unsolicited packets to arrive.
552
634
* Such packets need to be ignored.
553
- *
554
- * 2. Many times udp packets need to be parsed.
555
- * Since the filter can run in its own independent queue, you can parallelize this parsing quite easily.
556
- *
557
635
*
558
- * For more information about GCDAsyncUdpSocketReceiveFilterBlock, see the documentation for its typedef.
559
- * To remove a previously set filter, invoke this method and pass a nil filterBlock and NULL filterQueue.
636
+ * 3. Sometimes traffic shapers are needed to simulate real world environments.
637
+ * A filter allows you to write custom code to simulate such environments.
638
+ * The ability to code this yourself is especially helpful when your simulated environment
639
+ * is more complicated than simple traffic shaping (e.g. simulating a cone port restricted router),
640
+ * or the system tools to handle this aren't available (e.g. on a mobile device).
560
641
*
561
642
* Example:
562
643
*
@@ -569,6 +650,8 @@ typedef BOOL (^GCDAsyncUdpSocketReceiveFilterBlock)(NSData *data, NSData *addres
569
650
* };
570
651
* [udpSocket setReceiveFilter:filter withQueue:myParsingQueue];
571
652
*
653
+ * For more information about GCDAsyncUdpSocketReceiveFilterBlock, see the documentation for its typedef.
654
+ * To remove a previously set filter, invoke this method and pass a nil filterBlock and NULL filterQueue.
572
655
**/
573
656
- (void )setReceiveFilter : (GCDAsyncUdpSocketReceiveFilterBlock)filterBlock withQueue : (dispatch_queue_t )filterQueue ;
574
657
0 commit comments