forked from lesismal/nbio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine_unix.go
157 lines (138 loc) · 3.74 KB
/
engine_unix.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
// Copyright 2020 lesismal. All rights reserved.
// 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
// +build linux darwin netbsd freebsd openbsd dragonfly
package nbio
import (
"net"
"runtime"
"strings"
"github.com/soar4/nbio/logging"
"github.com/soar4/nbio/timer"
)
// Start init and start pollers.
func (g *Engine) Start() error {
udpListeners := make([]*net.UDPConn, len(g.addrs))[0:0]
switch g.network {
case "unix", "tcp", "tcp4", "tcp6":
for i := range g.addrs {
ln, err := newPoller(g, true, i)
if err != nil {
for j := 0; j < i; j++ {
g.listeners[j].stop()
}
return err
}
g.addrs[i] = ln.listener.Addr().String()
g.listeners = append(g.listeners, ln)
}
case "udp", "udp4", "udp6":
for i, addrStr := range g.addrs {
addr, err := net.ResolveUDPAddr(g.network, addrStr)
if err != nil {
for j := 0; j < i; j++ {
udpListeners[j].Close()
}
return err
}
ln, err := g.listenUDP("udp", addr)
if err != nil {
for j := 0; j < i; j++ {
udpListeners[j].Close()
}
return err
}
g.addrs[i] = ln.LocalAddr().String()
udpListeners = append(udpListeners, ln)
}
}
for i := 0; i < g.pollerNum; i++ {
p, err := newPoller(g, false, i)
if err != nil {
for j := 0; j < len(g.listeners); j++ {
g.listeners[j].stop()
}
for j := 0; j < i; j++ {
g.pollers[j].stop()
}
return err
}
g.pollers[i] = p
}
for i := 0; i < g.pollerNum; i++ {
g.pollers[i].ReadBuffer = make([]byte, g.readBufferSize)
g.Add(1)
go g.pollers[i].start()
}
for _, l := range g.listeners {
g.Add(1)
go l.start()
}
for _, ln := range udpListeners {
_, err := g.AddConn(ln)
if err != nil {
for j := 0; j < len(g.listeners); j++ {
g.listeners[j].stop()
}
for j := 0; j < len(g.pollers); j++ {
g.pollers[j].stop()
}
for j := 0; j < len(udpListeners); j++ {
udpListeners[j].Close()
}
return err
}
}
g.Timer.Start()
if len(g.addrs) == 0 {
logging.Info("NBIO[%v] start", g.Name)
} else {
logging.Info("NBIO[%v] start listen on: [\"%v@%v\"]", g.Name, g.network, strings.Join(g.addrs, `", "`))
}
return nil
}
// NewEngine is a factory impl.
func NewEngine(conf Config) *Engine {
cpuNum := runtime.NumCPU()
if conf.Name == "" {
conf.Name = "NB"
}
if conf.NPoller <= 0 {
conf.NPoller = cpuNum
}
if conf.ReadBufferSize <= 0 {
conf.ReadBufferSize = DefaultReadBufferSize
}
if conf.MaxConnReadTimesPerEventLoop <= 0 {
conf.MaxConnReadTimesPerEventLoop = DefaultMaxConnReadTimesPerEventLoop
}
if conf.Listen == nil {
conf.Listen = net.Listen
}
if conf.ListenUDP == nil {
conf.ListenUDP = net.ListenUDP
}
g := &Engine{
Timer: timer.New(conf.Name, conf.TimerExecute),
Name: conf.Name,
network: conf.Network,
addrs: conf.Addrs,
listen: conf.Listen,
listenUDP: conf.ListenUDP,
pollerNum: conf.NPoller,
readBufferSize: conf.ReadBufferSize,
maxWriteBufferSize: conf.MaxWriteBufferSize,
maxConnReadTimesPerEventLoop: conf.MaxConnReadTimesPerEventLoop,
udpReadTimeout: conf.UDPReadTimeout,
epollMod: conf.EpollMod,
epollOneshot: conf.EPOLLONESHOT,
lockListener: conf.LockListener,
lockPoller: conf.LockPoller,
listeners: make([]*poller, len(conf.Addrs))[0:0],
pollers: make([]*poller, conf.NPoller),
connsUnix: make([]*Conn, MaxOpenFiles),
}
g.initHandlers()
return g
}