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_windows.go

34 lines
787 B
Go

//go:build windows
package net
import (
"net"
"os"
"runtime"
"syscall"
"unsafe"
)
func SetTcpInfo(conn *net.TCPConn, usingKeepAlive bool, keepAliveIdel, keepAlivePeriod, keepAliveCount, userTimeout int) error {
if usingKeepAlive {
rawConn, err := conn.SyscallConn()
if err != nil {
return err
}
err = rawConn.Control(func(fd uintptr) {
ka := syscall.TCPKeepalive{
OnOff: 1,
Time: uint32(keepAliveIdel),
Interval: uint32(keepAlivePeriod),
}
ret := uint32(0)
size := uint32(unsafe.Sizeof(ka))
err = syscall.WSAIoctl(syscall.Handle(fd), syscall.SIO_KEEPALIVE_VALS, (*byte)(unsafe.Pointer(&ka)), size, nil, 0, &ret, nil, 0)
runtime.KeepAlive(fd)
})
return os.NewSyscallError("wsaioctl", err)
}
return conn.SetKeepAlive(false)
}