Jump to content

  • Log In with Google      Sign In   
  • Create Account

Sending structures through UDP sockets


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 sandorlev   Members   -  Reputation: 193

Like
0Likes
Like

Posted 11 February 2013 - 10:27 AM

Hey GD!

 

I was thinking about implementing an online server-client game and here is what I came up with:

1. The game is driven by events (if a player gets shot, an event is generated, etc).

2. The event is converted to some sort of format and sent to the server.

3. The server sends it back to all clients but the original sender.

4. The clients convert it back and make the necessary changes in game logic.

 

Does that sound like it could work? I haven't been able to think about a better solution myself.

 

Now the real question is (if my solution makes sense), what format should I use to send my event classes through UDP sockets? Should I just convert their data to strings? Are there popular solutions to this?



Sponsor:

#2 hplus0603   Moderators   -  Reputation: 5711

Like
2Likes
Like

Posted 11 February 2013 - 11:19 AM

The FAQ for this forum has some pointers about this.
Typically, you will serialize your data into some efficient binary format.
Additionally, you'll send a single UDP datagram with typically many messages/events in it, for each network tick.
enum Bool { True, False, FileNotFound };

#3 samoth   Crossbones+   -  Reputation: 5033

Like
0Likes
Like

Posted 11 February 2013 - 11:26 AM

If your event structure does not contain any pointers, and if your software does not need to be endian-portable (e.g. client runs on x86 and server runs on PPC), and if you use the same layout/padding options when compiling (and assuming trivial constructors/destructors, likely the case here) then you can usually just copy sizeof(event) bytes from an event's address to your packet.

In every other case, see the previous answer ("serialization"). Implementations that are well-tested and work for many people are msgpack and protocol buffers (with slightly different features and overhead).

Note: If you run your own serialization, be sure to also include at least a "protocol version" when your client connects, even if you'll never going to need it. In case you do need to change something later, you avoid one big nightmare.

#4 smr   GDNet+   -  Reputation: 1681

Like
0Likes
Like

Posted 11 February 2013 - 01:39 PM

If your event structure does not contain any pointers, and if your software does not need to be endian-portable (e.g. client runs on x86 and server runs on PPC), and if you use the same layout/padding options when compiling (and assuming trivial constructors/destructors, likely the case here) then you can usually just copy sizeof(event) bytes from an event's address to your packet.

 

This certainly is a way you COULD implement this, and it might work in a situation where there is only one type of data structure ever being sent, and it's sent infrequently enough that you won't incur too much overhead due to the UDP protocol, but I wouldn't ever recommend it. I can't imagine this ever working well -- or even working, for that matter -- for a game. As hplus said, you're going to want to combine more than one event into a packet, and in order to do that you're going to need some additional information in your packet to inform the receiver about how it should interpret those raw bytes. Furthermore, UDP is not ordered or guaranteed delivery, which can work well for a networked game, but there are some issues to be aware of and this will affect how your game constructs the data to be sent.



#5 Xanather   Members   -  Reputation: 712

Like
0Likes
Like

Posted 13 February 2013 - 08:40 AM

Hey GD!

 

I was thinking about implementing an online server-client game and here is what I came up with:

1. The game is driven by events (if a player gets shot, an event is generated, etc).

2. The event is converted to some sort of format and sent to the server.

3. The server sends it back to all clients but the original sender.

4. The clients convert it back and make the necessary changes in game logic.

 

Does that sound like it could work? I haven't been able to think about a better solution myself.

 

Now the real question is (if my solution makes sense), what format should I use to send my event classes through UDP sockets? Should I just convert their data to strings? Are there popular solutions to this?

If your new to networking I really think just using TCP would just be easier.

 

TCP is stream based, you don't have to worry about packing multiple messages into a network segment/packet. You also wont have to deal with issues like packet loss.

TCP really is not that slow like people say it is, it can become crippled on unstable connections otherwise it works quite well. Last I heard World of Warcraft uses TCP still and I have 250 millisecond of lag from australia -> america which is pretty average. Otherwise, if you can handle the required UDP work then I'm wrong use UDP all you want :D.

 

(following text will probably work more ideally in a TCP environment, with UDP you'd probably have to do what hplus said, 2nd post).

 

Also instead of converting data to strings (I used to do this when I first started networking) instead just send message types as bytes, no conversion needed... A player moved? Ok that could be byte 0, a player jumped? that could be byte 1. Prefixing messages with these bytes I think would be a better idea. Each message type could also have its own structure, say, for message 0 (a player moved)... It could look something like this:

{message byte (0)} {player who moved} {direction in which they moved} {current local position} {current local velocity} etc.. etc...

 

This is a example of a client->server message.

 

Once the server receives this it can then process it in a specific way (depending on the message type). For example in this case the player moved (message 0) so it can compare the current position of the player to the one in the message to see if the player is cheating (x/y/z has changed too much relative to current stored x/y/z). If the bounds are met then the player position is changed etc. etc.. server sends out a different type of server->client message to all the players but the source player indicating that the player moved.

 

Doing this means you probably wont need to serialize, saving loads of bandwidth.

 

I hope that helps, I'm still sort of new to networking myself.



#6 Kanadaj   Members   -  Reputation: 124

Like
0Likes
Like

Posted 28 February 2013 - 04:59 PM

About TCP/UDP, read this article: http://gafferongames.com/networking-for-game-programmers/udp-vs-tcp/

It describes the cons and pros of TCP, and even though he strongly advices against TCP, RTS games have a reason for using it.


Edited by Kanadaj, 28 February 2013 - 05:01 PM.


#7 wodinoneeye   Members   -  Reputation: 877

Like
0Likes
Like

Posted 16 March 2013 - 05:20 PM

I used to with alot of careful packing of data set up the  internal structures so that the data could be copied directly in and out of the packets (or write/read directly from the packet buffers as binary data for events).   (Tight control of how data was grouped)  Even had variant data  (optional sub structures of different data type content and even ordinal based offset pointers instead of true pointres -- was C/C++ so you could do things like that).

 

That usually flies in the face of object oriented programming in many ways , but if you MUST have the speed to make the game work the choice is simple.


--------------------------------------------Ratings are Opinion, not Fact

#8 Dave Weinstein   Members   -  Reputation: 532

Like
1Likes
Like

Posted 18 March 2013 - 10:57 AM

That usually flies in the face of object oriented programming in many ways , but if you MUST have the speed to make the game work the choice is simple.

 

Direct binary "globbing" makes sense for level-load, where seek time can kill you.

 

But the amount of time spent unpacking game messages is a rounding error on a rounding error of your frame rate; you are better off making the code readable and maintainable. Moreover, for network traffic, you are going to have to validate all the messages anyway, so you don't gain even that minuscule speedup

 

--Dave






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS