-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathrouters.go
124 lines (107 loc) · 2.96 KB
/
routers.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
package microSocket
import (
"net"
"reflect"
)
const (
DEFAULTACTION = "Default"
BEFORACTION = "BeforeRequest"
AFTERACTION = "AfterRequest"
)
type module interface {
Default(fd uint32,data map[string]string) bool
BeforeRequest(fd uint32,data map[string]string) bool
AfterRequest(fd uint32,data map[string]string) bool
}
type eventer interface {
OnHandel(fd uint32, conn net.Conn) bool
OnClose(fd uint32)
OnMessage(fd uint32, msg map[string]string) bool
}
type RoutersMap struct {
pools map[string] func(uint32,map[string]string) bool
strPools map[string] map[string] func(uint32,map[string]string) bool
structs map[string] module
events eventer
}
func NewRoutersMap() *RoutersMap{
return &RoutersMap{
pools :make(map[string]func(uint32,map[string]string) bool),
strPools:make(map[string]map[string] func(uint32,map[string]string) bool),
structs : make(map[string]module),
}
}
//注册事件
func (this *RoutersMap) RegisterEvent (events eventer) {
this.events = events
}
//注册单个逻辑
func (this *RoutersMap) RegisterFun (methodName string ,funcs func(uint32 ,map[string]string) bool) bool {
if _, exit := this.pools[methodName]; !exit {
this.pools[methodName] = funcs
return true
}
return false
}
//结构体 注册
func (this *RoutersMap) RegisterStructFun (moduleName string,mod module) bool {
if _, exit := this.strPools[moduleName]; exit {
return false
}
this.strPools[moduleName] = make(map[string] func(uint32,map[string]string) bool)
this.structs[moduleName] = mod
temType := reflect.TypeOf(mod)
temValue := reflect.ValueOf(mod)
for i := 0 ; i < temType.NumMethod(); i++ {
tem := temValue.Method(i).Interface()
if temFunc ,ok := tem.(func(uint32, map[string]string) bool); ok {
this.strPools[moduleName][temType.Method(i).Name] = temFunc
}
}
return true
}
func (this *RoutersMap) HookAction (funcionName string,fd uint32, data map[string]string) bool{
if action ,exit := this.pools[funcionName]; exit {
return action(fd, data)
} else {
return false
}
}
func (this *RoutersMap) HookModule(mouleName string, method string,fd uint32, data map[string]string) bool {
if _, exit := this.strPools[mouleName]; !exit {
return false
}
if this.strPools[mouleName][BEFORACTION](fd, data) == false {
return false
}
if action, exit := this.strPools[mouleName][method]; exit {
if action(fd, data) == false {
return false
}
} else {
if this.strPools[mouleName][DEFAULTACTION](fd, data) == false {
return false
}
}
if this.strPools[mouleName][AFTERACTION](fd, data) == false {
return false
}
return true
}
func (this *RoutersMap) OnClose(fd uint32) {
if this.events != nil {
this.events.OnClose(fd)
}
}
func (this *RoutersMap) OnHandel(fd uint32, conn net.Conn) bool {
if this.events != nil {
return this.events.OnHandel(fd, conn)
}
return true
}
func (this *RoutersMap) OnMessage(fd uint32, msg map[string]string) bool {
if this.events != nil {
return this.events.OnMessage(fd , msg)
}
return true
}