Jump to content
  • Advertisement
Sign in to follow this  
vidhvat

Simple Multiplayer Messaging Protocol

This topic is 2765 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 want to know how a game encodes it's messages that are sent to all the other users playing the game.
What I have is a simple game, with characters and objects. Players, as characters in the world, can pickup, drop, give, receive and interact with these objects. Currently, in the engine, every object and character has a url, more like a code to identify it, a single byte in this case (char).


I have no prior experience with network programming, but right now I have a thin layer programmed over UDP that allows me to send and receive simple packets in a reliable fashion.
What I want to know is, how do I pack all this information, in a generalized manner? What I can think of, is give every function a code, like, dropping can be 1, picking up can be 2, giving can be 3 etc. Then just send the character code, object code, and the function number. This information can be decoded and performed on all the other clients. Is there a more elegant way of doing this?

Share this post


Link to post
Share on other sites
Advertisement
More or less, that is how I would do it. But, I would personally send it as a data structure.

For example;

struct Action
{
BYTE actionCode;
BYTE characterCode;
BYTE objectCode;
}


Then send something like so;

send(socket,&ActionInstance,sizeof(action));

That way you are sending only 3 bytes (in the example) + UDP overhead and you data is easy to re-construct at the other end by copying the data directly back into a temporary struct (of the same properties).

By the way, when you say reliable UDP layer. Do you take into account packet loss, out of order packets, etc?

Good luck with your project and hope this helps. :)

Share this post


Link to post
Share on other sites
So, I was pretty much on the right track there. Thanks!
[color=#1C2837][size=2]By the way, when you say reliable UDP layer. Do you take into account packet loss, out of order packets, etc?[/quote]
Yes I do :-)

Share this post


Link to post
Share on other sites
Good introduction in my opinion is this on the wiki. Covers most of the basics. A key thing to notice is that you should probably be writing a lot of things to a single packet. (Keep them under 1400 bytes usually).

Share this post


Link to post
Share on other sites
Bitfields.

I don't think it's necessary, if I am going to use up all the possible values a byte can represent! This would be a good thing to use where I have smaller values that are being sent, for example, booleans! Nice link though, thanks!

So here's another question. Is it true that most clients just send their user input to the server? I wonder if it really works that way. For example, in my situation, I can send messages(via console) to objects in the world. Right now, I am sending the character code, the object code and the action. Would it be fine if I didn't send the object code and let the other clients figure it out themselves as to which object is closest to the player.

I think, It's a gamble really, because if the position is not synced, we're in big trouble! Right?

Share this post


Link to post
Share on other sites

I think, It's a gamble really, because if the position is not synced, we're in big trouble! Right?



The server can send its position for the client entity to the client once in a while, so the client can snap back in sync if it's not where it should be.

Share this post


Link to post
Share on other sites
The bottom line is never let you client tell the server what to do. (Never trust the client).

Send requests to the server instead - like "may I open the door please". In this case the server might respond "no you may not, you are nowhere near a door".

You can, however, use the client to predict that the server will be ok with things most of the time and perform the task anyway (as a means of making lag look invisible).

But, if the server responds "you cant do that", then snap back to where the client should be as hplus0603 suggested.

Also a good idea to log the amount of rejected requests too and the disconnect the client when it hits a threshold. As, then you may be dealing with a hacked client.

Share this post


Link to post
Share on other sites

unsure.gif

Okay, brace yourselves, here it comes:
I'm doing P2P!





I am too. Considering i'm not concerned with cheating, I think P2P is the easiest way for me. For player movement, the players send updates with position and velocity every X mS via unreliable UDP, for acquiring items from the map, it send reliable UDP, and I'll have it send the position and velocity of bullets it shoots reliably as well.

I just didn't feel like putting in the extra work for Client-Server, and it was for fun.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!