-
Notifications
You must be signed in to change notification settings - Fork 6
/
engine.go
165 lines (134 loc) · 2.74 KB
/
engine.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
// Copyright (c) 2023 Remember
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package easyio
import (
"fmt"
"net"
"runtime"
)
var (
MaxOpenFiles = 1024 * 1024 * 2
)
func New(network, addr string, fns ...Option) *Engine {
e := new(Engine)
opts := new(Options)
for _, opt := range fns {
opt(opts)
}
e.options = opts
e.exitCh = make(chan struct{})
e.network = network
e.addr = addr
return e
}
type Engine struct {
network string
addr string
exitCh chan struct{}
listener *Listener
pollerManger *Manger
conns []Conn
options *Options
}
func (e *Engine) Start() (err error) {
fmt.Println("engine start")
e.init()
// new a listener
ln, err := e.options.listener(e.network, e.addr)
if err != nil {
return err
}
listener := new(Listener)
listener.engine = e
listener.listener = ln
listener.addr = ln.Addr()
e.listener = listener
//init poller manger
if e.pollerManger, err = NewManger(e, e.options.numPoller); err != nil {
return err
}
go e.acceptPolling(true) //nolint:errcheck
return nil
}
func (e *Engine) init() {
if e.options.listener == nil {
e.options.listener = net.Listen
}
if e.options.numPoller <= 0 {
e.options.numPoller = runtime.NumCPU()
}
if e.options.eventHandler == nil {
e.options.eventHandler = new(eventHandler)
}
if e.options.byteBuffer == nil {
e.options.byteBuffer = new(BufferDefault)
}
e.conns = make([]Conn, MaxOpenFiles)
}
func (e *Engine) Stop() error {
close(e.exitCh)
// listener close
e.listener.Close()
// conns close
for _, conn := range e.conns {
if conn == nil {
continue
}
conn.Close()
}
// poller stop
e.pollerManger.Stop()
return nil
}
func (e *Engine) GetByteBuffer() ByteBuffer {
return e.options.byteBuffer
}
func (e *Engine) GetEventHandler() EventHandler {
return e.options.eventHandler
}
func (e *Engine) AddConn(conn Conn) {
e.conns[conn.Fd()] = conn
}
func (e *Engine) Remove(pd int) {
e.conns[pd] = nil
}
func (e *Engine) GetConn(pd int) Conn {
if pd >= len(e.conns) {
panic("fd conn is not exist")
}
return e.conns[pd]
}
func (e *Engine) acceptPolling(localOSThread bool) error {
if localOSThread {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
}
handler := e.GetEventHandler()
for {
select {
case <-e.exitCh:
fmt.Println("engine out")
return nil
default:
nc, err := e.listener.Accept()
if err != nil {
continue
}
if nc == nil {
continue
}
ec := nc.(*conn)
poller := e.pollerManger.Pick(ec.Fd())
ec.poller = poller
// set ctx
ec.ctx = handler.OnOpen(ec)
if err = poller.AddRead(ec.Fd()); err != nil {
fmt.Println("poller.AddRead:", err)
nc.Close()
continue
}
e.conns[ec.Fd()] = ec
}
}
}