@@ -97,7 +97,7 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
97
97
uint32_t flags;
98
98
uint16_t config;
99
99
100
- id delegate;
100
+ __weak id delegate;
101
101
dispatch_queue_t delegateQueue;
102
102
103
103
int socket4FD;
@@ -323,7 +323,7 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
323
323
324
324
/* *
325
325
* Connects to the given address, specified as a sockaddr structure wrapped in a NSData object.
326
- * For example, a NSData object returned from NSNetservice 's addresses method.
326
+ * For example, a NSData object returned from NSNetService 's addresses method.
327
327
*
328
328
* If you have an existing struct sockaddr you can convert it to a NSData object like so:
329
329
* struct sockaddr sa -> NSData *dsa = [NSData dataWithBytes:&remoteAddr length:remoteAddr.sa_len];
@@ -343,7 +343,7 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
343
343
* Connects to the given address, using the specified interface and timeout.
344
344
*
345
345
* The address is specified as a sockaddr structure wrapped in a NSData object.
346
- * For example, a NSData object returned from NSNetservice 's addresses method.
346
+ * For example, a NSData object returned from NSNetService 's addresses method.
347
347
*
348
348
* If you have an existing struct sockaddr you can convert it to a NSData object like so:
349
349
* struct sockaddr sa -> NSData *dsa = [NSData dataWithBytes:&remoteAddr length:remoteAddr.sa_len];
@@ -501,9 +501,10 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
501
501
* If the bufferOffset is greater than the length of the given buffer,
502
502
* the method will do nothing, and the delegate will not be called.
503
503
*
504
- * If you pass a buffer, you must not alter it in any way while AsyncSocket is using it.
504
+ * If you pass a buffer, you must not alter it in any way while the socket is using it.
505
505
* After completion, the data returned in socket:didReadData:withTag: will be a subset of the given buffer.
506
- * That is, it will reference the bytes that were appended to the given buffer.
506
+ * That is, it will reference the bytes that were appended to the given buffer via
507
+ * the method [NSData dataWithBytesNoCopy:length:freeWhenDone:NO].
507
508
**/
508
509
- (void )readDataWithTimeout : (NSTimeInterval )timeout
509
510
buffer : (NSMutableData *)buffer
@@ -523,9 +524,10 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
523
524
* If the bufferOffset is greater than the length of the given buffer,
524
525
* the method will do nothing, and the delegate will not be called.
525
526
*
526
- * If you pass a buffer, you must not alter it in any way while AsyncSocket is using it.
527
+ * If you pass a buffer, you must not alter it in any way while the socket is using it.
527
528
* After completion, the data returned in socket:didReadData:withTag: will be a subset of the given buffer.
528
- * That is, it will reference the bytes that were appended to the given buffer.
529
+ * That is, it will reference the bytes that were appended to the given buffer via
530
+ * the method [NSData dataWithBytesNoCopy:length:freeWhenDone:NO].
529
531
**/
530
532
- (void )readDataWithTimeout : (NSTimeInterval )timeout
531
533
buffer : (NSMutableData *)buffer
@@ -556,7 +558,8 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
556
558
*
557
559
* If you pass a buffer, you must not alter it in any way while AsyncSocket is using it.
558
560
* After completion, the data returned in socket:didReadData:withTag: will be a subset of the given buffer.
559
- * That is, it will reference the bytes that were appended to the given buffer.
561
+ * That is, it will reference the bytes that were appended to the given buffer via
562
+ * the method [NSData dataWithBytesNoCopy:length:freeWhenDone:NO].
560
563
**/
561
564
- (void )readDataToLength : (NSUInteger )length
562
565
withTimeout : (NSTimeInterval )timeout
@@ -570,11 +573,20 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
570
573
* If the timeout value is negative, the read operation will not use a timeout.
571
574
*
572
575
* If you pass nil or zero-length data as the "data" parameter,
573
- * the method will do nothing, and the delegate will not be called.
576
+ * the method will do nothing (except maybe print a warning) , and the delegate will not be called.
574
577
*
575
578
* To read a line from the socket, use the line separator (e.g. CRLF for HTTP, see below) as the "data" parameter.
576
- * Note that this method is not character-set aware, so if a separator can occur naturally as part of the encoding for
577
- * a character, the read will prematurely end.
579
+ * If you're developing your own custom protocol, be sure your separator can not occur naturally as
580
+ * part of the data between separators.
581
+ * For example, imagine you want to send several small documents over a socket.
582
+ * Using CRLF as a separator is likely unwise, as a CRLF could easily exist within the documents.
583
+ * In this particular example, it would be better to use a protocol similar to HTTP with
584
+ * a header that includes the length of the document.
585
+ * Also be careful that your separator cannot occur naturally as part of the encoding for a character.
586
+ *
587
+ * The given data (separator) parameter should be immutable.
588
+ * For performance reasons, the socket will retain it, not copy it.
589
+ * So if it is immutable, don't modify it while the socket is using it.
578
590
**/
579
591
- (void )readDataToData : (NSData *)data withTimeout : (NSTimeInterval )timeout tag : (long )tag ;
580
592
@@ -587,15 +599,25 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
587
599
* If the buffer if nil, a buffer will automatically be created for you.
588
600
*
589
601
* If the bufferOffset is greater than the length of the given buffer,
590
- * the method will do nothing, and the delegate will not be called.
602
+ * the method will do nothing (except maybe print a warning) , and the delegate will not be called.
591
603
*
592
- * If you pass a buffer, you must not alter it in any way while AsyncSocket is using it.
604
+ * If you pass a buffer, you must not alter it in any way while the socket is using it.
593
605
* After completion, the data returned in socket:didReadData:withTag: will be a subset of the given buffer.
594
- * That is, it will reference the bytes that were appended to the given buffer.
606
+ * That is, it will reference the bytes that were appended to the given buffer via
607
+ * the method [NSData dataWithBytesNoCopy:length:freeWhenDone:NO].
595
608
*
596
609
* To read a line from the socket, use the line separator (e.g. CRLF for HTTP, see below) as the "data" parameter.
597
- * Note that this method is not character-set aware, so if a separator can occur naturally as part of the encoding for
598
- * a character, the read will prematurely end.
610
+ * If you're developing your own custom protocol, be sure your separator can not occur naturally as
611
+ * part of the data between separators.
612
+ * For example, imagine you want to send several small documents over a socket.
613
+ * Using CRLF as a separator is likely unwise, as a CRLF could easily exist within the documents.
614
+ * In this particular example, it would be better to use a protocol similar to HTTP with
615
+ * a header that includes the length of the document.
616
+ * Also be careful that your separator cannot occur naturally as part of the encoding for a character.
617
+ *
618
+ * The given data (separator) parameter should be immutable.
619
+ * For performance reasons, the socket will retain it, not copy it.
620
+ * So if it is immutable, don't modify it while the socket is using it.
599
621
**/
600
622
- (void )readDataToData : (NSData *)data
601
623
withTimeout : (NSTimeInterval )timeout
@@ -614,13 +636,22 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
614
636
* The read will complete successfully if exactly maxLength bytes are read and the given data is found at the end.
615
637
*
616
638
* If you pass nil or zero-length data as the "data" parameter,
617
- * the method will do nothing, and the delegate will not be called.
639
+ * the method will do nothing (except maybe print a warning) , and the delegate will not be called.
618
640
* If you pass a maxLength parameter that is less than the length of the data parameter,
619
- * the method will do nothing, and the delegate will not be called.
641
+ * the method will do nothing (except maybe print a warning) , and the delegate will not be called.
620
642
*
621
643
* To read a line from the socket, use the line separator (e.g. CRLF for HTTP, see below) as the "data" parameter.
622
- * Note that this method is not character-set aware, so if a separator can occur naturally as part of the encoding for
623
- * a character, the read will prematurely end.
644
+ * If you're developing your own custom protocol, be sure your separator can not occur naturally as
645
+ * part of the data between separators.
646
+ * For example, imagine you want to send several small documents over a socket.
647
+ * Using CRLF as a separator is likely unwise, as a CRLF could easily exist within the documents.
648
+ * In this particular example, it would be better to use a protocol similar to HTTP with
649
+ * a header that includes the length of the document.
650
+ * Also be careful that your separator cannot occur naturally as part of the encoding for a character.
651
+ *
652
+ * The given data (separator) parameter should be immutable.
653
+ * For performance reasons, the socket will retain it, not copy it.
654
+ * So if it is immutable, don't modify it while the socket is using it.
624
655
**/
625
656
- (void )readDataToData : (NSData *)data withTimeout : (NSTimeInterval )timeout maxLength : (NSUInteger )length tag : (long )tag ;
626
657
@@ -637,18 +668,28 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
637
668
* it is treated similarly to a timeout - the socket is closed with a GCDAsyncSocketReadMaxedOutError.
638
669
* The read will complete successfully if exactly maxLength bytes are read and the given data is found at the end.
639
670
*
640
- * If you pass a maxLength parameter that is less than the length of the data parameter,
641
- * the method will do nothing, and the delegate will not be called.
671
+ * If you pass a maxLength parameter that is less than the length of the data (separator) parameter,
672
+ * the method will do nothing (except maybe print a warning) , and the delegate will not be called.
642
673
* If the bufferOffset is greater than the length of the given buffer,
643
- * the method will do nothing, and the delegate will not be called.
674
+ * the method will do nothing (except maybe print a warning) , and the delegate will not be called.
644
675
*
645
- * If you pass a buffer, you must not alter it in any way while AsyncSocket is using it.
676
+ * If you pass a buffer, you must not alter it in any way while the socket is using it.
646
677
* After completion, the data returned in socket:didReadData:withTag: will be a subset of the given buffer.
647
- * That is, it will reference the bytes that were appended to the given buffer.
678
+ * That is, it will reference the bytes that were appended to the given buffer via
679
+ * the method [NSData dataWithBytesNoCopy:length:freeWhenDone:NO].
648
680
*
649
681
* To read a line from the socket, use the line separator (e.g. CRLF for HTTP, see below) as the "data" parameter.
650
- * Note that this method is not character-set aware, so if a separator can occur naturally as part of the encoding for
651
- * a character, the read will prematurely end.
682
+ * If you're developing your own custom protocol, be sure your separator can not occur naturally as
683
+ * part of the data between separators.
684
+ * For example, imagine you want to send several small documents over a socket.
685
+ * Using CRLF as a separator is likely unwise, as a CRLF could easily exist within the documents.
686
+ * In this particular example, it would be better to use a protocol similar to HTTP with
687
+ * a header that includes the length of the document.
688
+ * Also be careful that your separator cannot occur naturally as part of the encoding for a character.
689
+ *
690
+ * The given data (separator) parameter should be immutable.
691
+ * For performance reasons, the socket will retain it, not copy it.
692
+ * So if it is immutable, don't modify it while the socket is using it.
652
693
**/
653
694
- (void )readDataToData : (NSData *)data
654
695
withTimeout : (NSTimeInterval )timeout
@@ -664,6 +705,17 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
664
705
*
665
706
* If you pass in nil or zero-length data, this method does nothing and the delegate will not be called.
666
707
* If the timeout value is negative, the write operation will not use a timeout.
708
+ *
709
+ * Thread-Safety Note:
710
+ * If the given data parameter is mutable (NSMutableData) then you MUST NOT alter the data while
711
+ * the socket is writing it. In other words, it's not safe to alter the data until after the delegate method
712
+ * socket:didWriteDataWithTag: is invoked signifying that this particular write operation has completed.
713
+ * This is due to the fact that GCDAsyncSocket does NOT copy the data. It simply retains it.
714
+ * This is for performance reasons. Often times, if NSMutableData is passed, it is because
715
+ * a request/response was built up in memory. Copying this data adds an unwanted/unneeded overhead.
716
+ * If you need to write data from an immutable buffer, and you need to alter the buffer before the socket
717
+ * completes writing the bytes (which is NOT immediately after this method returns, but rather at a later time
718
+ * when the delegate method notifies you), then you should first copy the bytes, and pass the copy to this method.
667
719
**/
668
720
- (void )writeData : (NSData *)data withTimeout : (NSTimeInterval )timeout tag : (long )tag ;
669
721
0 commit comments