Skip to main content
GameDev.net gamedev.net
🔒 Locked

Encapsulating a network message

Started by backr3ap Apr 14, 2012 at 9:25 PM 7 replies 2.3k views
Original Post
backr3ap
backr3ap
Hello everyone!

I'm now messing around with some networking stuff and started making a small multiplayer game, but i've came to the point where i need to decide how to encapsulate a network message.

I want to make my messages like this: [Message total length] [MessageID] [Data]
Can someone, please, explain me how to go for it? I've tried to make such a class in many ways but none of them succeeded...
Any example or pseudo code?

I'm coding in C#
hasore
hasore
1 - Work with word data type messages ( 2 Bytes ).
2 - I think, u don't need to store message length, because a word value is 2 bytes ( 0-65535 ).

Client:
After the message, write the buffer/data.

Server:
In server, first read the message, after read buffer/data based on messageid.
backr3ap
backr3ap

1 - Work with word data type messages ( 2 Bytes ).
2 - I think, u don't need to store message length, because a word value is 2 bytes ( 0-65535 ).

Client:
After the message, write the buffer/data.

Server:
In server, first read the message, after read buffer/data based on messageid.


The thing is that i have no access to the source code of the client, I'm making a server emulator.
So i need to send the message length, sorry for not mentioning it.
rip-off
rip-off
If you're trying to talk to an existing protocol, you must generate your messages in exactly the way that protocol expects. We cannot really give general advice without knowing details of the protocol. The only things I can recommend you figure out is how long the message length field is (typically, some whole number of bytes) and what endianness it has.

Can you explain in detail the part that is causing you trouble?
hasore
hasore
You know the packet structure for the game?
backr3ap
backr3ap

You know the packet structure for the game?

Yes, the packet consists of such parts in order: first comes the length of the message, then the message ID, after that comes the data
The length is encoded in the first byte, the message ID in the next three bytes and then comes data.
hasore
hasore
Certain?
I don't know a 3 byte value type...
ShortInt, Byte = 1 byte
SmallInt, Word = 2 bytes
Float, Integer = 4 bytes
Double, Int64, Currency, Date = 8 bytes

I think, the message length and the message id is word or smallint type, because message len and message id ocuppies 4 bytes in total for storage.
Antheus
Antheus
I don't know a 3 byte value type...[/quote]

It's recession, ints are now 3 bytes.


---
But seriously.

Bytes in packet are encoding. If message IDs can only have values in range of 0..16777215, then they can be uniquely stored in 3 bytes.

How to handle those?

To read:id = a[0] * 256 * 256 + a[1] * 256 + a[2];
To write:a[2] = id % 256;
a[1] = (id / 256) % 256;
a[0] = (id / 256 / 256) % 256;
rip-off
rip-off

I don't know a 3 byte value type...
[/quote]
You don't even have to use full bytes if you really want to squeeze things together. For example, you could pack two small values into a single byte by using bitwise arithmetic, or encode a series of boolean values by using individual bits in a byte. Such space saving techniques aren't often used in memory (perhaps if you have millions of entries), but for serialisation to disk or to the network they become more common.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.