NTPClient icon indicating copy to clipboard operation
NTPClient copied to clipboard

Compute and remove communication latency

Open jdoe1024 opened this issue 5 years ago • 2 comments
trafficstars

Current implementation only decodes the Transmit Timestamp and adopts it as the time when the NTP request was sent. It does not take into account the transmission latency between the device and the server, which can be important, especially when using Wi-Fi. The NTP protocol provides for that already, so why not do it?

There are 4 timestamps:

  • the Origin Timestamp (t0) which is taken when the client send the request packet
  • the Receive Timestamp (t1) which is taken when the server receives the request packet
  • the Transmit Timestamp (t2) which is taken when the server sends the response packet (t2 - t1 is the server processing time)
  • the Destination Timestamp (t4) which is taken when the client receives the response packet

The Origin Timestamp should be written in the request packet (bytes 20-28) before sending it and the Destination Timestamp should be measured when processing the response packet. t0 and t4 are taken using the same clock, which is different than the clock for t1 and t2. The transmission time is then:

T = (t4 - t0) - (t2 - t1)  ( = CLIENT_WAIT_TIME - SERVER_PROCESS_TIME)

What we want is the time it took between the sending of the response packet server and its reception by the client. The easiest approximation we can get is T / 2 (supposing it took the same time to send the request between client and server as to send the response between server and client).

jdoe1024 avatar Mar 16 '20 02:03 jdoe1024

Should the calculated latency be reduced in the existing returned time or do we need a separate more accurate function?

MisterAwesome23 avatar Mar 28 '20 22:03 MisterAwesome23

I add this feature in my Pull Request #102.

WhymustIhaveaname avatar Apr 25 '20 06:04 WhymustIhaveaname