I'm trying to find a simple, modular and idiomatic way of parsing a text based protocol for TCP streams.
Say the protocol looks like this: "[begin][length][blah][blah]...[blah][end][begin]...[end][begin]...[end]"
I'd like to correctly use streams (Transform?) to build a small component which just extracts individual messages (starts with [begin] and ends with [end]). Parsing to higher level data structures is left to other components.
I'm not so concerned about performance right now either so I'd just like to use a simple regex (this protocol is parsable with a regex).
I'm having trouble with a couple of concepts:
- Since the buffer may have not have a complete message, how do I correctly handle state and leave the partial message alone, to be parsed when more data comes in? Do I have to keep my own buffer or is there a way "put back" the data I didn't use?
- Since new data may contain several messages, can the
Transformstream handle multiple messages (like do I callthis.push(data);multiple times)?
(note that I'm trying to build this frame decoder outside of socket connection logic...I imagine it will be a class which extends stream.Transform and implement the read method)
via Shahbaz
No comments:
Post a Comment