Skip to content

Commit fb41b56

Browse files
committed
Improving documentation in GCDAsyncUdpSocket header file
1 parent 3bd4050 commit fb41b56

File tree

2 files changed

+112
-32
lines changed

2 files changed

+112
-32
lines changed

GCD/GCDAsyncUdpSocket.h

Lines changed: 112 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,29 @@ typedef enum GCDAsyncUdpSocketError GCDAsyncUdpSocketError;
2828

2929
/**
3030
* 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.
3436
*
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.
3638
* The filter can prevent such packets from arriving at the delegate.
3739
* And because the filter can run in its own independent queue, this doesn't slow down the delegate.
3840
*
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.
4042
* Many protocols built atop udp thus provide various resend/re-request algorithms.
4143
* 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.
4245
*
4346
* - Since the udp socket may be connectionless, its possible for unsolicited packets to arrive.
4447
* 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.
4848
*
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).
4954
*
5055
* @param data - The packet that was received.
5156
* @param address - The address the data was received from.
@@ -403,32 +408,90 @@ typedef BOOL (^GCDAsyncUdpSocketReceiveFilterBlock)(NSData *data, NSData *addres
403408
/**
404409
* Asynchronously sends the given data, with the given timeout and tag.
405410
*
406-
* If the timeout value is negative, the receive operation will not use a timeout.
407-
*
408-
*
409-
*
410411
* This method may only be used with a connected socket.
411412
* Recall that connecting is optional for a UDP socket.
412413
* For connected sockets, data can only be sent to the connected address.
413414
* 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.
414421
*
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.
417446
**/
418447
- (void)sendData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;
419448

420449
/**
421450
* Asynchronously sends the given data, with the given timeout and tag, to the given host and port.
422451
*
423-
* If the timeout value is negative, the receive operation will not use a timeout.
424-
*
425452
* This method cannot be used with a connected socket.
426453
* Recall that connecting is optional for a UDP socket.
427454
* For connected sockets, data can only be sent to the connected address.
428455
* 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.
429457
*
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.
432495
**/
433496
- (void)sendData:(NSData *)data
434497
toHost:(NSString *)host
@@ -443,10 +506,12 @@ typedef BOOL (^GCDAsyncUdpSocketReceiveFilterBlock)(NSData *data, NSData *addres
443506
* Recall that connecting is optional for a UDP socket.
444507
* For connected sockets, data can only be sent to the connected address.
445508
* 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.
446510
*
447511
* @param data
448512
* The data to send.
449513
* If data is nil or zero-length, this method does nothing.
514+
* If passing NSMutableData, please read the thread-safety notice below.
450515
*
451516
* @param address
452517
* 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
458523
* @param tag
459524
* The tag is for your convenience.
460525
* 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.
462528
* You can use it as an array index, state id, type constant, etc.
463529
*
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.
466543
**/
467544
- (void)sendData:(NSData *)data toAddress:(NSData *)remoteAddr withTimeout:(NSTimeInterval)timeout tag:(long)tag;
468545

@@ -540,23 +617,27 @@ typedef BOOL (^GCDAsyncUdpSocketReceiveFilterBlock)(NSData *data, NSData *addres
540617
*
541618
* A filter can provide several useful features.
542619
*
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.
544625
* The filter can prevent such packets from arriving at the delegate.
545626
* And because the filter can run in its own independent queue, this doesn't slow down the delegate.
546627
*
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.
548629
* Many protocols built atop udp thus provide various resend/re-request algorithms.
549630
* 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.
550632
*
551633
* - Since the udp socket may be connectionless, its possible for unsolicited packets to arrive.
552634
* 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-
*
557635
*
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).
560641
*
561642
* Example:
562643
*
@@ -569,6 +650,8 @@ typedef BOOL (^GCDAsyncUdpSocketReceiveFilterBlock)(NSData *data, NSData *addres
569650
* };
570651
* [udpSocket setReceiveFilter:filter withQueue:myParsingQueue];
571652
*
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.
572655
**/
573656
- (void)setReceiveFilter:(GCDAsyncUdpSocketReceiveFilterBlock)filterBlock withQueue:(dispatch_queue_t)filterQueue;
574657

GCD/GCDAsyncUdpSocket.m

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3126,7 +3126,6 @@ - (void)maybeConnect
31263126
flags |= kDidBind;
31273127
flags |= kDidConnect;
31283128

3129-
31303129
cachedConnectedAddress = address;
31313130
cachedConnectedHost = [[self class] hostFromAddress:address];
31323131
cachedConnectedPort = [[self class] portFromAddress:address];
@@ -3502,9 +3501,7 @@ - (void)sendData:(NSData *)data toAddress:(NSData *)remoteAddr withTimeout:(NSTi
35023501

35033502
[sendQueue addObject:packet];
35043503
[self maybeDequeueSend];
3505-
35063504
}});
3507-
35083505
}
35093506

35103507
- (void)maybeDequeueSend

0 commit comments

Comments
 (0)