Guest User

Untitled

a guest
Dec 11th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. NSUInteger partialReadBufferLength = [partialReadBuffer length];
  2.  
  3. if (partialReadBufferLength > 0)
  4. {
  5. // There are 3 types of read packets:
  6. //
  7. // 1) Read all available data.
  8. // 2) Read a specific length of data.
  9. // 3) Read up to a particular terminator.
  10.  
  11. NSUInteger bytesToCopy;
  12.  
  13. if (currentRead->term != nil)
  14. {
  15. // Read type #3 - read up to a terminator
  16.  
  17. bytesToCopy = [currentRead readLengthForTermWithPreBuffer:partialReadBuffer found:&done];
  18. }
  19. else
  20. {
  21. // Read type #1 or #2
  22.  
  23. bytesToCopy = [currentRead readLengthForNonTermWithHint:partialReadBufferLength];
  24. }
  25.  
  26. // Make sure we have enough room in the buffer for our read.
  27.  
  28. [currentRead ensureCapacityForAdditionalDataOfLength:bytesToCopy];
  29.  
  30. // Copy bytes from prebuffer into packet buffer
  31.  
  32. uint8_t *buffer = (uint8_t *)[currentRead->buffer mutableBytes] + currentRead->startOffset +
  33. currentRead->bytesDone;
  34.  
  35. memcpy(buffer, [partialReadBuffer bytes], bytesToCopy);
  36.  
  37. // Remove the copied bytes from the partial read buffer
  38. [partialReadBuffer replaceBytesInRange:NSMakeRange(0, bytesToCopy) withBytes:NULL length:0];
  39. partialReadBufferLength -= bytesToCopy;
  40.  
  41. LogVerbose(@"copied(%lu) partialReadBufferLength(%lu)", bytesToCopy, partialReadBufferLength);
  42.  
  43. // Update totals
  44.  
  45. currentRead->bytesDone += bytesToCopy;
  46. totalBytesReadForCurrentRead += bytesToCopy;
  47.  
  48. // Check to see if the read operation is done
  49.  
  50. if (currentRead->readLength > 0)
  51. {
  52. // Read type #2 - read a specific length of data
  53.  
  54. done = (currentRead->bytesDone == currentRead->readLength);
  55. }
  56. else if (currentRead->term != nil)
  57. {
  58. // Read type #3 - read up to a terminator
  59.  
  60. // Our 'done' variable was updated via the readLengthForTermWithPreBuffer:found: method
  61.  
  62. if (!done && currentRead->maxLength > 0)
  63. {
  64. // We're not done and there's a set maxLength.
  65. // Have we reached that maxLength yet?
  66.  
  67. if (currentRead->bytesDone >= currentRead->maxLength)
  68. {
  69. error = [self readMaxedOutError];
  70. }
  71. }
  72. }
  73. else
  74. {
  75. // Read type #1 - read all available data
  76. //
  77. // We're done as soon as
  78. // - we've read all available data (in prebuffer and socket)
  79. // - we've read the maxLength of read packet.
  80.  
  81. done = ((currentRead->maxLength > 0) && (currentRead->bytesDone == currentRead->maxLength));
  82. }
  83.  
  84. }
Add Comment
Please, Sign In to add comment