Jump to content
  • Advertisement
Sign in to follow this  
tordyvel

Network messages

This topic is 4555 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

I've read trough a lot of articles, forum posts and tried to google on it, but haven't really found any good information about it. What I want to know is how to build your network message packages. Let's for example say that I want to send a player position of 2 bytes (x and y). To send this i would have to manualy build some sort of byte array and probably put in another byte to identyfy the message. To me this seems realy ugly because then my code would be like: recieveMsg(msg); unsigned char id = msg[0]; switch(id) case 0: unsigned char x = msg[1]; unsigned char y = msg[2]; doStuff(x,y); case 1: .... Are there any sweet programming patterns or any higer level network lib that helps you with these things? Something like the class stream in java would be a bit sweet.

Share this post


Link to post
Share on other sites
Advertisement
I really wouldnt know - however whenever I've dealt with sending and recieving packets I always felt that it was best to build a custom packet. This way you avoid unneccessary overhead. The packets will always be a minimum size which will help you out in the long run.

Share this post


Link to post
Share on other sites
You should probably look into various ways of achieving runtime reflection for data types in your system. By that, I simply mean that you need a way to define various structures, and get code that marshals/demarshals this data into/out of network packet buffers; ideally with some registry for message types.

Your network code then looks somewhat like:


struct MoveMessage {
int xpos;
int ypos;
};
struct TakeDamageMessage {
int amount;
DamageType type;
};


MoveMessage mm;
mm.xpos = target_x;
mm.ypos = target_y;
movingEntity->addMessage( mm );

TakeDamageMessage tdm;
tdm.amount = damage_amount;
tdm.type = DamageTypeExplosive;
target->addMessage( tdm );


You'd make this happen by making your addMessage() a template on its target struct, and using meta-data available on this struct to actually marshal/demarshal the data. That metadata could describe what amount of bits, precision, compression etc you use for that kind of data.

Slickest is with self-deriving data structures, although a system that either looks at a header and generates code, or just uses macros or custom marshal/demarshal functions written by the user, will also work.

The movingEntity / target objects would in turn know how to serialize their own internal ID into the message, so that the receiving end knows how to unpack the message and forward it to the appropriate recipient.

Share this post


Link to post
Share on other sites
I use a Type identifier for my messages. This cleans up my switch statements greatly.

I have Movement, Server, Ping (to clean up dead clients), and Social so far.

I do a switch on the main type, and then do processing on a smaller subset of a switch.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!