I am writing erlang tcp-server which runs following protocol.
- Each packet has exactly 4 bytes size.
- There is one special-case packet -
<<?SPECIAL_BYTE, 0, PayloadLength:2/big-unsigned-integer-unit:8>>
. This packet indicates, that server must read nextPayloadLength
bytes of raw data.
I can receive raw stream of data and parse this protocol in erlang code, of course. But I wonder, is there any way to use builtin erlang packet packaging? When my packets preceded with its length, I can say [{packet, HeaderLength}]. Is there any way to force erlang to automatically package received data by 4-bytes chuks?
UPD: I am planning to use {active, once} mode. Also I can use gen_tcp:recv(Socket, 4), but I afraid performance penalty due multiple socket reads in this case. Is my fear justified?