On Sep 9, 2015, at 2:07 PM, Motti Shneor < email@hidden> wrote:
I need reading messages off the stream, each prefixed by 4 bytes containing its size. I was wondering if it was advisable to first read just the size (4 bytes) then read the rest of the message, or maybe it was better to have a big buffer into which to “drain” the stream, then parse my messages off the buffer?
The first approach won’t work, because the amount of data received is unpredictable. For instance you might receive only three bytes, which isn’t enough to parse the data size. Or you might be able to read the data size, but then try to read a 100000-byte message and get only 1500 bytes of it.
The usual approach when parsing data asynchronously from a network stream is to keep a variable-size buffer of unparsed data. When data arrives, you read all of it and append it to the buffer; then you try to parse the data in the buffer. If there’s not enough to parse yet, you give up till there’s more; or if you parse some data, you remove it from the start of the buffer.
So in your case you’d first check whether the buffer has at least four bytes; if so, you look at the length, and check whether that many more bytes are available. If they are, you read the whole message and remove it from the buffer.
An NSMutableData object works well as a buffer, if you’re not too concerned about high performance. The fastest implementation is probably some sort of custom ring buffer.
—Jens |