Reputation: 15501
I am using go
and protocol buffers
. go
program sends encoded protocol buffer messages to clients connected to the socket. Now since protocol-buffers is not delimited, clients don't know how much data to read from the socket.
I am planning to prefix the message with the message length, a 32bit integer. So clients can read 4 bytes, get the length and read the full message.
I can put an integer value into the bytes array using binary
package. Something like,
binary.Write(buf, binary.LittleEndian, value)
Now the question is, write
needs a byte order and how will the receiving end know what is the byte order? Is there a way to deal with this without specifying explicit byte order?
Upvotes: 1
Views: 1052
Reputation:
The convention is that network byte order is big endian, but your client and server must agree in any case. Specify the byte order explicitly.
Edit: Reading the Protobuf docs, it might be better to use proto.EncodeVarint
and proto.DecodeVarint
to write the length before the message, for the sake of consistency.
Upvotes: 6
Reputation: 1062600
You should always explicitly define and document the byte order (and layout) on such things. In general communications, big-endian direct layout seems to be the norm (so 47 would be 4 bytes with hex values 00 00 00 2F). However, you might want to take specific context into consideration. For example, if you are already talking protobuf, you could also use the "varint" encoding format, in which case 47 is a single byte: 2F - many protobuf implementations will provide pre-rolled methods to consume or produce a varint (otherwise, it is documented here - but can be summarised as 7-bit payload with continuation bit, with the least-significant 7-bit group first)
Upvotes: 2