forked from davyxu/cellnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeta.go
More file actions
226 lines (168 loc) · 3.6 KB
/
meta.go
File metadata and controls
226 lines (168 loc) · 3.6 KB
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
package cellnet
import (
"fmt"
"path"
"reflect"
"regexp"
"strings"
)
// 消息元信息
type MessageMeta struct {
Codec Codec // 消息用到的编码
Type reflect.Type // 消息类型
ID int // 消息ID (二进制协议中使用)
}
func (self *MessageMeta) TypeName() string {
if self == nil {
return ""
}
if self.Type.Kind() == reflect.Ptr {
return self.Type.Elem().Name()
}
return self.Type.Name()
}
func (self *MessageMeta) FullName() string {
if self == nil {
return ""
}
rtype := self.Type
if rtype.Kind() == reflect.Ptr {
rtype = rtype.Elem()
}
var sb strings.Builder
sb.WriteString(path.Base(rtype.PkgPath()))
sb.WriteString(".")
sb.WriteString(rtype.Name())
return sb.String()
}
func (self *MessageMeta) NewType() interface{} {
return reflect.New(self.Type).Interface()
}
var (
// 消息元信息与消息名称,消息ID和消息类型的关联关系
metaByFullName = map[string]*MessageMeta{}
metaByID = map[int]*MessageMeta{}
metaByType = map[reflect.Type]*MessageMeta{}
)
/*
http消息
Method URL -> Meta
Type -> Meta
非http消息
ID -> Meta
Type -> Meta
*/
// 注册消息元信息
func RegisterMessageMeta(meta *MessageMeta) {
// 非http类,才需要包装Type必须唯一
if _, ok := metaByType[meta.Type]; ok {
panic(fmt.Sprintf("Duplicate message meta register by type: %d", meta.ID))
} else {
metaByType[meta.Type] = meta
}
if _, ok := metaByFullName[meta.FullName()]; ok {
panic(fmt.Sprintf("Duplicate message meta register by fullname: %s", meta.FullName()))
} else {
metaByFullName[meta.FullName()] = meta
}
if meta.ID == 0 {
panic("message meta require 'ID' field: " + meta.TypeName())
}
if _, ok := metaByID[meta.ID]; ok {
panic(fmt.Sprintf("Duplicate message meta register by id: %d", meta.ID))
} else {
metaByID[meta.ID] = meta
}
}
// 根据名字查找消息元信息
func MessageMetaByFullName(name string) *MessageMeta {
if v, ok := metaByFullName[name]; ok {
return v
}
return nil
}
func MessageMetaVisit(nameRule string, callback func(meta *MessageMeta) bool) error {
exp, err := regexp.Compile(nameRule)
if err != nil {
return err
}
for name, meta := range metaByFullName {
if exp.MatchString(name) {
if !callback(meta) {
return nil
}
}
}
return nil
}
// 根据类型查找消息元信息
func MessageMetaByType(t reflect.Type) *MessageMeta {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if v, ok := metaByType[t]; ok {
return v
}
return nil
}
// 根据消息对象获得消息元信息
func MessageMetaByMsg(msg interface{}) *MessageMeta {
if msg == nil {
return nil
}
return MessageMetaByType(reflect.TypeOf(msg))
}
// 根据id查找消息元信息
func MessageMetaByID(id int) *MessageMeta {
if v, ok := metaByID[id]; ok {
return v
}
return nil
}
func MessageToName(msg interface{}) string {
if msg == nil {
return ""
}
meta := MessageMetaByMsg(msg)
if meta == nil {
return ""
}
return meta.TypeName()
}
func MessageToID(msg interface{}) int {
if msg == nil {
return 0
}
meta := MessageMetaByMsg(msg)
if meta == nil {
return 0
}
return int(meta.ID)
}
func MessageSize(msg interface{}) int {
if msg == nil {
return 0
}
// 获取消息元信息
meta := MessageMetaByType(reflect.TypeOf(msg))
if meta == nil {
return 0
}
// 将消息编码为字节数组
raw, err := meta.Codec.Encode(msg, nil)
if err != nil {
return 0
}
return len(raw.([]byte))
}
func MessageToString(msg interface{}) string {
if msg == nil {
return ""
}
if stringer, ok := msg.(interface {
String() string
}); ok {
return stringer.String()
}
return ""
}