Okay, I've been fooling around with messages and pings a bit and after reading
this thread about TimeSynchronizationMessage I've tried to do the follwing:
We have a two player game where one player is the server at the same time. Another player connects and synchronizes his time:
client.connectAndWait(serverReliable, serverFast, 5000);
client.getServerConnection().getFastClient().synchronizeTimeAndWait(5000);
I have implemented a simle ping system where a timestamped ping (with System.nanoTime()) is sent to another peer, who basically sends it back. The ping im milliseconds is then determined by
(System.nanoTime() - pong.getTime()) / MILLI2NANO / 2;
\_____________________________/ \_________/ \_/
round trip time (rtt) in nanos to millis to ping
Now, what I do is (after adding "implements TimestampedMessage" to Pong):
public void messageReceived(Pong message) {
LOG.debug("PONG: " + (System.currentTimeMillis() - message.getTimestamp()));
}
That should - according to my understanding of the use of time synchronization - give me approximately the same result: the ping in milliseconds.
But the difference between my ping calculations and the TimestampedMessage-method ranges between 30 and 100 milliseconds. Unfortunately I am testing running server and client on one machine. My local ping is usually about 3 ms (which appears to be realistic). The TimestampedMessage-method results in local pings of 35 to about 100 ms.
Am I doing something generally wrong? Or any ideas what may be causing this effect?