• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
sandorlev

Sending structures through UDP sockets

6 posts in this topic

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?

0

Share this post


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

Share this post


Link to post
Share on other sites
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 [tt]sizeof(event)[/tt] 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.
0

Share this post


Link to post
Share on other sites

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 [tt]sizeof(event)[/tt] 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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0