-
Notifications
You must be signed in to change notification settings - Fork 0
/
gnet_plugin.go
75 lines (59 loc) · 1.69 KB
/
gnet_plugin.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
package gnet
import (
"context"
"log"
"net"
"github.com/gomystery/easynet/interface"
"github.com/panjf2000/gnet/v2"
)
type GnetEasyNetPlugin struct {
Conn net.Conn
Ctx context.Context
Config *YamlConfig
Server *GnetServer
Handler _interface.IEasyNet
}
func NewGnetEasyNetPlugin(ctx context.Context, iconfig _interface.IConfig, handler _interface.IEasyNet) *GnetEasyNetPlugin {
var config *YamlConfig
var ok bool
if config, ok = iconfig.(*YamlConfig); !ok {
log.Printf("gnet yaml error \n")
}
gnetEasyNetPlugin := &GnetEasyNetPlugin{
Ctx: ctx,
Config: config,
Handler: handler,
}
gnetServer := NewGnetServer(ctx, config, handler)
gnetEasyNetPlugin.Server = gnetServer
return gnetEasyNetPlugin
}
func (g GnetEasyNetPlugin) Run() error {
var optionArr = []gnet.Option{}
if g.Config.GetMulticore() {
optionArr = append(optionArr, gnet.WithMulticore(g.Config.GetMulticore()))
}
if g.Config.GetLockosthread() {
optionArr = append(optionArr, gnet.WithLockOSThread(g.Config.GetLockosthread()))
}
if g.Config.GetReadBufferCap() > 0 {
optionArr = append(optionArr, gnet.WithReadBufferCap(int(g.Config.GetReadBufferCap())))
}
if g.Config.GetWriteBufferCap() > 0 {
optionArr = append(optionArr, gnet.WithWriteBufferCap(int(g.Config.GetWriteBufferCap())))
}
if g.Config.GetNumEventLoop() > 0 {
optionArr = append(optionArr, gnet.WithNumEventLoop(int(g.Config.GetNumEventLoop())))
}
if g.Config.GetReusePort() {
optionArr = append(optionArr, gnet.WithReusePort(g.Config.GetReusePort()))
}
if g.Config.GetReuseAddr() {
optionArr = append(optionArr, gnet.WithReuseAddr(g.Config.GetReuseAddr()))
}
err := gnet.Run(
g.Server,
g.Server.addr,
optionArr...)
return err
}