GCDAsyncReadPacket Class Reference

Inherits from NSObject
Declared in GCDAsyncSocket.m

Overview

The GCDAsyncReadPacket encompasses the instructions for any given read. The content of a read packet allows the code to determine if we’re: - reading to a certain length - reading to a certain separator - or simply reading the first chunk of available data

– ensureCapacityForAdditionalDataOfLength:

Increases the length of the buffer (if needed) to ensure a read of the given size will fit.

- (void)ensureCapacityForAdditionalDataOfLength:(NSUInteger)bytesToRead

Discussion

Increases the length of the buffer (if needed) to ensure a read of the given size will fit.

Declared In

GCDAsyncSocket.m

– optimalReadLengthWithDefault:shouldPreBuffer:

This method is used when we do NOT know how much data is available to be read from the socket. This method returns the default value unless it exceeds the specified readLength or maxLength.

- (NSUInteger)optimalReadLengthWithDefault:(NSUInteger)defaultValue shouldPreBuffer:(BOOL *)shouldPreBufferPtr

Discussion

This method is used when we do NOT know how much data is available to be read from the socket. This method returns the default value unless it exceeds the specified readLength or maxLength.

Furthermore, the shouldPreBuffer decision is based upon the packet type, and whether the returned value would fit in the current buffer without requiring a resize of the buffer.

Declared In

GCDAsyncSocket.m

– readLengthForNonTermWithHint:

For read packets without a set terminator, returns the amount of data that can be read without exceeding the readLength or maxLength.

- (NSUInteger)readLengthForNonTermWithHint:(NSUInteger)bytesAvailable

Discussion

For read packets without a set terminator, returns the amount of data that can be read without exceeding the readLength or maxLength.

The given parameter indicates the number of bytes estimated to be available on the socket, which is taken into consideration during the calculation.

The given hint MUST be greater than zero.

Declared In

GCDAsyncSocket.m

– readLengthForTermWithHint:shouldPreBuffer:

For read packets with a set terminator, returns the amount of data that can be read without exceeding the maxLength.

- (NSUInteger)readLengthForTermWithHint:(NSUInteger)bytesAvailable shouldPreBuffer:(BOOL *)shouldPreBufferPtr

Discussion

For read packets with a set terminator, returns the amount of data that can be read without exceeding the maxLength.

The given parameter indicates the number of bytes estimated to be available on the socket, which is taken into consideration during the calculation.

To optimize memory allocations, mem copies, and mem moves the shouldPreBuffer boolean value will indicate if the data should be read into a prebuffer first, or if the data can be read directly into the read packet’s buffer.

Declared In

GCDAsyncSocket.m

– readLengthForTermWithPreBuffer:found:

For read packets with a set terminator, returns the amount of data that can be read from the given preBuffer, without going over a terminator or the maxLength.

- (NSUInteger)readLengthForTermWithPreBuffer:(GCDAsyncSocketPreBuffer *)preBuffer found:(BOOL *)foundPtr

Discussion

For read packets with a set terminator, returns the amount of data that can be read from the given preBuffer, without going over a terminator or the maxLength.

It is assumed the terminator has not already been read.

Declared In

GCDAsyncSocket.m

– searchForTermAfterPreBuffering:

For read packets with a set terminator, scans the packet buffer for the term. It is assumed the terminator had not been fully read prior to the new bytes.

- (NSInteger)searchForTermAfterPreBuffering:(ssize_t)numBytes

Discussion

For read packets with a set terminator, scans the packet buffer for the term. It is assumed the terminator had not been fully read prior to the new bytes.

If the term is found, the number of excess bytes after the term are returned. If the term is not found, this method will return -1.

Note: A return value of zero means the term was found at the very end.

Prerequisites: The given number of bytes have been added to the end of our buffer. Our bytesDone variable has NOT been changed due to the prebuffered bytes.

Declared In

GCDAsyncSocket.m