-
Notifications
You must be signed in to change notification settings - Fork 0
/
gnetserver.go
396 lines (354 loc) · 10.7 KB
/
gnetserver.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package gnetserver
// https://github.com/yuwf/gobase
import (
"context"
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/yuwf/gobase/utils"
"github.com/panjf2000/gnet"
"github.com/rs/zerolog/log"
)
// GNetServer
// ClientId客户端ID类型
// ClientInfo是和业务相关的客户端信息结构类型
type GNetServer[ClientId any, ClientInfo any] struct {
*gnet.EventServer
// 不可需改
Address string // 监听地址
Scheme string // scheme支持tcp和ws,为空表示tcp
event GNetEvent[ClientInfo] //event
state int32 // 运行状态 0:未运行 1:开启监听
//所有的连接的客户端 [gnet.Conn:*gClient]
connMap *sync.Map
//外层添加的用户映射 [ClientId:*gClient]
clientMap *sync.Map
// 请求处理完后回调 不使用锁,默认要求提前注册好
hook []GNetHook[ClientInfo]
}
// GNetClient过渡对象,便于保存id,减少GNetClient的复杂度
type gClient[ClientId any, ClientInfo any] struct {
gc *GNetClient[ClientInfo]
id ClientId // 调用GNetServer.AddClient设置的id 目前无锁 不存在复杂使用
}
// 创建服务器
// scheme支持tcp和ws,为空表示tcp
func NewGNetServer[ClientId any, ClientInfo any](port int, event GNetEvent[ClientInfo]) *GNetServer[ClientId, ClientInfo] {
s := &GNetServer[ClientId, ClientInfo]{
Address: fmt.Sprintf("tcp://:%d", port),
Scheme: "tcp",
event: event,
state: 0,
EventServer: &gnet.EventServer{},
connMap: new(sync.Map),
clientMap: new(sync.Map),
}
return s
}
func NewGNetServerWS[ClientId any, ClientInfo any](port int, event GNetEvent[ClientInfo]) *GNetServer[ClientId, ClientInfo] {
s := &GNetServer[ClientId, ClientInfo]{
Address: fmt.Sprintf("tcp://:%d", port),
Scheme: "ws",
event: event,
state: 0,
EventServer: &gnet.EventServer{},
connMap: new(sync.Map),
clientMap: new(sync.Map),
}
return s
}
// 开启监听
func (s *GNetServer[ClientId, ClientInfo]) Start() error {
if !atomic.CompareAndSwapInt32(&s.state, 0, 1) {
log.Error().Str("Addr", s.Address).Msg("GNetServer already Start")
return nil
}
log.Info().Str("Addr", s.Address).Msg("GNetServer Starting")
// 开启监听 gnet.Serve会阻塞
go func() {
err := gnet.Serve(s, s.Address,
gnet.WithMulticore(true),
gnet.WithTCPKeepAlive(time.Minute*2),
gnet.WithCodec(s),
gnet.WithReusePort(true),
gnet.WithTicker(true))
if err == nil {
log.Info().Str("Addr", s.Address).Msg("GNetServer Exist")
} else {
atomic.StoreInt32(&s.state, 0)
log.Error().Err(err).Str("Addr", s.Address).Msg("GNetServer Start error")
}
}()
return nil
}
func (s *GNetServer[ClientId, ClientInfo]) Stop() error {
if !atomic.CompareAndSwapInt32(&s.state, 1, 0) {
log.Error().Str("Addr", s.Address).Msg("GNetServer already Stop")
return nil
}
log.Info().Str("Addr", s.Address).Msg("GNetServer Stoping")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := gnet.Stop(ctx, s.Address) // 会回调所有连接的的OnClosed 和 OnShutdown
if err != nil {
log.Error().Err(err).Str("Addr", s.Address).Msg("GNetServer Stop error")
}
return nil
}
// 添加用户映射
func (s *GNetServer[ClientId, ClientInfo]) AddClient(id ClientId, gc *GNetClient[ClientInfo]) {
// 先检查下是否存在连接
client, ok := s.connMap.Load(gc.conn)
if !ok {
return
}
client.(*gClient[ClientId, ClientInfo]).id = id
s.clientMap.Store(id, client)
// 回调回调hook
for _, h := range s.hook {
h.OnAddClient(gc)
}
}
func (s *GNetServer[ClientId, ClientInfo]) GetClient(id ClientId) *GNetClient[ClientInfo] {
client, ok := s.clientMap.Load(id)
if ok {
return client.(*gClient[ClientId, ClientInfo]).gc
}
return nil
}
func (s *GNetServer[ClientId, ClientInfo]) RemoveClient(id ClientId) *GNetClient[ClientInfo] {
client, ok := s.clientMap.Load(id)
if ok {
s.clientMap.Delete(id)
gc := client.(*gClient[ClientId, ClientInfo]).gc
// 回调hook
for _, h := range s.hook {
h.OnRemoveClient(gc)
}
return gc
}
return nil
}
// 主动关闭 不会回调事件的OnDisConnect
// 使用GNetClient.Close会回调事件的OnDisConnect
func (s *GNetServer[ClientId, ClientInfo]) CloseClient(id ClientId) {
client, ok := s.clientMap.Load(id)
if ok {
gc := client.(*gClient[ClientId, ClientInfo]).gc
log.Info().Str("Name", gc.ConnName()).Msg("OnClosed CloseClient") // 日志为GNetServer OnClosed 便于和下面的OnClosed统一查找
s.connMap.Delete(gc.conn)
s.clientMap.Delete(id)
gc.Close(nil) // 会回调GNetServer的OnClosed 所以上面先删除对象
// 回调
for _, h := range s.hook {
h.OnDisConnect(gc, true, nil)
}
}
}
// 遍历Client f函数返回false 停止遍历
func (s *GNetServer[ClientId, ClientInfo]) RangeClient(f func(gc *GNetClient[ClientInfo]) bool) {
s.connMap.Range(func(key, value interface{}) bool {
gclient := value.(*gClient[ClientId, ClientInfo])
gc := gclient.gc
return f(gc)
})
}
func (s *GNetServer[ClientId, ClientInfo]) Send(ctx context.Context, id ClientId, data []byte) error {
client, ok := s.clientMap.Load(id)
if ok {
return client.(*gClient[ClientId, ClientInfo]).gc.Send(ctx, data)
}
err := fmt.Errorf("not exist client %v", id)
utils.LogCtx(log.Debug(), ctx).Err(err).Int("Size", len(data)).Msg("Send error")
return err
}
func (s *GNetServer[ClientId, ClientInfo]) SendMsg(ctx context.Context, id ClientId, msg utils.SendMsger) error {
client, ok := s.clientMap.Load(id)
if ok {
return client.(*gClient[ClientId, ClientInfo]).gc.SendMsg(ctx, msg)
}
err := fmt.Errorf("not exist client %v", id)
utils.LogCtx(log.Debug(), ctx).Err(err).Interface("Msg", msg).Msg("SendMsg error")
return err
}
func (s *GNetServer[ClientId, ClientInfo]) ConnCount() (int, int) {
count := 0
handshakecount := 0
s.connMap.Range(func(key, value interface{}) bool {
count++
gc := value.(*gClient[ClientId, ClientInfo]).gc
if gc.wsh != nil && gc.wsh.upgrade {
handshakecount++
}
return true
})
return count, handshakecount
}
func (s *GNetServer[ClientId, ClientInfo]) ClientCount() int {
count := 0
s.clientMap.Range(func(key, value interface{}) bool {
count++
return true
})
return count
}
// 注册hook
func (s *GNetServer[ClientId, ClientInfo]) RegHook(h GNetHook[ClientInfo]) {
s.hook = append(s.hook, h)
}
// 编解码原样返回
func (s *GNetServer[ClientId, ClientInfo]) Encode(c gnet.Conn, buf []byte) ([]byte, error) {
// 回调
client, ok := s.connMap.Load(c)
if ok {
gc := client.(*gClient[ClientId, ClientInfo]).gc
for _, h := range s.hook {
h.OnSend(gc, len(buf))
}
}
return buf, nil
}
// 返回值[]byte 就是React的packet
func (s *GNetServer[ClientId, ClientInfo]) Decode(c gnet.Conn) ([]byte, error) {
buf := c.Read()
if len(buf) == 0 {
return nil, nil
}
// 回调
client, ok := s.connMap.Load(c)
if ok {
gc := client.(*gClient[ClientId, ClientInfo]).gc
for _, h := range s.hook {
h.OnRecv(gc, len(buf))
}
}
return buf, nil
}
func (s *GNetServer[ClientId, ClientInfo]) OnInitComplete(server gnet.Server) (action gnet.Action) {
log.Info().Str("Addr", server.Addr.String()).Msg("GNetServer InitComplete")
return
}
func (s *GNetServer[ClientId, ClientInfo]) OnShutdown(server gnet.Server) {
log.Info().Str("Addr", server.Addr.String()).Msg("GNetServer Shutdown")
}
func (s *GNetServer[ClientId, ClientInfo]) OnOpened(c gnet.Conn) (out []byte, action gnet.Action) {
logOut := !ParamConf.Get().IsIgnoreIp(c.RemoteAddr().String())
if logOut {
log.Info().Str("RemoveAddr", c.RemoteAddr().String()).Str("LocalAddr", c.LocalAddr().String()).Msg("OnOpened")
}
gc := newGNetClient(c, s.event, s.hook)
if s.Scheme == "ws" {
gc.ctx = context.WithValue(gc.ctx, CtxKey_WS, 1)
gc.wsh = newGNetWSHandler(gc)
}
client := &gClient[ClientId, ClientInfo]{
gc: gc,
}
// 如果clientName还为空 就用client里面的id来表示
if client.gc.connName == nil {
gc.connName = func() string {
name := fmt.Sprintf("%v", client.id)
if len(name) == 0 || name == "0" {
return gc.removeAddr.String()
}
return name
}
}
s.connMap.Store(c, client)
if s.event != nil {
gc.seq.Submit(func() {
ctx := context.WithValue(gc.ctx, utils.CtxKey_traceId, utils.GenTraceID())
s.event.OnConnected(ctx, gc)
})
}
// 回调
for _, h := range s.hook {
h.OnConnected(gc)
}
return
}
func (s *GNetServer[ClientId, ClientInfo]) OnClosed(c gnet.Conn, err error) (action gnet.Action) {
client, ok := s.connMap.Load(c)
if ok {
gc := client.(*gClient[ClientId, ClientInfo]).gc
logOut := !ParamConf.Get().IsIgnoreIp(gc.removeAddr.String())
if logOut {
if gc.closeReason != nil {
err = gc.closeReason
}
log.Info().Err(err).Str("Name", gc.ConnName()).Str("RemoveAddr", gc.removeAddr.String()).Msg("OnClosed")
}
s.connMap.Delete(c)
_, delClient := s.clientMap.LoadAndDelete(client.(*gClient[ClientId, ClientInfo]).id)
if s.event != nil {
gc.seq.Submit(func() {
ctx := context.WithValue(gc.ctx, utils.CtxKey_traceId, utils.GenTraceID())
s.event.OnDisConnect(ctx, gc)
})
}
// 回调
for _, h := range s.hook {
h.OnDisConnect(gc, delClient, err)
}
}
return
}
func (s *GNetServer[ClientId, ClientInfo]) React(packet []byte, c gnet.Conn) (out []byte, action gnet.Action) {
if c.BufferLength() == 0 {
return
}
client, ok := s.connMap.Load(c)
if ok {
gclient := client.(*gClient[ClientId, ClientInfo])
gc := gclient.gc
atomic.StoreInt64(&gc.lastRecvTime, time.Now().UnixMicro())
// 是否websock
if gc.wsh != nil {
len, handshake, err := gc.wsh.recv(c.Read())
if err != nil {
c.ResetBuffer() // 先调用下,防止gnet.Conn有数据还会继续调用
gc.Close(err)
return
}
if handshake {
// 查找真正的ip
addr := utils.ClientTCPIPHeader(gc.wsh.Header)
if addr != nil {
gc.removeAddr = *addr
}
log.Info().Str("RemoveAddr", gc.removeAddr.String()+"("+gc.conn.RemoteAddr().String()+")").Interface("Header", gc.wsh.Header).Msg("HandShake")
// 回调
for _, h := range s.hook {
h.OnWSHandShake(gc)
}
}
c.ShiftN(len)
} else {
len, err := gc.recv(gc.ctx, c.Read())
if err != nil {
c.ResetBuffer() // 先调用下,防止gnet.Conn有数据还会继续调用
gc.Close(err)
return
}
c.ShiftN(len)
}
}
return
}
func (s *GNetServer[ClientId, ClientInfo]) Tick() (delay time.Duration, action gnet.Action) {
delay = time.Second
if s.event != nil {
s.connMap.Range(func(key, value interface{}) bool {
gclient := value.(*gClient[ClientId, ClientInfo])
gc := gclient.gc
ctx := context.WithValue(gc.ctx, utils.CtxKey_traceId, utils.GenTraceID())
ctx = context.WithValue(ctx, utils.CtxKey_msgId, "_tick_")
gc.seq.Submit(func() {
s.event.OnTick(ctx, gc)
})
return true
})
}
return
}