🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Sending different types of information via network

Started by
6 comments, last by Loomina 3 years, 8 months ago

Hi!

I'm very new to networking and am still trying to understand basic stuff but my question is how do games handle sending all types of data? For example sending movement data or animation data or other stuff. How do they package their data so its easy for for the server to decide what kind of data it is?

The only thing I can come up with is making an enum and having the different types of data In there and just prefixing the network packets with that. Meaning packets look like this = Enum.Datatype.Input + Input.W.

So I'm just trying to find out how I don't have to write a bunch of if statements on my server for all the data that's coming in.

Hope the question makes sense and thank you for any help!

Advertisement

That's the gist of it, yes. If you're working with binary data, you may have a dedicated structure that is transmitted, and the structure always has the “type” as the 1st entry.

If we're talking C/C++, it could even use a union after it to define the possible data types, so something like this:

enum TDataTYpe {
  TYPE_CHAT = 0,
  TYPE_POSITION,
  TYPE_ACTION,
  TYPE_MAX
};

struct TDataPacket {
  TDataType DataType;
  uint32_t FromPlayerId;
  union {
    char ChatString[MAX_CHAT_STRING];
    TPositionData PosData;
    TActionData ActionData;
  };
};

And then you're parse the data type to determine which data element to access.

Of course, there are tons of other ways to do this, but that's just a simple example I've used in the past.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

So I'm just trying to find out how I don't have to write a bunch of if statements on my server for all the data that's coming in.

After your game code has parsed the inbound network packets (similar to what BeerNutts has described up here, this would eventually define your game network protocol), you can use:

if-statements and/or events to process these packets .

They have their pros and cons, but whichever one you use it's going to be the least of your worry.

Depending on what network lib you use: Things to watch out for are initial secured TCP connections, UDP network packet count and/or losses, lag/latency, lobbies, client/server states (full or whatnot…), the network packet window size, the endianness of data, and the list goes on…

So don't worry if your code classifies your packet with loads of ifs just to know what to do with it. I use both ifs and events.

http://www.parsec.org/netdocs/index.html#gamecodelayer

https://www.gamedev.net/forums/forum/8-networking-and-multiplayer/

There's a lot to read about it, here are a couple of links, but it's definitely fun to code

That's it… all the best ?

@undefined Thank you for your response. I’ll be looking into those links. You’re helping me out a lot thank you!

@undefined Thank you for your help! This is gonna help me out a lot

Another way to think about it (which ends up with the same kind of solution): A network packet is like a small file, that you write into memory, and send over the wire. Just like you have to somehow serialize/package your data to write it to a file, you have to do it the same way in a packet.

The requirements are slightly different – you don't usually write 30 files a second, and files very seldom go missing from the hard disk, but when it comes to data serialization, it's more or less the same kind of code and solutions.

enum Bool { True, False, FileNotFound };

@hplus0603 Okay I'll keep that in mind and will try to come up with something Thank you!

This topic is closed to new replies.

Advertisement