forked from panjf2000/gnet
/
server.go
188 lines (164 loc) · 4.18 KB
/
server.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
package socker
import (
"fmt"
"os"
"runtime"
"sync/atomic"
"time"
"github.com/Esbiya/gnet"
"github.com/Esbiya/loguru"
"github.com/panjf2000/ants/v2"
)
type (
Conn gnet.Conn
Action gnet.Action
Server struct {
*gnet.EventServer
mode string
addr string
router *Router
pool *ants.Pool
clients int
connected int32
disconnected int32
timeout time.Duration
multicore bool
}
)
const (
None Action = iota
Close // 连接关闭
ShutDown // 服务关闭
Continue // 消息继续
Done // 消息结束
)
const (
DefaultAntsPoolSize = 1 << 18
ExpiryDuration = 10 * time.Second
Nonblocking = true
)
const (
TCP = "tcp"
UDS = "uds"
UDP = "udp"
)
const (
DefaultUDSAddr = "/tmp/us.socket"
DefaultTCPAddr = ":20124"
)
func (u *Server) Router() *Router {
return u.router
}
func (u *Server) OnInitComplete(srv gnet.Server) (action gnet.Action) {
loguru.Info("server is listening on %s (multi-cores: %t, loops: %d)", srv.Addr.String(), srv.Multicore, srv.NumEventLoop)
return
}
func (u *Server) OnOpened(c gnet.Conn) (out []byte, action gnet.Action) {
c.SetContext(c)
atomic.AddInt32(&u.connected, 1)
msg := NewMessage("client.init", "hello world")
out = MergeBytes(IntToBytes(msg.length), msg.bytes)
if c.LocalAddr() == nil {
panic("nil local addr")
}
if c.RemoteAddr() == nil {
panic("nil local addr")
}
return
}
func (u *Server) OnClosed(c gnet.Conn, err error) (action gnet.Action) {
if c.Context() != c {
panic("invalid context")
}
atomic.AddInt32(&u.disconnected, 1)
if atomic.LoadInt32(&u.connected) == atomic.LoadInt32(&u.disconnected) &&
atomic.LoadInt32(&u.disconnected) == int32(u.clients) {
action = gnet.Shutdown
}
return
}
func NewServer(mode, addr string, multicore bool, poolSize int, timeout time.Duration) *Server {
options := ants.Options{ExpiryDuration: ExpiryDuration, Nonblocking: Nonblocking}
pool, _ := ants.NewPool(poolSize, ants.WithOptions(options))
return &Server{
mode: mode,
addr: addr,
router: &Router{
Callbacks: map[string]func(msg Data, c chan Reply){},
},
pool: pool,
timeout: timeout,
multicore: multicore,
}
}
// 记录日志
func (u *Server) RecordLog() {
err := loguru.Enable(loguru.FileLog)
if err != nil {
loguru.Error("log write file error: %v", err)
os.Exit(1)
}
}
func (u *Server) React(frame []byte, c gnet.Conn, out chan gnet.Out) {
var msg Message
err := msg.Parse(frame[4:])
reply := make(chan Reply)
if err != nil {
msg.reset(false, map[string]interface{}{
"code": 400,
"msg": "message decode err! ",
})
out <- gnet.Out{
Body: msg.out(),
}
} else {
loguru.Debug("receive message - length: %d, body: %s", msg.bodyLength, msg.BodyStringify())
_ = u.pool.Submit(func() {
u.router.Get(msg.Api)(msg.ToData(), reply)
})
LOOP:
select {
case _reply := <-reply:
msg.reset(_reply.Async, _reply.Body)
loguru.Debug("reply message - length: %d, body: %s", msg.bodyLength, msg.BodyStringify())
if msg.async {
_ = c.AsyncWrite(msg.out())
} else {
out <- gnet.Out{
Body: msg.out(),
Action: gnet.Action(_reply.Status),
}
}
switch _reply.Status {
case Continue:
goto LOOP
default:
}
case <-time.After(u.timeout):
msg.reset(false, map[string]interface{}{
"code": 500,
"msg": "process message timeout! ",
})
out <- gnet.Out{
Body: msg.out(),
}
}
}
}
func (u *Server) version() {
fmt.Println(loguru.Fuchsia(` __`))
fmt.Println(loguru.Fuchsia(` _________ _____/ /_____ _____`))
fmt.Println(loguru.Fuchsia(` / ___/ __ \/ ___/ //_/ _ \/ ___/`))
fmt.Println(loguru.Fuchsia(` (__ ) /_/ / /__/ ,< / __/ /`))
fmt.Println(loguru.Fuchsia(fmt.Sprintf(`/____/\____/\___/_/|_|\___/_/ socker v0.0.6 %s/%s`, runtime.GOOS, runtime.GOARCH)))
}
func (u *Server) Run() {
u.version()
_ = gnet.Serve(u, fmt.Sprintf("%s://%s", u.mode, u.addr), gnet.WithMulticore(u.multicore))
}
func DefaultUDSServer() *Server {
return NewServer(UDS, DefaultUDSAddr, true, DefaultAntsPoolSize, ExpiryDuration)
}
func DefaultTCPServer() *Server {
return NewServer(TCP, DefaultTCPAddr, true, DefaultAntsPoolSize, ExpiryDuration)
}