NSFileHandle readInBackground vs threading?
NSFileHandle readInBackground vs threading?
- Subject: NSFileHandle readInBackground vs threading?
- From: Alexander Bokovikov <email@hidden>
- Date: Tue, 08 Nov 2011 22:54:52 +0600
Hi, All,
I have a need to read some data from a local socket, which serves for
IPC. And data may come very quickly, so (AFAIU) inner socket buffer
might overflow, so a portion of data might be lost. I don't see a way
how to define an inner system buffer size, so the only I can is to do
my best to read from the socket quickly enough. The problem is that I
need yet to process the incoming data, not only to read them. Now I'm
doing the next:
- (void) readPacket:(NSNotification *)aNotification {
[packetBuffer appendData:[[aNotification userInfo]
objectForKey:NSFileHandleNotificationDataItem]];
[sockFileHandle readInBackgroundAndNotifyForModes:modesArray];
[self processPacket];
}
where packetBuffer is the storage for incoming data and processPacket
is where data are processed.
My question is:
Isn't it better to do it in this way:
- (void) readPacket:(NSNotification *)aNotification {
[packetBuffer appendData:[[aNotification userInfo]
objectForKey:NSFileHandleNotificationDataItem]];
[sockFileHandle readInBackgroundAndNotifyForModes:modesArray];
[self performSelector:@selector(processPacket) withObject:nil
afterDelay:0];
}
Or is the multithreaded processing the only (or at least much better)
solution here? If yes, then may I be sure that NSData appendData (see
above) will never relocate the initial portion of the data buffer, but
only will add new data to the end of buffer? My data processing
routine looks like the next:
................
len = [packetBuffer length];
ptr = [packetBuffer bytes];
while (len >= MIN_PACKET_SZ) {
/// doing something with data pointed by ptr->
ptr += _some_value;
len -= _some_value;
}
................
So, I need be sure that once reading the ptr, then increasing it, step
by step up to the len value, I'll always have valid data despite of
how many times append data will be called in another thread. What
about this?
Thanks in advance.
-Alex
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden