Skip to content

Commit

Permalink
Add platform-specific socket setting functions (#14)
Browse files Browse the repository at this point in the history
* Add platform-specific socket setting functions

The commit introduces two new files called `util_windows.go` and `util_unix.go` which contain specific socket options' settings for Windows and Unix. These functions replace the previous implementation in `util.go`, resulting in cleaner code and segregating functionalities based on the system platform.

* Replace SO_REUSEADDR with SO_REUSEPORT in Unix socket settings

The commit updates the Unix socket settings function from using the SO_REUSEADDR option to the SO_REUSEPORT option. This change is made in the `util_unix.go` file. SO_REUSEPORT is preferable as it permits multiple sockets to be bound to an identical socket address.
  • Loading branch information
ryanbekhen authored Feb 15, 2024
1 parent dab4855 commit 2f68937
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
10 changes: 1 addition & 9 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/pion/sdp/v3"
"github.com/pion/turn/v2"
"github.com/pion/webrtc/v3"
"golang.org/x/sys/unix"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
Expand Down Expand Up @@ -362,14 +361,7 @@ func StartTurnServer(ctx context.Context, publicIP string) *turn.Server {
// will load-balance received packets per the IP 5-tuple
listenerConfig := &net.ListenConfig{
Control: func(network, address string, conn syscall.RawConn) error {
var operr error
if err = conn.Control(func(fd uintptr) {
operr = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEPORT, 1)
}); err != nil {
return err
}

return operr
return setSocketOptions(network, address, conn)
},
}

Expand Down
19 changes: 19 additions & 0 deletions util_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//go:build unix

package sfu

import (
"golang.org/x/sys/unix"
"syscall"
)

func setSocketOptions(network, address string, conn syscall.RawConn) error {
var operr error
if err := conn.Control(func(fd uintptr) {
operr = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEPORT, 1)
}); err != nil {
return err
}

return operr
}
16 changes: 16 additions & 0 deletions util_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build windows

package sfu

import "syscall"

func setSocketOptions(network, address string, conn syscall.RawConn) error {
var operr error
if err := conn.Control(func(fd uintptr) {
operr = syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
}); err != nil {
return err
}

return operr
}

0 comments on commit 2f68937

Please sign in to comment.