There is no reason to make something so complicated. I see that you have an action identifier, so I assume that a certain number of actions will be committed.
For each action, you must define a data structure, and then you put each of these values in the structure. To send it by cable, you simply allocate sum (size.i) bytes for each element of your structure. So your package will look like this:
[action ID][item 1 (sizeof(item 1 bytes)][item 1 (sizeof(item 2 bytes)]...[item n (sizeof(item n bytes)]
The idea is that you already know the size and type of each variable on each side of the connection, so you do not need to send this information.
For strings, you can simply throw them into a form with zero completion, and then when you “know” to search for a string based on your package type, start reading and look for zero.
-
Another option is to use '\ r \ n' to define your variables. This will require some overhead, and you will have to use text rather than binary values for numbers. But that way you could just use readline to read each variable. Your packages will look like this:
[action ID] [item 1 (as text)] ... [item n (as text)]
-
Finally, just serializing objects and passing them by wire is a good way to do this too, with the least amount of code to write. Remember that you do not want to optimize prematurely as well as network traffic. If it turns out that you need to squeeze a little more performance later, you can go back and find out a more efficient mechanism.
And check google protocol buffers , which are supposedly an extremely fast way to serialize data in a neutral way, sort of like binary XML, but without nested elements. There is also JSON , which is another neutral coding platform. Using protocol buffers or JSON would mean you don't have to worry about how to specifically encode messages.