• Advertisement
Sign in to follow this  

Encapsulating a network message

This topic is 2104 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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#

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
[quote name='hasore' timestamp='1334448075' post='4931285']
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.
[/quote]

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[quote name='hasore' timestamp='1334522552' post='4931528']
You know the packet structure for the game?
[/quote]
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote]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:[code]id = a[0] * 256 * 256 + a[1] * 256 + a[2];[/code]
To write:[code]a[2] = id % 256;
a[1] = (id / 256) % 256;
a[0] = (id / 256 / 256) % 256;[/code]

Share this post


Link to post
Share on other sites
[quote]
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement