-
Notifications
You must be signed in to change notification settings - Fork 52
/
netpoll_linux.go
139 lines (121 loc) · 2.72 KB
/
netpoll_linux.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package gn
import (
"fmt"
"golang.org/x/sys/unix"
"syscall"
)
// 对端关闭连接 8193
const (
EpollRead = syscall.EPOLLIN | syscall.EPOLLPRI | syscall.EPOLLERR | syscall.EPOLLHUP | unix.EPOLLET | syscall.EPOLLRDHUP
EpollClose = uint32(syscall.EPOLLIN | syscall.EPOLLRDHUP)
)
type epoll struct {
listenFD int
epollFD int
}
func newNetpoll(address string) (netpoll, error) {
listenFD, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, 0)
if err != nil {
log.Error(err)
return nil, err
}
err = syscall.SetsockoptInt(listenFD, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
if err != nil {
log.Error(err)
return nil, err
}
addr, port, err := getIPPort(address)
if err != nil {
return nil, err
}
err = syscall.Bind(listenFD, &syscall.SockaddrInet4{
Port: port,
Addr: addr,
})
if err != nil {
log.Error(err)
return nil, err
}
err = syscall.Listen(listenFD, 1024)
if err != nil {
log.Error(err)
return nil, err
}
epollFD, err := syscall.EpollCreate1(0)
if err != nil {
log.Error(err)
return nil, err
}
return &epoll{listenFD: listenFD, epollFD: epollFD}, nil
}
func (n *epoll) accept() (nfd int, addr string, err error) {
nfd, sa, err := syscall.Accept(n.listenFD)
if err != nil {
return
}
// 设置为非阻塞状态
err = syscall.SetNonblock(nfd, true)
if err != nil {
return
}
err = syscall.EpollCtl(n.epollFD, syscall.EPOLL_CTL_ADD, nfd, &syscall.EpollEvent{
Events: EpollRead,
Fd: int32(nfd),
})
if err != nil {
return
}
s := sa.(*syscall.SockaddrInet4)
addr = fmt.Sprintf("%d.%d.%d.%d:%d", s.Addr[0], s.Addr[1], s.Addr[2], s.Addr[3], s.Port)
return
}
func (n *epoll) addRead(fd int) error {
err := syscall.EpollCtl(n.epollFD, syscall.EPOLL_CTL_ADD, fd, &syscall.EpollEvent{
Events: EpollRead,
Fd: int32(fd),
})
if err != nil {
return err
}
return nil
}
func (n *epoll) closeFD(fd int) error {
// 移除文件描述符的监听
err := syscall.EpollCtl(n.epollFD, syscall.EPOLL_CTL_DEL, fd, nil)
if err != nil {
return err
}
// 关闭文件描述符
err = syscall.Close(fd)
if err != nil {
return err
}
return nil
}
func (n *epoll) getEvents() ([]event, error) {
epollEvents := make([]syscall.EpollEvent, 100)
num, err := syscall.EpollWait(n.epollFD, epollEvents, -1)
if err != nil {
return nil, err
}
events := make([]event, 0, len(epollEvents))
for i := 0; i < num; i++ {
event := event{
FD: epollEvents[i].Fd,
}
if epollEvents[i].Events == EpollClose {
event.Type = EventClose
} else {
event.Type = EventIn
}
events = append(events, event)
}
return events, nil
}
func (n *epoll) closeFDRead(fd int) error {
_, _, e := syscall.Syscall(syscall.SHUT_RD, uintptr(fd), 0, 0)
if e != 0 {
return e
}
return nil
}