Suppose you have two computers on different networks with the following assumptions
Each computer has a system time which may be different from the other, but this offset is constant. Let be the client and server time, then there exists some such that
Each computer clock advances forward at the same rate
The travel time from the client to server denoted by and respectively, and there exists some such that
In the clock delta problem, you do not know the value of nor , and you must design a program which estimates these values. The program may send packets over the network with any required information.
The reason for is that the travel time from client to server is always the same and the travel time from the server to client is always the same, but the two times my differ (different packet routing).
Time Conversion Function
Suppose that are times measured on the client and server clock respectively, then to convert the time to the servers time one has to compute and to convert a server time to client time one has to compute
This follows directly from the definition of the clock delta problem as
The above corollary is important because we cannot measure the elapsed time unless two times are measured on the same clock.
Elapsed Time
Suppose that are two times measured on the same clock , then the amount of time between the two is given by
Note that if this equation is simply
Isolating for Travel Time and Clock Delta From Packets
Suppose that a packet is sent from the client to the server it is sent at client time and received at server time , after a slight delay it sent back from the server at time and received on the client at client time then and
Since the travel time from client to server and server to client is constant, then we know that is the amount of elapsed time from the moment the packet was sent and received, as measured on the clients clock the initial send time is and the received time was as measured on the servers clock, therefore the time that was sent on the servers clock is given by therefore the elapsed time during travel is given by Note that the absolute values are omitted because when measured on the the same clock, the time that a packet is sent comes before when a packet is received.
Similarly we can deduce that Since we know that then we can deduce that therefore and
The above proposition is good, but we can't isolate exactly or we need one to compute the other. This is when approximations start to come in, we will build up an approximation of the travel and clock delta. For each of the symbols we've mentioned let the same symbol above it with a bar represent the approximation of it, for example is the approximation of , additionally this process will be iterative, and so we'll denote the approximation after the -th update as
In order to make this process tractable, we'll start by assuming that which is not a horrible assumption to make, which allows us to compute that
But the above equation also has some problems too, recall that is just a sampling of a single full pass, and since we are now on the live network there is going to be variance between any two different passes. In order to account for this, we'll average out the values in question, to kickstart this process we'll need better variables, so let denote the -th client send, server receive, server send, and client receive times all on their own clocks respectively.
With that in place, we set up the following system, the client starts by sending a packet with its current time to the server, this is the "send time" denoted by then the server receives the packet and gets the time which is denoted by , then the sever waits for some fixed time duration which we wil denoted by and measured in seconds (to slow down the frequency of the ping/pong) and gets a new time, which is denoted by and sends to the client.
When received the client stores the value somewhere and then puts the value into a ring buffer which we will denote by which represents the a collection of time differences between the client send and server receive, note that the values stored are not actually the travel time from the client to the server because the two times are not measured on the same clock.
will have some fixed size which we will assume to be , additionally there will is another ring buffer of the same size in which will store . To simplify things we make the behavior of the server identical to that of the client, the benefit being that it spreads out the work-load between two computers meaning twice the number of iterations can be done in the same time.
For clarity after the value is added to the ring buffer the client then waits for seconds and then measures and sends to the server, then the server adds and the process restarts. The ping and the pong continues until the ring buffers are full, which will first occur on the client because the server only starts adding to the ring buffer after its first receive, which occurs after the clients first receive.
Once the ring buffers are full, we take the average of the ring buffers, on the client when we take the average of it is given by this equation: and for some , the purpose of is to just denote that the index may not start at because after more than client receives the ring buffer removes some of the beginning elements.
Now looking back to our initial value for we will use these averaged values instead to say that which will help with the variance between individual network send and receive events.
Now that we've computed an initial value for we can use this to come up with an expected arrival time for the next packet we send to the server. Right before we send the packet, we get our current time
Now that we know we can compute The order of computation will be and so on each iteration we will first compute and then use that to compute .
Now that the process has been bootstrapped we will continue updating our ring buffers so that are still a smoothed version of the current travel times, and at each iteration we first compute