fix: timeval in different arch

Signed-off-by: HystericalDragon <HystericalDragons@proton.me>
This commit is contained in:
HystericalDragon 2024-04-05 09:11:37 +08:00
parent 13586df2ba
commit 9520d84094
No known key found for this signature in database
GPG key ID: E205724F8931D99B
3 changed files with 31 additions and 5 deletions

View file

@ -58,11 +58,7 @@ func fdControlUnixSocketImpl(c *net.UDPConn, path string) error {
}
defer unix.Close(socketFd)
timeoutUsec := fdControlUnixTimeout.Microseconds()
timeout := unix.Timeval{
Sec: timeoutUsec / 1e6,
Usec: timeoutUsec % 1e6,
}
timeout := unixTimeval()
_ = unix.SetsockoptTimeval(socketFd, unix.SOL_SOCKET, unix.SO_RCVTIMEO, &timeout)
_ = unix.SetsockoptTimeval(socketFd, unix.SOL_SOCKET, unix.SO_SNDTIMEO, &timeout)

View file

@ -0,0 +1,15 @@
//go:build linux && (386 || arm || mips || mipsle || ppc)
package sockopts
import (
"golang.org/x/sys/unix"
)
func unixTimeval() unix.Timeval {
timeUsec := fdControlUnixTimeout.Microseconds()
return unix.Timeval{
Sec: int32(timeUsec / 1e6),
Usec: int32(timeUsec % 1e6),
}
}

View file

@ -0,0 +1,15 @@
//go:build linux && (amd64 || arm64 || loong64 || mips64 || mips64le || ppc64 || ppc64le || riscv64 || s390x || sparc64)
package sockopts
import (
"golang.org/x/sys/unix"
)
func unixTimeval() unix.Timeval {
timeUsec := fdControlUnixTimeout.Microseconds()
return unix.Timeval{
Sec: timeUsec / 1e6,
Usec: timeUsec % 1e6,
}
}