-
Notifications
You must be signed in to change notification settings - Fork 14
/
server.go
78 lines (57 loc) · 1.63 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Interface to listen on a TFO enabled TCP socket
package main
import (
"errors"
"fmt"
"log"
"syscall"
)
type TFOServer struct {
ServerAddr [4]byte
ServerPort int
fd int
}
const TCP_FASTOPEN int = 23
const LISTEN_BACKLOG int = 23
// Create a tcp socket, setting the TCP_FASTOPEN socket option.
func (s *TFOServer) Bind() (err error) {
s.fd, err = syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, 0)
if err != nil {
if err == syscall.ENOPROTOOPT {
err = errors.New("TCP Fast Open server support is unavailable (unsupported kernel).")
}
return
}
err = syscall.SetsockoptInt(s.fd, syscall.SOL_TCP, TCP_FASTOPEN, 1)
if err != nil {
err = errors.New(fmt.Sprintf("Failed to set necessary TCP_FASTOPEN socket option: %s", err))
return
}
sa := &syscall.SockaddrInet4{Addr: s.ServerAddr, Port: s.ServerPort}
err = syscall.Bind(s.fd, sa)
if err != nil {
err = errors.New(fmt.Sprintf("Failed to bind to Addr: %v, Port: %d, Reason: %s", s.ServerAddr, s.ServerPort, err))
return
}
log.Printf("Server: Bound to addr: %v, port: %d\n", s.ServerAddr, s.ServerPort)
err = syscall.Listen(s.fd, LISTEN_BACKLOG)
if err != nil {
err = errors.New(fmt.Sprintf("Failed to listen: %s", err))
return
}
return
}
// Block, waiting for connections, handling each connection in its own go
// routine
func (s *TFOServer) Accept() {
log.Println("Server: Waiting for connections")
defer syscall.Close(s.fd)
for {
fd, sockaddr, err := syscall.Accept(s.fd)
if err != nil {
log.Fatalln("Failed to accept(): ", err)
}
cxn := TFOServerConn{fd: fd, sockaddr: sockaddr.(*syscall.SockaddrInet4)}
go cxn.Handle()
}
}