fix RTT for specific platforms

This commit is contained in:
2025-03-11 23:06:26 -05:00
parent 534982b99d
commit 041b439a66
3 changed files with 54 additions and 30 deletions

View File

@@ -6,9 +6,7 @@ import (
"net"
"runtime"
"sync"
"syscall"
"time"
"unsafe"
"github.com/Sudo-Ivan/reticulum-go/pkg/common"
)
@@ -408,28 +406,6 @@ func (tc *TCPClientInterface) IsConnected() bool {
return tc.conn != nil && tc.Online && !tc.reconnecting
}
func getRTTFromSocket(fd uintptr) time.Duration {
var info syscall.TCPInfo
size := uint32(syscall.SizeofTCPInfo)
_, _, err := syscall.Syscall6(
syscall.SYS_GETSOCKOPT,
fd,
syscall.SOL_TCP,
syscall.TCP_INFO,
uintptr(unsafe.Pointer(&info)),
uintptr(unsafe.Pointer(&size)),
0,
)
if err != 0 {
return 0
}
// RTT is in microseconds, convert to Duration
return time.Duration(info.Rtt) * time.Microsecond
}
func (tc *TCPClientInterface) GetRTT() time.Duration {
tc.mutex.RLock()
defer tc.mutex.RUnlock()
@@ -439,13 +415,15 @@ func (tc *TCPClientInterface) GetRTT() time.Duration {
}
if tcpConn, ok := tc.conn.(*net.TCPConn); ok {
var rtt time.Duration
if info, err := tcpConn.SyscallConn(); err == nil {
info.Control(func(fd uintptr) {
rtt = getRTTFromSocket(fd)
})
return rtt
var rtt time.Duration = 0
if runtime.GOOS == "linux" {
if info, err := tcpConn.SyscallConn(); err == nil {
info.Control(func(fd uintptr) {
rtt = platformGetRTT(fd)
})
}
}
return rtt
}
return 0

View File

@@ -0,0 +1,14 @@
//go:build !linux
// +build !linux
package interfaces
import (
"time"
)
// platformGetRTT is defined in OS-specific files
// Default implementation for non-Linux platforms
func platformGetRTT(fd uintptr) time.Duration {
return 0
}

View File

@@ -0,0 +1,32 @@
//go:build linux
// +build linux
package interfaces
import (
"syscall"
"time"
"unsafe"
)
func platformGetRTT(fd uintptr) time.Duration {
var info syscall.TCPInfo
size := uint32(syscall.SizeofTCPInfo)
_, _, err := syscall.Syscall6(
syscall.SYS_GETSOCKOPT,
fd,
syscall.SOL_TCP,
syscall.TCP_INFO,
uintptr(unsafe.Pointer(&info)),
uintptr(unsafe.Pointer(&size)),
0,
)
if err != 0 {
return 0
}
// RTT is in microseconds, convert to Duration
return time.Duration(info.Rtt) * time.Microsecond
}