Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to define custom rule for splitting read streams into packets #27

Closed
gtennis opened this issue Feb 20, 2012 · 2 comments
Closed

How to define custom rule for splitting read streams into packets #27

gtennis opened this issue Feb 20, 2012 · 2 comments

Comments

@gtennis
Copy link

gtennis commented Feb 20, 2012

I rewrote the question to be more precise on the solution I'm looking for:

Is it possible to do the following with AsyncSocket:

while ("response packet" is not read) do {

  1. Read at least first 5 bytes from read stream, and extract "response packet" length size from that.
  2. Read further until reached reading amount of bytes defined in "response packet" header.
  3. If reached the end of read stream but some bytes are lacking then wait for another read stream and finish reading the "response packet" then.
  4. Copy read bytes into buffer and mark that read data as "response packet" data.
  5. Do callback to my delegate onSocket:didReadData:withTag: with that data.
  6. clear buffer (response packet) and do the same steps with the next read streams.

}

Here's my current usage of AsyncSocket:

I'm writing a data via socket with AsyncSocket:

[_socket writeData:request withTimeout:commandData tag:commandTag];

and receiving a callback in my delegate class:

onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag

The problem is, there's no terminating char in data stream and the only way to distinguish the length of comming "response packets" is to read and parse their first 5 bytes and use that for cutting data into "packets" and making callbacks to my delegate. Currently, I'm using AsyncSocket as shown in code fragment above, and some callbacks "onSocket:didReadData:withTag:" are already good length data packets (received NSData), but some are merged. IMHO I need explicitly to tell AsyncSocket how to interpret and cut coming data into packets when reading.

Original post you can find here: http://stackoverflow.com/questions/9359391/asyncsocket-getting-merged-two-packets-instead-of-separate-two-packets

@yvbeek
Copy link

yvbeek commented Feb 25, 2012

I have a similar situation.

The first byte of data tells me the length of the rest of the packet. Header packets are in my case recognisable by bit 0 = 1, bit 4 = 0, bit 7 = 0. Is there an easy way to do this with (GDC)AsyncSocket?

Is the proper way to do a Read-with-length: 1 until I encounter a header packet and then Read-with-length: bodySize?

@gtennis
Copy link
Author

gtennis commented Mar 2, 2012

Zyphrax, I found the solution (at least for situations when for every request you receive a response). Yes, you need to call readToLength in onSocket:didWriteDataWithTag: in AsyncSocket delegate method:

- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag {

  ...
  [sock readDataToLength:<preferred size of response packet> withTimeout:myTimeout tag:tag];

  //instead of
  //[sock readDataWithTimeout:myTimeout tag:tag];
}

If you don't use terminator then for every request you make (for every write to server) you need to tell AsyncSocket when to callback on read.

However, was not able to deal with subscription type events when handling data coming from a server without a request. Here's my post on stackoverflow http://stackoverflow.com/questions/9517101/is-asyncsocket-suitable-for-subscription-events
I'm closing the issue.

@gtennis gtennis closed this as completed Mar 2, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants