You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
star/net/setcpinfo_darwin.go

36 lines
796 B
Go

//go:build darwin
package net
import (
"net"
"syscall"
)
func SetTcpInfo(conn *net.TCPConn, usingKeepAlive bool, keepAliveIdel, keepAlivePeriod, keepAliveCount, userTimeout int) error {
rawConn, err := conn.SyscallConn()
if err != nil {
return err
}
if usingKeepAlive {
err = rawConn.Control(func(fd uintptr) {
err = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPALIVE, keepAliveIdel)
if err != nil {
return
}
err = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, 0x101, keepAlivePeriod)
if err != nil {
return
}
})
} else {
err = conn.SetKeepAlive(false)
}
if userTimeout > 0 {
err = rawConn.Control(func(fd uintptr) {
err = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, 0x12, userTimeout)
})
}
return err
}