In the context of lag compensation, one needs to know when the command is instantiated on the client (this can be named as "command execution time" as well). AFAIK, there can be 2 methods for this:

  1. Client sends a timestamp with the command.
  2. Client doesn't send any timestamps, but server does a smart thing to calculate the command's instantiation time.

About #1:

  • Is this safe in terms of cheating?

About #2:

According to Valve's paper, command execution time is as following:

Command Execution Time = Current Server Time - Packet Latency - Client View Interpolation

This means, server must know the packet latency.

Another paper from Valve confirms this and says:

Before executing a player's current user command, the server:

  1. Computes a fairly accurate latency for the player
  2. ...
  • How can the server compute "fairly accurate latency for the player"?

Most naive and easiest approach would be sending pings regularly (less frequent than game commands and updates though) to find out an average of the latency and use it. Busy network traffic, latency fluctuation and naiveness of this method makes me feel there must be a more elegant way.

EDIT: Would UDP vs TCP change anything in this context?


1 Answer 1


Your intuition about #1 is correct: don't trust client timestamps. Even without cheating, timestamps can be wildly wrong.

Best approach is to ping, and divide the ping time by two. The lag may be asymmetric, but there's no way to verify that from the server. If you already have a persistent connection (TCP), that channel is fine. For just a ping, a no-setup UDP is probably better.

A client can "cheat" making a ping time appear longer than the actual latency, if server=initiated. (If client-initiated, it could lie about the result to the server.) So be careful about revealing "the future" to a client, while trying to compensate, if that can affect the game.

Yes, latency will vary by "network weather", and you can smooth over time and perhaps discard outliers.

  • \$\begingroup\$ (And, unsolicited development guidance... Unless this is for real money, better to just get it working despite any security vulnerabilities, than get too caught up in hacked-client-scenarios. Unless that's the part that's most interesting to you. :-) ) \$\endgroup\$ Commented Aug 5, 2015 at 23:45
  • 1
    \$\begingroup\$ Well, ok, thanks... I think I will just drop the latency measurement idea for now and use command timestamps as the game is not next Counter Strike. I can do the latency measurement in version 10 :) \$\endgroup\$
    – Ali Ok
    Commented Aug 6, 2015 at 8:26
  • 2
    \$\begingroup\$ 2 years later, I am now actually implementing this thing. I forgot asking this question and was googling. So funny to find your own question \$\endgroup\$
    – Ali Ok
    Commented Jul 18, 2017 at 21:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .