Design UDP Networking, trying to be smart

Simon Gust

Member
Hello GMC,
I worked on Networking for my game (Eyes of Regret) a while ago and now I came back to it
after a break. I had implemented a working example of UDP networking in a p2p manner,
presented to me by andev.

Since I've never done Networking before and have barely experience, I just jumped into it.
There isn't a problem (yet) but I don't have a background when it comes to judging my design.

To test and develop my Networking I've made it as direct as possible:
Networking is directly handled inside all playable characters. If I press a button, that character creates
their socket. Now As long as anyone knows their ip and port they can join them and will be put in their game
as a dummy client object. Now both host and client have each other in their game.

Since there isn't a real host though, anyone can join any client as long as they know their ip and port.
This way, the game should not end when the person pressing the host button leaves.

The data for the clients are in the dummy clients themselves to make it very simple to find the right client
or quickly send a packet to every client just by calling a with statement.

Now, the insecurities I am running into are: Who handles the flow of the game?
The game spawns enemies which represent the main difficulty.

Should I flag someone as host so that their game handles that and use a type of host-migration when they leave?
Or should I make it client-sided so that each client spawns their own enemies and set them free?

How do I even handle enemies in a non-server environment? I feel like this can cause a lot of traffic.

Any tips?
 

YanBG

Member
I made a simple p2p, where you can create or join a game(same build). So host and clients are decided at the start. Everybody sees the same game, enemies etc but there is only one "player" object, which is that you control and the others are just client/whatever objects that take the stats/position/data from the other player's games.

Didn't make it so the game continues without the host. You could have a save and restore, making one of the rest as host.
 

MishMash

Member
Generally speaking, it entirely depends on what the demands of your game are. If you value guaranteed full game integrity, then you NEED to have one of the players acting as a dedicated host for the duration of the game, however also bear in mind that this one player could manipulate the game and cheat (think of old COD modded lobbies), though if one player is host and enforcing a ruleset on the game, every other player must abide to the rules outlined by that virtual host.

Host migration itself can actually be done in a few ways. For example, a "full" host migration would involve the original host sending the full "server" state to the new host before leaving, however naturally this doesn't completely work under disconnects or crashes. So instead, you can also implement a soft host migration, where we assume that all the players have a roughly up-to-date snapshot of the game world, and thus can simply immediately take over hosting. (The downside here is that it requires you to send more information in general. Normally a server would not send every piece of information, only that information which mattered.)

For both of these systems, it is generally a good idea to have a matchmaking server which manages/tracks all of this, so that it can communicate changes to the players when things break down.

There is an extended option however, and this involves multi-hosts. You can engineer your networking to give different people control over different components of the game. This can help mitigate traffic demands on one player. This could for example mean that Player A is responsible for processing enemies, Player B responsible for processing the environment, etc; However, you do need to know that when working with P2P games like this, that its nigh impossible to make the game entirely secure, simply because you are giving so much freedom and control to the players themselves. Obviously, depending on the game, this may not matter.

Another interesting example is classical age of empires. I'm not 100% sure how this works, but from observation it seems that each player opens up a connection to each other player, and everyone propagates their own updates and verifies changes with each other. As, if any one player leaves, the other players can save the game state and still interact with each other. (This has been upgraded a bit in the AOE2 rework, as that mostly relies on dedicated hosts and Steam's packet relay system).
The interesting thing about AoE which makes it clever is that there seems to be some verification process so that responsibility for validating integrity of the game state is shared amongst all players, and all players must "agree" on any action made. The downside is that sometimes the game starts to stutter and have micro-freezes given that so much extra work has to be done to retain state. But it does mean that any one client who manipulates their game with the intent of cheating will not get by the other players. The way the world-state snapshot saving works is also rather interesting, as at any point in time, any player can save the game; this is really useful given that if one player disconnects (which was common back in the day), all other players would have a copy of the game state, and the other player could simply rejoin (whereas in dedicated host games, if the host dies, thats it, gg).

To add onto what I said before, by guaranteed integrity, I meant that the game was secure in all situations. This AoE P2P example can be secure within itself, however it still relies players complying, and there may be certain instances where its possible for a player to subtly lie about their own game state. You can also run into subtle issues such as if a game is lagging slightly for one player, their resources may tick at a very very slightly different rate, which causes an inconsistency, and then you have to write systems which deal with that.
 

YanBG

Member
@MishMash i see a fellow aoe2 player :D You are right about aoe2 and afaik even on steam it's PvP. You know how there were out of syncs(OOS)? That happens when one player's data is different than the host(usually from mods, graphics can be replaced without such problems). Steam just took the role of old MS Zone to show the lobbies and keep elo rating, added matchmaking etc but you lost the LAN option of the old game that used direct IP and port forwarding.

Edit: Issue with the AOE2 system is if one player has slow pc or internet, all others experience the same to keep sync(low FPS).
 
Last edited:

MishMash

Member
@MishMash i see a fellow aoe2 player :D You are right about aoe2 and afaik even on steam it's PvP. You know how there were out of syncs(OOS)? That happens when one player's data is different than the host(usually from mods, graphics can be replaced without such problems). Steam just took the role of old MS Zone to show the lobbies and keep elo rating, added matchmaking etc but you lost the LAN option of the old game that used direct IP and port forwarding.

Edit: Issue with the AOE2 system is if one player has slow pc or internet, all others experience the same to keep sync(low FPS).
Yeah, the slowness is defo a thing, and out of syncs are annoying. (There was also an irritating way to trigger an out of sync by just holding the window border for 10s, which people abused to cancel a lost match). Just did a bit of reading about the relay, and the game still functions in the same way, they just use steam relay servers in the event that someone has NAT disabled (or ports not forwarded), which does increase the accessibility and makes the game playable for everyone.
 

The-any-Key

Member
I would make one player host that hold the connections. So players need to connect to a host to play. The host then give control to clients to run sertain areas or a room to act like servers. If a player exit. The host give the control to another player in the area or pause it. But if the host exit the game exit.
 
Top