feat: implement real ping RTT calculation instead of hardcoded 50ms

This commit is contained in:
Jose Luis Montañes Ojados
2026-01-16 19:43:31 +01:00
parent 184fff202f
commit 13f444193d
3 changed files with 79 additions and 5 deletions

View File

@@ -37,6 +37,12 @@ type Client struct {
PongPacketID uint16 // Type 0x05
AckPacketID uint16 // Type 0x06
// Ping RTT tracking
PingSentTimes map[uint16]time.Time // Map PingPacketID -> Time sent
PingRTT float64 // Rolling average RTT in ms
PingDeviation float64 // Rolling deviation in ms
PingSampleCount int // Number of samples for rolling avg
// State
Connected bool
ServerName string
@@ -70,6 +76,9 @@ func NewClient(nickname string) *Client {
VoiceDecoders: make(map[uint16]*opus.Decoder),
CommandQueue: make(map[uint16]*protocol.Packet),
ExpectedCommandPID: 0,
PingSentTimes: make(map[uint16]time.Time),
PingRTT: 0,
PingDeviation: 0,
done: make(chan struct{}),
}
}
@@ -187,6 +196,9 @@ func (c *Client) sendPing() error {
pkt.Data = encData
copy(pkt.Header.MAC[:], mac)
// Record send time for RTT calculation
c.PingSentTimes[pkt.Header.PacketID] = time.Now()
log.Printf("Sending proper Encrypted Ping (PID=%d)", pkt.Header.PacketID)
return c.Conn.SendPacket(pkt)
}