-
Notifications
You must be signed in to change notification settings - Fork 6
/
epoll_linux.go
159 lines (130 loc) · 3.12 KB
/
epoll_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (c) 2023 Remember
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
//go:build linux || darwin || netbsd || freebsd || openbsd || dragonfly
package easyio
import (
"errors"
"fmt"
"runtime"
"sync/atomic"
"syscall"
)
type Poller struct {
e *Engine
index int
fd int
wfd int
shutdown atomic.Bool
}
func NewPoller(e *Engine) (*Poller, error) {
p := new(Poller)
p.e = e
fd, err := syscall.EpollCreate1(0)
if err != nil {
return nil, err
}
p.fd = fd
r1, _, err0 := syscall.Syscall(syscall.SYS_EVENTFD2, 0, syscall.O_NONBLOCK, 0)
if err0 != 0 {
syscall.Close(p.fd) //nolint:err
return nil, err0
}
p.wfd = int(r1)
if err = syscall.EpollCtl(fd, syscall.EPOLL_CTL_ADD, int(r1),
&syscall.EpollEvent{Fd: int32(r1),
Events: syscall.EPOLLIN,
},
); err != nil {
syscall.Close(p.fd) //nolint:err
syscall.Close(p.wfd) //nolint:err
return nil, err
}
return p, nil
}
func (p *Poller) Wait() error {
mesc := -1
events := make([]syscall.EpollEvent, 1024)
handler := p.e.GetEventHandler()
for !p.shutdown.Load() {
n, err := syscall.EpollWait(p.fd, events, mesc)
if err != nil && errors.Is(err, syscall.EINTR) {
return err
}
// no event
if n <= 0 {
mesc = -1
runtime.Gosched()
continue
}
mesc = 20
for i := 0; i < n; i++ {
event := events[i]
//find conn
c := p.e.GetConn(int(event.Fd))
if c == nil {
syscall.Close(int(event.Fd))
continue
}
//写
if event.Events&WriteEvents != 0 {
fmt.Printf("fd:%d write event\n", event.Fd)
c.Flush()
}
//event Error
if event.Events&ErrEvents != 0 {
p.closeConn(c)
continue
}
//read event
if event.Events&ReadEvents != 0 {
handler.OnRead(c.Context(), c)
}
}
}
return nil
}
func (p *Poller) closeConn(c Conn) {
if c == nil {
return
}
c.Close()
p.removeConn(c)
e := p.e.GetEventHandler()
if e != nil {
e.OnClose(c.Context(), c)
}
}
func (p *Poller) removeConn(conn Conn) {
if conn == nil {
return
}
fd := conn.Fd()
if c := p.e.GetConn(fd); c != nil {
p.e.Remove(fd)
_ = p.Delete(fd)
}
}
func (p *Poller) Close() error {
p.shutdown.Store(true)
syscall.Close(p.wfd) //nolint:errcheck
return syscall.Close(p.fd)
}
func (p *Poller) AddRead(fd int) error {
return syscall.EpollCtl(p.fd, syscall.EPOLL_CTL_ADD, fd, &syscall.EpollEvent{Fd: int32(fd),
Events: syscall.EPOLLERR | syscall.EPOLLHUP | syscall.EPOLLRDHUP | syscall.EPOLLPRI | syscall.EPOLLIN,
})
}
func (p *Poller) ModWrite(fd int) error {
return syscall.EpollCtl(p.fd, syscall.EPOLL_CTL_MOD, fd, &syscall.EpollEvent{Fd: int32(fd),
Events: syscall.EPOLLERR | syscall.EPOLLHUP | syscall.EPOLLRDHUP | syscall.EPOLLPRI | syscall.EPOLLIN | syscall.EPOLLOUT,
})
}
func (p *Poller) ModRead(fd int) error {
return syscall.EpollCtl(p.fd, syscall.EPOLL_CTL_MOD, fd, &syscall.EpollEvent{Fd: int32(fd),
Events: syscall.EPOLLERR | syscall.EPOLLHUP | syscall.EPOLLRDHUP | syscall.EPOLLPRI | syscall.EPOLLIN,
})
}
func (p *Poller) Delete(fd int) error {
return syscall.EpollCtl(p.fd, syscall.EPOLL_CTL_DEL, fd, &syscall.EpollEvent{Fd: int32(fd)})
}