Fix windows build

This commit is contained in:
9seconds
2021-10-04 09:46:49 +03:00
parent 686f177ab9
commit d1e5f9d145
3 changed files with 44 additions and 15 deletions
+3 -15
View File
@@ -3,8 +3,6 @@ package network
import ( import (
"fmt" "fmt"
"net" "net"
"golang.org/x/sys/unix"
) )
// SetClientSocketOptions tunes a TCP socket that represents a connection to // SetClientSocketOptions tunes a TCP socket that represents a connection to
@@ -53,19 +51,9 @@ func setCommonSocketOptions(conn *net.TCPConn, bufferSize int) error {
return fmt.Errorf("cannot get underlying raw connection: %w", err) return fmt.Errorf("cannot get underlying raw connection: %w", err)
} }
rawConn.Control(func(fd uintptr) { // nolint: errcheck if err := setSocketReuseAddrPort(rawConn, bufferSize); err != nil {
err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1) return fmt.Errorf("cannot setup SO_REUSEADDR/PORT: %w", err)
if err != nil { }
err = fmt.Errorf("cannot set SO_REUSEADDR: %w", err)
return
}
err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
if err != nil {
err = fmt.Errorf("cannot set SO_REUSEPORT: %w", err)
}
})
return nil return nil
} }
+31
View File
@@ -0,0 +1,31 @@
//go:build !windows
// +build !windows
package network
import (
"fmt"
"syscall"
"golang.org/x/sys/unix"
)
func setSocketReuseAddrPort(conn syscall.RawConn, bufferSize int) error {
var err error
conn.Control(func(fd uintptr) { // nolint: errcheck
err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1)
if err != nil {
err = fmt.Errorf("cannot set SO_REUSEADDR: %w", err)
return
}
err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
if err != nil {
err = fmt.Errorf("cannot set SO_REUSEPORT: %w", err)
}
})
return err
}
+10
View File
@@ -0,0 +1,10 @@
//go:build windows
// +build windows
package network
import "syscall"
func setSocketReuseAddrPort(conn syscall.RawConn, bufferSize int) error {
return nil
}