-
Notifications
You must be signed in to change notification settings - Fork 70
/
einx_default.go
100 lines (81 loc) · 2.17 KB
/
einx_default.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
package einx
import (
"github.com/Cyinx/einx/console"
"github.com/Cyinx/einx/event"
"github.com/Cyinx/einx/lua"
"github.com/Cyinx/einx/module"
"github.com/Cyinx/einx/network"
"github.com/Cyinx/einx/slog"
"os"
"os/signal"
)
var _einxDefault = &einx{
closeChan: make(chan bool),
onClose: nil,
}
func Init(opts ...Option) {
for _, opt := range opts {
opt()
}
}
func Run() {
console.Run()
network.Run()
module.Start()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
sig := <-c
slog.LogWarning("einx", "einx will close down (signal: %v)", sig)
_einxDefault.doClose()
}
func ContinueClose() {
_einxDefault.continueClose()
}
func Close() {
_einxDefault.close()
slog.Close()
}
func GetModule(name string) Module {
return module.GetModule(name)
}
func NewLuaStae() *lua_state.LuaRuntime {
return lua_state.NewLuaStae()
}
func AddTcpServerMgr(m module.Module, addr string, mgr interface{}, opts ...Option) {
er := m.(event.EventReceiver)
opts = append(opts, NetworkOption.ListenAddr(addr))
opts = append(opts, network.Module(er))
opts = append(opts, NetworkOption.ServeHandler(mgr.(SessionHandler)))
tcpServer := network.NewTcpServerMgr(opts...)
e := &event.ComponentEventMsg{}
e.MsgType = event.EVENT_COMPONENT_CREATE
e.Sender = tcpServer
e.Attach = mgr
er.PushEventMsg(e)
}
func StartTcpClientMgr(m module.Module, name string, mgr interface{}, opts ...Option) {
er := m.(event.EventReceiver)
opts = append(opts, NetworkOption.Name(name))
opts = append(opts, network.Module(er))
opts = append(opts, NetworkOption.ServeHandler(mgr.(SessionHandler)))
tcpClient := network.NewTcpClientMgr(opts...)
e := &event.ComponentEventMsg{}
e.MsgType = event.EVENT_COMPONENT_CREATE
e.Sender = tcpClient
e.Attach = mgr
er.PushEventMsg(e)
}
func AddModuleComponent(m module.Module, c Component, mgr interface{}) {
er := m.(event.EventReceiver)
e := &event.ComponentEventMsg{}
e.MsgType = event.EVENT_COMPONENT_CREATE
e.Sender = c
e.Attach = mgr
er.PushEventMsg(e)
}
func CreateModuleWorkers(name string, size int) WorkerPool {
return module.CreateWorkers(name, size)
}
func GetWorkerPool(name string) WorkerPool {
return module.GetWorkerPool(name)
}