@@ -449,9 +449,10 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
449
449
* If the bufferOffset is greater than the length of the given buffer,
450
450
* the method will do nothing, and the delegate will not be called.
451
451
*
452
- * If you pass a buffer, you must not alter it in any way while AsyncSocket is using it.
452
+ * If you pass a buffer, you must not alter it in any way while the socket is using it.
453
453
* After completion, the data returned in socket:didReadData:withTag: will be a subset of the given buffer.
454
- * That is, it will reference the bytes that were appended to the given buffer.
454
+ * That is, it will reference the bytes that were appended to the given buffer via
455
+ * the method [NSData dataWithBytesNoCopy:length:freeWhenDone:NO].
455
456
**/
456
457
- (void )readDataWithTimeout : (NSTimeInterval )timeout
457
458
buffer : (NSMutableData *)buffer
@@ -471,9 +472,10 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
471
472
* If the bufferOffset is greater than the length of the given buffer,
472
473
* the method will do nothing, and the delegate will not be called.
473
474
*
474
- * If you pass a buffer, you must not alter it in any way while AsyncSocket is using it.
475
+ * If you pass a buffer, you must not alter it in any way while the socket is using it.
475
476
* After completion, the data returned in socket:didReadData:withTag: will be a subset of the given buffer.
476
- * That is, it will reference the bytes that were appended to the given buffer.
477
+ * That is, it will reference the bytes that were appended to the given buffer via
478
+ * the method [NSData dataWithBytesNoCopy:length:freeWhenDone:NO].
477
479
**/
478
480
- (void )readDataWithTimeout : (NSTimeInterval )timeout
479
481
buffer : (NSMutableData *)buffer
@@ -504,7 +506,8 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
504
506
*
505
507
* If you pass a buffer, you must not alter it in any way while AsyncSocket is using it.
506
508
* After completion, the data returned in socket:didReadData:withTag: will be a subset of the given buffer.
507
- * That is, it will reference the bytes that were appended to the given buffer.
509
+ * That is, it will reference the bytes that were appended to the given buffer via
510
+ * the method [NSData dataWithBytesNoCopy:length:freeWhenDone:NO].
508
511
**/
509
512
- (void )readDataToLength : (NSUInteger )length
510
513
withTimeout : (NSTimeInterval )timeout
@@ -518,11 +521,20 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
518
521
* If the timeout value is negative, the read operation will not use a timeout.
519
522
*
520
523
* If you pass nil or zero-length data as the "data" parameter,
521
- * the method will do nothing, and the delegate will not be called.
524
+ * the method will do nothing (except maybe print a warning) , and the delegate will not be called.
522
525
*
523
526
* To read a line from the socket, use the line separator (e.g. CRLF for HTTP, see below) as the "data" parameter.
524
- * Note that this method is not character-set aware, so if a separator can occur naturally as part of the encoding for
525
- * a character, the read will prematurely end.
527
+ * If you're developing your own custom protocol, be sure your separator can not occur naturally as
528
+ * part of the data between separators.
529
+ * For example, imagine you want to send several small documents over a socket.
530
+ * Using CRLF as a separator is likely unwise, as a CRLF could easily exist within the documents.
531
+ * In this particular example, it would be better to use a protocol similar to HTTP with
532
+ * a header that includes the length of the document.
533
+ * Also be careful that your separator cannot occur naturally as part of the encoding for a character.
534
+ *
535
+ * The given data (separator) parameter should be immutable.
536
+ * For performance reasons, the socket will retain it, not copy it.
537
+ * So if it is immutable, don't modify it while the socket is using it.
526
538
**/
527
539
- (void )readDataToData : (NSData *)data withTimeout : (NSTimeInterval )timeout tag : (long )tag ;
528
540
@@ -535,15 +547,25 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
535
547
* If the buffer if nil, a buffer will automatically be created for you.
536
548
*
537
549
* If the bufferOffset is greater than the length of the given buffer,
538
- * the method will do nothing, and the delegate will not be called.
550
+ * the method will do nothing (except maybe print a warning) , and the delegate will not be called.
539
551
*
540
- * If you pass a buffer, you must not alter it in any way while AsyncSocket is using it.
552
+ * If you pass a buffer, you must not alter it in any way while the socket is using it.
541
553
* After completion, the data returned in socket:didReadData:withTag: will be a subset of the given buffer.
542
- * That is, it will reference the bytes that were appended to the given buffer.
554
+ * That is, it will reference the bytes that were appended to the given buffer via
555
+ * the method [NSData dataWithBytesNoCopy:length:freeWhenDone:NO].
543
556
*
544
557
* To read a line from the socket, use the line separator (e.g. CRLF for HTTP, see below) as the "data" parameter.
545
- * Note that this method is not character-set aware, so if a separator can occur naturally as part of the encoding for
546
- * a character, the read will prematurely end.
558
+ * If you're developing your own custom protocol, be sure your separator can not occur naturally as
559
+ * part of the data between separators.
560
+ * For example, imagine you want to send several small documents over a socket.
561
+ * Using CRLF as a separator is likely unwise, as a CRLF could easily exist within the documents.
562
+ * In this particular example, it would be better to use a protocol similar to HTTP with
563
+ * a header that includes the length of the document.
564
+ * Also be careful that your separator cannot occur naturally as part of the encoding for a character.
565
+ *
566
+ * The given data (separator) parameter should be immutable.
567
+ * For performance reasons, the socket will retain it, not copy it.
568
+ * So if it is immutable, don't modify it while the socket is using it.
547
569
**/
548
570
- (void )readDataToData : (NSData *)data
549
571
withTimeout : (NSTimeInterval )timeout
@@ -562,13 +584,22 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
562
584
* The read will complete successfully if exactly maxLength bytes are read and the given data is found at the end.
563
585
*
564
586
* If you pass nil or zero-length data as the "data" parameter,
565
- * the method will do nothing, and the delegate will not be called.
587
+ * the method will do nothing (except maybe print a warning) , and the delegate will not be called.
566
588
* If you pass a maxLength parameter that is less than the length of the data parameter,
567
- * the method will do nothing, and the delegate will not be called.
589
+ * the method will do nothing (except maybe print a warning) , and the delegate will not be called.
568
590
*
569
591
* To read a line from the socket, use the line separator (e.g. CRLF for HTTP, see below) as the "data" parameter.
570
- * Note that this method is not character-set aware, so if a separator can occur naturally as part of the encoding for
571
- * a character, the read will prematurely end.
592
+ * If you're developing your own custom protocol, be sure your separator can not occur naturally as
593
+ * part of the data between separators.
594
+ * For example, imagine you want to send several small documents over a socket.
595
+ * Using CRLF as a separator is likely unwise, as a CRLF could easily exist within the documents.
596
+ * In this particular example, it would be better to use a protocol similar to HTTP with
597
+ * a header that includes the length of the document.
598
+ * Also be careful that your separator cannot occur naturally as part of the encoding for a character.
599
+ *
600
+ * The given data (separator) parameter should be immutable.
601
+ * For performance reasons, the socket will retain it, not copy it.
602
+ * So if it is immutable, don't modify it while the socket is using it.
572
603
**/
573
604
- (void )readDataToData : (NSData *)data withTimeout : (NSTimeInterval )timeout maxLength : (NSUInteger )length tag : (long )tag ;
574
605
@@ -585,18 +616,28 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
585
616
* it is treated similarly to a timeout - the socket is closed with a GCDAsyncSocketReadMaxedOutError.
586
617
* The read will complete successfully if exactly maxLength bytes are read and the given data is found at the end.
587
618
*
588
- * If you pass a maxLength parameter that is less than the length of the data parameter,
589
- * the method will do nothing, and the delegate will not be called.
619
+ * If you pass a maxLength parameter that is less than the length of the data (separator) parameter,
620
+ * the method will do nothing (except maybe print a warning) , and the delegate will not be called.
590
621
* If the bufferOffset is greater than the length of the given buffer,
591
- * the method will do nothing, and the delegate will not be called.
622
+ * the method will do nothing (except maybe print a warning) , and the delegate will not be called.
592
623
*
593
- * If you pass a buffer, you must not alter it in any way while AsyncSocket is using it.
624
+ * If you pass a buffer, you must not alter it in any way while the socket is using it.
594
625
* After completion, the data returned in socket:didReadData:withTag: will be a subset of the given buffer.
595
- * That is, it will reference the bytes that were appended to the given buffer.
626
+ * That is, it will reference the bytes that were appended to the given buffer via
627
+ * the method [NSData dataWithBytesNoCopy:length:freeWhenDone:NO].
596
628
*
597
629
* To read a line from the socket, use the line separator (e.g. CRLF for HTTP, see below) as the "data" parameter.
598
- * Note that this method is not character-set aware, so if a separator can occur naturally as part of the encoding for
599
- * a character, the read will prematurely end.
630
+ * If you're developing your own custom protocol, be sure your separator can not occur naturally as
631
+ * part of the data between separators.
632
+ * For example, imagine you want to send several small documents over a socket.
633
+ * Using CRLF as a separator is likely unwise, as a CRLF could easily exist within the documents.
634
+ * In this particular example, it would be better to use a protocol similar to HTTP with
635
+ * a header that includes the length of the document.
636
+ * Also be careful that your separator cannot occur naturally as part of the encoding for a character.
637
+ *
638
+ * The given data (separator) parameter should be immutable.
639
+ * For performance reasons, the socket will retain it, not copy it.
640
+ * So if it is immutable, don't modify it while the socket is using it.
600
641
**/
601
642
- (void )readDataToData : (NSData *)data
602
643
withTimeout : (NSTimeInterval )timeout
@@ -612,6 +653,17 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
612
653
*
613
654
* If you pass in nil or zero-length data, this method does nothing and the delegate will not be called.
614
655
* If the timeout value is negative, the write operation will not use a timeout.
656
+ *
657
+ * Thread-Safety Note:
658
+ * If the given data parameter is mutable (NSMutableData) then you MUST NOT alter the data while
659
+ * the socket is writing it. In other words, it's not safe to alter the data until after the delegate method
660
+ * socket:didWriteDataWithTag: is invoked signifying that this particular write operation has completed.
661
+ * This is due to the fact that GCDAsyncSocket does NOT copy the data. It simply retains it.
662
+ * This is for performance reasons. Often times, if NSMutableData is passed, it is because
663
+ * a request/response was built up in memory. Copying this data adds an unwanted/unneeded overhead.
664
+ * If you need to write data from an immutable buffer, and you need to alter the buffer before the socket
665
+ * completes writing the bytes (which is NOT immediately after this method returns, but rather at a later time
666
+ * when the delegate method notifies you), then you should first copy the bytes, and pass the copy to this method.
615
667
**/
616
668
- (void )writeData : (NSData *)data withTimeout : (NSTimeInterval )timeout tag : (long )tag ;
617
669
0 commit comments