I've been building a UDP server and have hit a bit of a stumbling point. Since UDP is not a persisted connection, there really isn't a way for me to track if the Player has left the current session/closed the game etc.

What I was planning on doing was having the server/client perform some kind of handshake when the client first attempts to join a game session. The server would store the server-side Player object and it's associated EndPoint(s) in a collection. This way, when Player A does something that affects Player B, I can update the two player objects on the server-side and send both players updated info.

What happens if Player A closes the game? I need to clean up my collection of Player/Endpoint(s). I'm not sure how that is typically handled. Should I have a timer that runs every minute or two, pinging clients and removing them if no ping comes back? Since It's UDP, I can't assume that "no new datagrams" really means the player isn't playing. It's possible that they've just not needed to send any data for a while (maybe it's paused or something idk). I have a low-overhead async timer API that runs on background threads that I could use, but it feels like a lot of overhead to ping every connected client every couple of minutes.


It's fairly simple. Timeout

Make each client ping the server every few seconds. If the server haven't received any packets from the client for say 15 seconds, consider it at timeout.

But by default you should not rely on timeout to remove clients. When the client leaves or closes the game, it should send a leave message to the server. Timeout should only happen if the client crashes or loses his internet connection.

Additionally you might also want to add timeout on the client in case the server should crash.

| improve this answer | |
  • \$\begingroup\$ ok, thanks. I wasn't sure where the responsibility laid. I have a special datagram that signifies disconnecting (for preserving the auth token) and another for logging out (for discarding the token). Wasn't sure who was responsible for handling timeouts and what mechanic it should be built with. Having the client do it makes sense. Then I don't have to wait for a pong server side if the server sent the ping. \$\endgroup\$ – Johnathon Sullinger Nov 20 '16 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.