Assuming...
- you are talking about converting to a buffer of bytes
- You are using UDP and performance is a concern
Try to avoid wasting space in your packet for defining structure.
I.E. send, at minimum, a byte to denote the type of packet, then just assume each packet received follows the predefined structure for that type of packet
Should I just read structs into the socket? Use proticol
buffers/thrift?
- Yes, read the whole struct IF you NEED the whole struct
- No, make the packet structure yourself, This will surely be smaller than serialization using these methods; you should know exactly what data the packet should include
How should I represent arrays of data?
- As arrays of data. When receiving continue reading the buffer until end of data to avoid sending a Count of the array's elements
What should the interface look like for packing/unpacking data?
- You could easily setup a bunch of methods to convert basic types to bytes, from there build on these methods to convert custom types as well. The specifics on how to do this could be found almost anywhere I'm sure (I use C# personally)
One last thing, packet size is a concern, especially for a snapshot: size = packetSize x entities x connectedPlayers;
So you might have 60 x 10 x 16 = 9,600 bytes per packet
Then sending this 20 times a second: = 192,000 bps = 187 KBps. This is obviously a high bandwith upload speed. Thus the need to minimize each of the factors contributing to packet size where possible.
This article has helped me immensely:
Valve Multiplayer Networking