Hi everyone.
I’m re-implementing an old media-streaming socket using NSStream APIs (along with some CFNetwork and posix APIs for configuring the underlying sockets).
I’m creating my stream-pair simply like thus
::CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (__bridge CFStringRef)hostAddress, portNum, &readStream, &writeStream);
Later, when handling the NSStreamEventHasBytesAvailable event in my - (void)stream:handleEvent:
I’m reading the data awaiting using the normal
NSInteger readLength = [_inputStream read:buffer maxLength:maxReadLength];
My question: is there a special penalty or cost for calling the read:maxLength: frequently?
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 is simpler to implement and avoids further data copying, while the second approach reduces the frequency of calling read:maxLength:but will force me to copy messages off my internal buffer.
My messages are video/audio and similar. Audio messages are 25 times a second, video messages are roughly 32 a second.
Can anyone hint me about the better approach?
Thanks
|