During the realization of the Babel project, I had to work on a protocol system, to enable interactions between the client and the server.
If you want more details about the base project, feel free to click here.
Tech stack
Programming language: C++
The protocol system does not depend on anything else.
Architecture
PacketManager
Global functions
deserialize(std::string &packet)
1. message[0] == '0x32'?
We check if the first byte is our magic value.
2. PacketType = message[1]
We retrieve the packet type from the next byte.
3. PacketField[] = message[2..]
We convert the rest of the content into a list of packet field.
4. Packet = Deserialize(PacketType, PacketField[])
We call the deserialize function for the specific packet type, with the list of packet fields.
serialize(Packet&)
It will simply call the serialize inherited function from the Packet class.
A trivial implementation of the handlePacket function is to simply loop through all PacketHandlers, to target the one registered for the received packet.
You can also use a HashMap<PacketType, PacketHandler>.