|
|
|
|
//go:build windows
|
|
|
|
|
|
|
|
|
|
package netforward
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net"
|
|
|
|
|
"os"
|
|
|
|
|
"runtime"
|
|
|
|
|
"syscall"
|
|
|
|
|
"unsafe"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func SetTcpInfo(conn *net.TCPConn, usingKeepAlive bool, keepAliveIdel, keepAlivePeriod, keepAliveCount, userTimeout int) error {
|
|
|
|
|
//windows上,这两个值是毫秒,linux上则是秒
|
|
|
|
|
keepAlivePeriod *= 1000
|
|
|
|
|
keepAliveIdel *= 1000
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SetReUseAddr(fd uintptr) {
|
|
|
|
|
syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ControlSetReUseAddr(network, address string, c syscall.RawConn) (err error) {
|
|
|
|
|
if err := c.Control(func(fd uintptr) {
|
|
|
|
|
err = syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
|
|
|
|
|
}); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|