mirror of
https://github.com/cmz0228/hysteria-dev.git
synced 2025-06-09 13:59:54 +00:00
chore: a better fix for 32-bit unix.Timeval
Why there is no decltype() in Golang? At least we got generics now. ref: 9520d840942d3950ed43af281935c262fac35615
This commit is contained in:
parent
9520d84094
commit
e1d7ce4640
@ -7,6 +7,8 @@ import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"golang.org/x/exp/constraints"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
@ -58,7 +60,11 @@ func fdControlUnixSocketImpl(c *net.UDPConn, path string) error {
|
||||
}
|
||||
defer unix.Close(socketFd)
|
||||
|
||||
timeout := unixTimeval()
|
||||
var timeout unix.Timeval
|
||||
timeUsec := fdControlUnixTimeout.Microseconds()
|
||||
castAssignInteger(timeUsec/1e6, &timeout.Sec)
|
||||
// Specifying the type explicitly is not necessary here, but it makes GoLand happy.
|
||||
castAssignInteger[int64](timeUsec%1e6, &timeout.Usec)
|
||||
|
||||
_ = unix.SetsockoptTimeval(socketFd, unix.SOL_SOCKET, unix.SO_RCVTIMEO, &timeout)
|
||||
_ = unix.SetsockoptTimeval(socketFd, unix.SOL_SOCKET, unix.SO_SNDTIMEO, &timeout)
|
||||
@ -85,3 +91,7 @@ func fdControlUnixSocketImpl(c *net.UDPConn, path string) error {
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func castAssignInteger[F, T constraints.Integer](from F, to *T) {
|
||||
*to = T(from)
|
||||
}
|
||||
|
@ -1,15 +0,0 @@
|
||||
//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),
|
||||
}
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
//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,
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user