Keeps time in sync between server and client.
The project keeps time in sync between server and client. Both sides know how many ticks were performed at a certain time.
The algorithm can survive through ping spikes and bad network conditions. For simplicity, the server supports only one client.
Overview |
---|
Client sends request to server 1/TimeUpdateDelay times per second. Request contains client local time.
When he receives a response - he finds delta between local time and first float in the packet. That delta is added to the second float in the packet, the resulting time is increased on each update. From that approximation, the client calculates the number of ticks that happened on the server. The client modifies his tick frequency to smoothly reach approximated tick value.
In that implementation, only the server will send his local time to clients with specified frequency. The client can utilize ENet.Peer's RTT value to understand how far behind he is generally.
When client receives the first packet with server time, he will add RTT/2 to it and store local time and offset(RTT/2). When client receives the next packets, he will calculate the delta between current local time and last stored local time, then the total time will be equal to receivedTime + offset + delta. Then local time is stored again.
In that approach, the most important part is getting the correct RTT value at the first packet.
The server increases his local tick count. When he receives a request from a client - he will send back time from request and his local time.