Variable Length Binary packets

56 views
Skip to first unread message

John Pruitt

unread,
Nov 16, 2016, 7:32:15 AM11/16/16
to Dart Server-side and Cloud Development
My Dart-VM application has a socket open to a remote server that is sending multiple variable length BINARY packets to me.  The value of the first two bytes of each packet is the total length of that packet.  It's possible, even likely the packet may not be complete or even some of the next packet's bytes are available at the moment I'm notified.  As far as I've found, ALL the available bytes in the stream have already been read into a buffer before I'm notified of anything.  Is there a way of "peeking" at the pending stream to read the first two bytes and then make sure all are available before reading JUST the expected quantity of bytes or do I have to do my own buffering and splitting/joining of multiple packets?
Here is the code I'm using:
ServerSocket.bind(InternetAddress.ANY_IP_V4, 8123).then
(
(ServerSocket server)
{
server.listen(handleSocket);
}
);

void handleSocket(Socket client){
client.listen((data){
int pkLen=data.length;
   //By the time it gets to this line,
   //data has already been read from the stream
   //and may be incomplete or contain extra data

Thanks in advance,
John

Alex Haslam

unread,
Nov 16, 2016, 7:45:21 AM11/16/16
to John Pruitt, Dart Server-side and Cloud Development
I don't think there is a way to "peek" but, could you add your packet to a queue and then just check the next packet in the Queue?
e.g.

```
List<List<int>> queue = new List();
queue.add();
queue.removeLast();
queue[0]; //peek at next
Please excuse the terrible example.

--
You received this message because you are subscribed to the Google Groups "Dart Server-side and Cloud Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cloud+un...@dartlang.org.
Visit this group at https://groups.google.com/a/dartlang.org/group/cloud/.

Natalie Weizenbaum

unread,
Nov 16, 2016, 3:12:03 PM11/16/16
to Alex Haslam, John Pruitt, Dart Server-side and Cloud Development
If you want direct control over the low-level interface, you might consider using the RawSocket class.

John Pruitt

unread,
Nov 16, 2016, 3:22:18 PM11/16/16
to Dart Server-side and Cloud Development, champ...@gmail.com, john.p...@gmail.com

Thanks, that is very helpful.
I wasn't aware of the RawSocket class.
Reply all
Reply to author
Forward
0 new messages