
The idea is to describe your various data structures using a basic high-level language, and then generate the necessary code machinations to go between that description and byte streams automatically. This example uses a custom Perl script, although there are various standardized tools that can do the same thing (such as the ASN.1 standards suite).
An example of a packet description is as follows:
Begin("Packets.h"); StructDef(SystemGreeting => [ major => Int(0, 255), minor => Int(0, 255), host => String(), ]); StructDef(LoginRequest => [ id => Int(0, 32767), name => String(), password => String(), ]); StructDef(LoginResult => [ id => Int(0, 32767), player => Int(0, 32767), result => Int(0, 1), text => String(), ]); End();
This code does a few things:
1) It declares that the input file will generate a header called "Packets.h". Thus, in your C++ code, you will #include "Packets.h" to get access to the packet functions.
2) It declares three kinds of data packets: A system greeting, containing two small integers and a string; a login request, containing an integer and two strings, and a login result, containing two integers, a flag (an integer with only two values) and a string.
3) It ends with the marker "End()" which tells the Perl code to generate code for the definitions in the file.