Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- NSUInteger partialReadBufferLength = [partialReadBuffer length];
- if (partialReadBufferLength > 0)
- {
- // There are 3 types of read packets:
- //
- // 1) Read all available data.
- // 2) Read a specific length of data.
- // 3) Read up to a particular terminator.
- NSUInteger bytesToCopy;
- if (currentRead->term != nil)
- {
- // Read type #3 - read up to a terminator
- bytesToCopy = [currentRead readLengthForTermWithPreBuffer:partialReadBuffer found:&done];
- }
- else
- {
- // Read type #1 or #2
- bytesToCopy = [currentRead readLengthForNonTermWithHint:partialReadBufferLength];
- }
- // Make sure we have enough room in the buffer for our read.
- [currentRead ensureCapacityForAdditionalDataOfLength:bytesToCopy];
- // Copy bytes from prebuffer into packet buffer
- uint8_t *buffer = (uint8_t *)[currentRead->buffer mutableBytes] + currentRead->startOffset +
- currentRead->bytesDone;
- memcpy(buffer, [partialReadBuffer bytes], bytesToCopy);
- // Remove the copied bytes from the partial read buffer
- [partialReadBuffer replaceBytesInRange:NSMakeRange(0, bytesToCopy) withBytes:NULL length:0];
- partialReadBufferLength -= bytesToCopy;
- LogVerbose(@"copied(%lu) partialReadBufferLength(%lu)", bytesToCopy, partialReadBufferLength);
- // Update totals
- currentRead->bytesDone += bytesToCopy;
- totalBytesReadForCurrentRead += bytesToCopy;
- // Check to see if the read operation is done
- if (currentRead->readLength > 0)
- {
- // Read type #2 - read a specific length of data
- done = (currentRead->bytesDone == currentRead->readLength);
- }
- else if (currentRead->term != nil)
- {
- // Read type #3 - read up to a terminator
- // Our 'done' variable was updated via the readLengthForTermWithPreBuffer:found: method
- if (!done && currentRead->maxLength > 0)
- {
- // We're not done and there's a set maxLength.
- // Have we reached that maxLength yet?
- if (currentRead->bytesDone >= currentRead->maxLength)
- {
- error = [self readMaxedOutError];
- }
- }
- }
- else
- {
- // Read type #1 - read all available data
- //
- // We're done as soon as
- // - we've read all available data (in prebuffer and socket)
- // - we've read the maxLength of read packet.
- done = ((currentRead->maxLength > 0) && (currentRead->bytesDone == currentRead->maxLength));
- }
- }
Add Comment
Please, Sign In to add comment