Re: Trouble getting SSD Project working & Questions
Re: Trouble getting SSD Project working & Questions
- Subject: Re: Trouble getting SSD Project working & Questions
- From: "Quinn \"The Eskimo!\"" <email@hidden>
- Date: Tue, 19 Jul 2011 09:15:40 +0100
On 19 Jul 2011, at 02:45, email@hidden wrote:
> I can find myself in this kind of situation:
>
> 1. A blob of data arrives and a read event is generated
> 2. read( ... ) is called, but does not read in all of the data
> 3. Another event is generated results in read( ... ) being called again.
> 4. If all of the data has now been read, stop, otherwise return to #3
>
> Is this more correct?
Correct. The fact that read() can return less data than you request is a standard UNIXism. If you were doing this synchronously, you'd have a loop like this:
do {
calculate the number of bytes remaining in this message
read that many bytes
if (we got an error) {
break
}
if (we got all the bytes for the message) {
break
}
} while (true);
GCD just unravels this loop, so instead of looping your block gets queue each time a chunk of data arrives.
It's important to to note that this concept covers two cases:
o the message being delivered to your socket in chunks -- In this case the first read returns the first chunk and the second read blocks waiting for the second chunk (or, in GCD parlance, your block gets queued when the first chunk arrives and then, sometime later, when the second chunk arrives, your block gets queued again).
o read() returning short -- Even if all the data is sitting there waiting for you, read() is not guaranteed to read all of it. So the first read can return the first half of the message and then you turn around and call read again and that returns the second half of the message (and likewise with GCD).
S+E
--
Quinn "The Eskimo!" <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden