Advertisement

What exactly do I send in packages?

Started by August 14, 2015 09:36 PM
6 comments, last by Argha 9 years, 3 months ago

So question is basically in the topic title.

In a pong game I would send e.g the following:

  • Player1/2 x-/y-Pos
  • Ball Pos

Luckily SFML already knows that these values in the packages are float numbers e.g. .

Question is: If it would be a byte stream only, how shall the receiver know that it is a float number? Putting some kind of identifier in it as a prefix?

And let's say there are 6 Players and 3 NPCs, every player is able to collect items. So I have to send the positions every single tick and in case someone collects an item than this information too.

But how do I encode it into a datastream? Like player2 collected weapon1. What Data would you send to your server? Some kind of code?

Like:

std::string message = "Something to tell that player2 collected weapon1";

sf::Packet packet;
packet << message;
socket.send(packet, recipient, port);

See the the links in the Forum FAQ item #12.

Advertisement
Hi. It all depends on the protocol you want to use.
Like http you build a sting with command tokens.

Or you can roll your own protocol
Using something like google protocol buffers.
That will create data classes that can be serialized to a device independent format that can be transmitted over the wire.
With the proto buffer you can put in a message type identifier uneque for each message then when you receive a whole message you check the type and parse the data as that type.

Like
Class protoMsglocation
{
Public:
Int32_t Type;// the message type for this message only
// other data members go her
}

Then in some header you define all your message types

Indeed a http post-like protocol could fit my needs :)

Thanks

For a real-time game, HTTP POST is about as bad a match as you can find.
The overhead per request is staggering, and the latency is really high.
enum Bool { True, False, FileNotFound };

For a real-time game, HTTP POST is about as bad a match as you can find.
The overhead per request is staggering, and the latency is really high.

I just meant that I would/could encode the data via UDP package that way, like sending a string "playerId=01&posX=-333&posY=47"

Advertisement

I would/could encode the data via UDP package that way, like sending a string "playerId=01&posX=-333&posY=47"


Or you could do what most games do, and define a binary protocol. (See the FAQ)

Specifically, if there can be 255 players connected to an instance, and positions have a range smaller than -32767..32767 for X and Y, you can pack what you need into five bytes:

playerid: 1 byte
xpos: 2 bytes
ypos: 2 bytes
Each kind of message you may want to send is typically known as a "PDU" (protocol data unit or packet data unit) and you typically put a PDU identifier in front of the data so the receiver can know what they're receiving. You'd also use some kind of framing that takes care of things like loss detection, latency measurement, authentication, etc.
enum Bool { True, False, FileNotFound };

Yes, I read about that too. But I also think that my little game won't be that critical performance wise :)

Anyways, thanks, then I might go that way

This topic is closed to new replies.

Advertisement