Packet combining

Started by
6 comments, last by Telanor 9 years, 10 months ago
How do games usually handle having a bunch of different packets to send out in a single tick?

Suppose you have:
- 5 entities that have moved
- 3 entities that have started playing a new animation
- 6 new particle systems created (or rather 6 events that will create particle systems and sounds, etc)
- 2 new effects (as in the poison, heal, etc) applied to an entity (which is more than just a particle system, its also an icon to display on the UI)

Would any of those packets be combined into one to reduce overhead? If so, how? Do you combine all of the same type? Combine everything? Combine as many as you can until a certain size limit? What would the size limit be?

Suppose only some of those packets need to be sent to player A and some to player B (with some being sent to both). How does that affect the combining?

By the way, I'm using the lidgren networking library, in case that's important.
Advertisement

They would most certainly be combined into a single update packet. The way you do that is by putting all the entities, and their properties, in a list inside the packet. This includes any other properties an entity may need to convey an update about. You would have a byte telling the recipient how many entities are listed, and at the beginning of each entity have a byte that describes what entity properties are described. Then you can easily sift through each incoming packet to pull out the data that needs to be applied to entities locally.

Well that's exactly what I'm doing currently, but only that. I'd like to know if there's more I should be doing. The entity packets are combined, but the event packets and effect packets aren't. I also haven't dealt with the "some packets to player A, some to player B" thing yet. So I'd like to know how to deal with those two things.

You would include other things just the same way, so the first part of your packet is something like (for example) 3 bytes, one byte for how many entities are in the packet, one for how many events are in the packet, and one for effects.

Then the first byte of each entity (after an entity ID) is which properties of that entity follow.

After all the entities, and their info are in the packet, you move on to listing the events, then effects and their properties.

There are many different ways of handling your packets. I tend to define a header as the size of the packet contents, then its protocol. That is it. This helps avoid errors within a single packet from causing off-by-n errors elsewhere. So an entity update packet might involve sending the number of repeated entities, then packing their data, and appending the header to that packet.

So I should combine them all together then? How about the "some packets to player A, some to player B" issue? How is that dealt with?

How about the "some packets to player A, some to player B" issue? How is that dealt with?


1. Set a network tick rate, such as 10 Hz or 20 Hz or 60 Hz
2. Each time a network tick comes up, iterate over all connected players
3. For each connected player, glom together all the updates that that particular player needs to see, and send as one packet to that player
enum Bool { True, False, FileNotFound };
So I have to go through the whole process again for each player? Guess I'll have to rework things a little bit then. Thanks for the help guys

This topic is closed to new replies.

Advertisement