-
Notifications
You must be signed in to change notification settings - Fork 1
/
io.poll.go
189 lines (156 loc) · 3.95 KB
/
io.poll.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// This code has a lot of references to 'github.com/tidwall/evio' - cppis
// +build linux
package elio
import (
"fmt"
"syscall"
"unsafe"
"golang.org/x/sys/unix"
)
// PollEvent poll event
type PollEvent struct {
unix.EpollEvent
Session *Session
}
// Poll event poll object
type Poll struct {
fd int // epoll fd
wfd int // wake fd
wfdBuf []byte // wfd buffer to read packet
jobs SafeSlice
}
// String object to string
func (p *Poll) String() string {
return fmt.Sprintf("Poll::%p", p)
}
// NewPoll create poll
func NewPoll() (p *Poll) {
p = new(Poll)
return p
}
// Begin begin poll
func (p *Poll) Begin() (err error) {
defer func() {
if nil != err {
if 0 != p.fd {
unix.Close(p.fd)
}
if 0 != p.wfd {
unix.Close(p.wfd)
}
}
}()
if p.fd, err = unix.EpollCreate1(0); nil != err {
AppError().Err(err).Msg("failed to create epoll")
return err
}
r0, _, e0 := unix.Syscall(unix.SYS_EVENTFD2, 0, 0, 0)
if 0 != e0 {
err = fmt.Errorf("failed to init fd:%v", r0)
} else {
p.wfd = int(r0)
p.wfdBuf = make([]byte, 8)
p.ControlAdd(p.wfd, unix.EPOLLIN|unix.EPOLLERR)
}
AppInfo().Str(LogObject, p.String()).Msg("begin epoll")
return err
}
// End ...
func (p *Poll) End() {
AppInfo().Str(LogObject, p.String()).Msgf("end epoll")
var err error
if 0 != p.wfd {
if err = unix.Close(p.wfd); nil != err {
AppError().Str(LogObject, p.String()).Err(err).
Msgf("failed to close epoll")
}
p.wfd = 0
}
if 0 != p.fd {
AppInfo().Str(LogObject, p.String()).Msgf("poll:%s closing", p.String())
if err = unix.Close(p.fd); nil != err {
AppError().Str(LogObject, p.String()).Err(err).
Msgf("failed to close epoll")
}
p.fd = 0
}
}
// WaitResult wait result
type WaitResult struct {
evt unix.EpollEvent
err error
}
// // WaitAndRun ...
// func (p *Poll) WaitAndRun(count int, iter func(e unix.EpollEvent) error) (errored []WaitResult, err error) {
// events := make([]unix.EpollEvent, count)
// var results []WaitResult
// for {
// var n int
// n, err = unix.EpollWait(p.fd, events, -1)
// if nil != err && unix.EINTR != err {
// AppError().Str(LogObject, p.String()).Err(err).
// Msg("failed to poll.wait.run")
// return results, err
// }
// AppDebug().Str(LogObject, p.String()).
// Msgf("poll.wait.run awake with %v events", n)
// for i := 0; i < n; i++ {
// if e := iter(events[i]); nil != e {
// // AppError().Str(LogObject, p.String()).
// // Msgf("failed to poll.wait.run.iter with error:'%v'", e)
// results = append(results, WaitResult{evt: events[i], err: e})
// }
// }
// }
// }
// Wait ...
func (p *Poll) Wait(events []unix.EpollEvent, timeout int) (waits int, err error) {
waits, err = unix.EpollWait(p.fd, events, timeout)
if nil != err && unix.EINTR != err {
//AppError().Str(LogObject, p.String()).Err(err).
// Msg("failed to poll.wait")
return waits, err
}
//AppDebug().Str(LogObject, p.String()).
// Msgf("poll.wait awake with %v events", waits)
return waits, nil
}
// Make the endianness of bytes compatible with more linux OSs under different processor-architectures,
// according to http://man7.org/linux/man-pages/man2/eventfd.2.html.
var (
x uint64 = 1
b = (*(*[8]byte)(unsafe.Pointer(&x)))[:]
)
// Trigger ...
func (p *Poll) Trigger(job interface{}) error {
p.jobs.Append(job)
_, err := syscall.Write(p.wfd, b)
return err
}
// ControlAdd control add
func (p *Poll) ControlAdd(fd int, events uint32) error {
return unix.EpollCtl(p.fd, unix.EPOLL_CTL_ADD, fd,
&unix.EpollEvent{
Fd: int32(fd),
Events: events,
},
)
}
// ControlMod control mod
func (p *Poll) ControlMod(fd int, events uint32) error {
return unix.EpollCtl(p.fd, unix.EPOLL_CTL_MOD, fd,
&unix.EpollEvent{
Fd: int32(fd),
Events: events,
},
)
}
// ControlDel control del
func (p *Poll) ControlDel(fd int, events uint32) error {
return unix.EpollCtl(p.fd, unix.EPOLL_CTL_DEL, fd,
&unix.EpollEvent{
Fd: int32(fd),
Events: events,
},
)
}