Creating a network message from my game object

Started by
1 comment, last by thelovegoose 16 years, 1 month ago
Enet, the networking library we are using sends messages where the body of the message is an unsigned char array. I want to be able to send a dynamicly sized list of IDs. 1) How should I write the byte representation of the integer ids into the unsigned char array? (Ass opposed to writing the character representation of the integer)? 2) In what clever way could I make it so that, if I had 100 units selected, I wouldn't have to send 100 * sizeof(unitID)? 3) How can I design the message so that I can correctly parse it at the other end no matter how many IDs are being sent? Many Thanks in advance!
Advertisement
Quote:Original post by thelovegoose
1) How should I write the byte representation of the integer ids into the unsigned char array? (Ass opposed to writing the character representation of the integer)?


A typical approach is to send a custom byte representation (to be endianness-independent). For instance, The NGrid serializer, for instance, uses an ordinal system which uses one byte for integers 0-127, two bytes for integers 128-16383, three bytes for integers 16384-2097151, four bytes for integers 2097152-268435456, and five bytes for any greater 32-bit integers.

Quote:2) In what clever way could I make it so that, if I had 100 units selected, I wouldn't have to send 100 * sizeof(unitID)?


Sort them, then send the first and the deltas as ordinals.

Quote:3) How can I design the message so that I can correctly parse it at the other end no matter how many IDs are being sent?


Send the number of ordinals as an ordinal, then read that many ordinals from the stream. Be wary of DoS attacks where a computer sends you a very high ordinal count and expects you to store it all—reject requests which contain more ordinals than there are objects in the game.

Quote:
Quote:Original post by thelovegoose
1) How should I write the byte representation of the integer ids into the unsigned char array? (Ass opposed to writing the character representation of the integer)?


A typical approach is to send a custom byte representation (to be endianness-independent). For instance, The NGrid serializer, for instance, uses an ordinal system which uses one byte for integers 0-127, two bytes for integers 128-16383, three bytes for integers 16384-2097151, four bytes for integers 2097152-268435456, and five bytes for any greater 32-bit integers.


Okay, so reserve a bit to say if there is another byte following describing this same integer, should be simple enough to do.

Quote:
Quote:2) In what clever way could I make it so that, if I had 100 units selected, I wouldn't have to send 100 * sizeof(unitID)?


Sort them, then send the first and the deltas as ordinals.


By deltas do you mean the integer differences between them? Thats a very clever idea...

Quote:3) How can I design the message so that I can correctly parse it at the other end no matter how many IDs are being sent?


Quote:Send the number of ordinals as an ordinal, then read that many ordinals from the stream. Be wary of DoS attacks where a computer sends you a very high ordinal count and expects you to store it all—reject requests which contain more ordinals than there are objects in the game.


Okay, I'll bear this in mind.

Thanks very much, probably the most useful answers I've had for some time :)

This topic is closed to new replies.

Advertisement