-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclocksync.go
More file actions
29 lines (25 loc) · 862 Bytes
/
clocksync.go
File metadata and controls
29 lines (25 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package fastctrl
import "encoding/binary"
const (
MsgPing byte = 0xFE
MsgPong byte = 0xFF
)
// BuildPong creates a pong datagram with the engine's PTP timestamp and
// the echoed ping sequence number for RTT correlation.
// Format: [0xFF][4 bytes: seq BE][8 bytes: PTP µs big-endian] = 13 bytes
func BuildPong(ptpMicroseconds int64, seq uint32) []byte {
buf := make([]byte, 13)
buf[0] = MsgPong
binary.BigEndian.PutUint32(buf[1:5], seq)
binary.BigEndian.PutUint64(buf[5:13], uint64(ptpMicroseconds))
return buf
}
// ParsePing extracts an optional sequence number from a ping datagram.
// The type byte (0xFE) is already stripped by the dispatcher.
// Format: [] (empty) or [4 bytes: sequence number big-endian]
func ParsePing(data []byte) (seq uint32, err error) {
if len(data) >= 4 {
seq = binary.BigEndian.Uint32(data[:4])
}
return seq, nil
}