/
epoll.go
177 lines (156 loc) · 5.93 KB
/
epoll.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
// Copyright (c) 2019 Andy Pan
// Copyright (c) 2017 Joshua J Baker
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// +build linux
package netpoll
import (
"os"
"unsafe"
"github.com/NeoclubTechnology/gnet/errors"
"github.com/NeoclubTechnology/gnet/internal"
"github.com/NeoclubTechnology/gnet/internal/logging"
"golang.org/x/sys/unix"
)
// Poller represents a poller which is in charge of monitoring file-descriptors.
type Poller struct {
fd int // epoll fd
wfd int // wake fd
wfdBuf []byte // wfd buffer to read packet
asyncJobQueue internal.AsyncJobQueue
}
// OpenPoller instantiates a poller.
func OpenPoller() (poller *Poller, err error) {
poller = new(Poller)
if poller.fd, err = unix.EpollCreate1(unix.EPOLL_CLOEXEC); err != nil {
poller = nil
err = os.NewSyscallError("epoll_create1", err)
return
}
if poller.wfd, err = unix.Eventfd(0, unix.EFD_NONBLOCK|unix.EFD_CLOEXEC); err != nil {
_ = poller.Close()
poller = nil
err = os.NewSyscallError("eventfd", err)
return
}
poller.wfdBuf = make([]byte, 8)
if err = poller.AddRead(poller.wfd); err != nil {
_ = poller.Close()
poller = nil
return
}
poller.asyncJobQueue = internal.NewAsyncJobQueue()
return
}
// Close closes the poller.
func (p *Poller) Close() error {
if err := os.NewSyscallError("close", unix.Close(p.fd)); err != nil {
return err
}
return os.NewSyscallError("close", unix.Close(p.wfd))
}
// 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 (
u uint64 = 1
b = (*(*[8]byte)(unsafe.Pointer(&u)))[:]
)
// Trigger wakes up the poller blocked in waiting for network-events and runs jobs in asyncJobQueue.
func (p *Poller) Trigger(job internal.Job) (err error) {
if p.asyncJobQueue.Push(job) == 1 {
_, err = unix.Write(p.wfd, b)
}
return os.NewSyscallError("write", err)
}
// Polling blocks the current goroutine, waiting for network-events.
func (p *Poller) Polling(callback func(fd int, ev uint32) error) error {
el := newEventList(InitEvents)
var wakenUp bool
for {
n, err := unix.EpollWait(p.fd, el.events, -1)
if err != nil && err != unix.EINTR {
logging.DefaultLogger.Warnf("Error occurs in epoll: %v", os.NewSyscallError("epoll_wait", err))
continue
}
for i := 0; i < n; i++ {
if fd := int(el.events[i].Fd); fd != p.wfd {
logging.DefaultLogger.Infof("Polling fd:%d e:%d", fd, el.events[i].Events)
switch err = callback(fd, el.events[i].Events); err {
case nil:
logging.DefaultLogger.Infof("Polling nil fd:%d e:%d", fd, el.events[i].Events)
case errors.ErrAcceptSocket, errors.ErrServerShutdown:
logging.DefaultLogger.Infof("Polling err fd:%d e:%d", fd, el.events[i].Events)
return err
default:
logging.DefaultLogger.Warnf("Error occurs in event-loop: %v", err)
}
} else {
wakenUp = true
_, _ = unix.Read(p.wfd, p.wfdBuf)
}
}
if wakenUp {
wakenUp = false
switch err = p.asyncJobQueue.ForEach(); err {
case nil:
case errors.ErrServerShutdown:
return err
default:
logging.DefaultLogger.Warnf("Error occurs in user-defined function, %v", err)
}
}
if n == el.size {
el.increase()
}
}
}
const (
readEvents = unix.EPOLLPRI | unix.EPOLLIN
writeEvents = unix.EPOLLOUT
readWriteEvents = readEvents | writeEvents
)
// AddReadWrite registers the given file-descriptor with readable and writable events to the poller.
func (p *Poller) AddReadWrite(fd int) error {
return os.NewSyscallError("epoll_ctl add",
unix.EpollCtl(p.fd, unix.EPOLL_CTL_ADD, fd, &unix.EpollEvent{Fd: int32(fd), Events: readWriteEvents}))
}
// AddRead registers the given file-descriptor with readable event to the poller.
func (p *Poller) AddRead(fd int) error {
return os.NewSyscallError("epoll_ctl add",
unix.EpollCtl(p.fd, unix.EPOLL_CTL_ADD, fd, &unix.EpollEvent{Fd: int32(fd), Events: readEvents}))
}
// AddWrite registers the given file-descriptor with writable event to the poller.
func (p *Poller) AddWrite(fd int) error {
return os.NewSyscallError("epoll_ctl add",
unix.EpollCtl(p.fd, unix.EPOLL_CTL_ADD, fd, &unix.EpollEvent{Fd: int32(fd), Events: writeEvents}))
}
// ModRead renews the given file-descriptor with readable event in the poller.
func (p *Poller) ModRead(fd int) error {
return os.NewSyscallError("epoll_ctl mod",
unix.EpollCtl(p.fd, unix.EPOLL_CTL_MOD, fd, &unix.EpollEvent{Fd: int32(fd), Events: readEvents}))
}
// ModReadWrite renews the given file-descriptor with readable and writable events in the poller.
func (p *Poller) ModReadWrite(fd int) error {
return os.NewSyscallError("epoll_ctl mod",
unix.EpollCtl(p.fd, unix.EPOLL_CTL_MOD, fd, &unix.EpollEvent{Fd: int32(fd), Events: readWriteEvents}))
}
// Delete removes the given file-descriptor from the poller.
func (p *Poller) Delete(fd int) error {
return os.NewSyscallError("epoll_ctl del", unix.EpollCtl(p.fd, unix.EPOLL_CTL_DEL, fd, nil))
}