浏览代码

enable tfo on other platforms

Darien Raymond 7 年之前
父节点
当前提交
418746fdb0
共有 3 个文件被更改,包括 94 次插入1 次删除
  1. 1 1
      transport/internet/sockopt_other.go
  2. 49 0
      transport/internet/sockopt_unix.go
  3. 44 0
      transport/internet/sockopt_windows.go

+ 1 - 1
transport/internet/sockopt_other.go

@@ -1,4 +1,4 @@
-// +build !linux
+// +build js
 
 package internet
 

+ 49 - 0
transport/internet/sockopt_unix.go

@@ -0,0 +1,49 @@
+// +build darwin dragonfly freebsd netbsd openbsd
+
+package internet
+
+import (
+	"strings"
+	"syscall"
+)
+
+const (
+	// For incoming connections.
+	TCP_FASTOPEN = 23
+	// For out-going connections.
+	TCP_FASTOPEN_CONNECT = 30
+)
+
+func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
+	if strings.HasPrefix(network, "tcp") {
+		switch config.Tfo {
+		case SocketConfig_Enable:
+			if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN_CONNECT, 1); err != nil {
+				return err
+			}
+		case SocketConfig_Disable:
+			if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN_CONNECT, 0); err != nil {
+				return err
+			}
+		}
+	}
+
+	return nil
+}
+
+func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
+	if strings.HasPrefix(network, "tcp") {
+		switch config.Tfo {
+		case SocketConfig_Enable:
+			if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, 1); err != nil {
+				return err
+			}
+		case SocketConfig_Disable:
+			if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, 0); err != nil {
+				return err
+			}
+		}
+	}
+
+	return nil
+}

+ 44 - 0
transport/internet/sockopt_windows.go

@@ -0,0 +1,44 @@
+package internet
+
+import (
+	"strings"
+	"syscall"
+)
+
+const (
+	TCP_FASTOPEN = 15
+)
+
+func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
+	if strings.HasPrefix(network, "tcp") {
+		switch config.Tfo {
+		case SocketConfig_Enable:
+			if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, 1); err != nil {
+				return err
+			}
+		case SocketConfig_Disable:
+			if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, 0); err != nil {
+				return err
+			}
+		}
+	}
+
+	return nil
+}
+
+func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
+	if strings.HasPrefix(network, "tcp") {
+		switch config.Tfo {
+		case SocketConfig_Enable:
+			if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, 1); err != nil {
+				return err
+			}
+		case SocketConfig_Disable:
+			if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, 0); err != nil {
+				return err
+			}
+		}
+	}
+
+	return nil
+}