Encapsulating a network message

Started by
7 comments, last by rip-off 12 years ago
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#
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.

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.
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?
You know the packet structure for the game?

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.
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.
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;

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.

This topic is closed to new replies.

Advertisement