readMinimumLength(_:maximumLength:completionHandler:)
Read the requested range of bytes.
Declaration
func readMinimumLength(_ minimum: Int, maximumLength maximum: Int, completionHandler completion: @escaping (Data?, (any Error)?) -> Void)Parameters
- minimum:
The minimum number of bytes the caller wants to read.
- maximum:
The maximum number of bytes the caller wants to read.
- completion:
The completion handler to be invoked when data has been read or an error occurred.
Discussion
The completion handler will be invoked when:
The exact number of requested bytes have been read;
datawill be non-nil.Fewer than the requested number of bytes, or no bytes, have been read, and the connection’s read side has been closed.
datamight benil, depending on whether there was any data to be read when the connection’s read side was closed.Some fatal error has occurred, returned in
error, anddatawill be nil.
To know when to schedule a read again, check for the condition whether an error has occurred.
For better performance, the caller should pick the effective minimum and maximum lengths. For example, if the caller absolutely needs a specific number of bytes before it can make any progress, use that value as the minimum. The maximum bytes can be the upperbound that the caller wants to read. Typically, the minimum length can be the caller protocol fixed-size header and the maximum length can be the maximum size of the payload or the size of the current read buffer.