robbiehanson/CocoaAsyncSocket — Issue #31: Is AsyncSocket suitable for subscription events?
Author: centurionas
Created: 2012-03-02 07:12:26+00:00
State: open

Currently, I'm trying to use AsyncSocket and I see its not suitable for handling subscription type events (reading data that came from a server without request (write)). I was unable to find related info on its wiki. Everything is OK when every write you make (request) is paired with a read (a server sends a response). AsyncSocket manages it via its tags. But in my case, I also have subscription functionality where I'm writing to server a request like "start getting location updates from a server" and then I'm receiving multiple responses (reads), so they are not paired with writes.

First, I'v added a callback in AsyncSocket doBytesAvailable that notified my delegate object when such read occurs:

- (void)doBytesAvailable {
// If data is available on the stream, but there is no read request, then we don't need to process the data yet.

// Also, if there is a read request but no read stream setup, we can't process any data yet.

if (theReadStream == NULL)

return;

if (theCurrentRead == nil) {

// My fix: Notify about received data for missing read:
[theDelegate didReceiveEventsFromSubscription];
return;
}

But the length of "response" packets is variable, so I need to grab existing read data, extract "response" length, make callback to my delegate with that data and then call readToLength on socket. I'm not very comfortable with making code modifications to AsyncSocket internal code. Just curious are there any options or am I missing smith?