forked from davyxu/cellnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
procreg.go
65 lines (50 loc) · 1.43 KB
/
procreg.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
package proc
import (
"fmt"
"github.com/davyxu/cellnet"
"sort"
)
type ProcessorBinder func(bundle ProcessorBundle, userCallback cellnet.EventCallback)
var (
procByName = map[string]ProcessorBinder{}
)
// 注册事件处理器,内部及自定义收发流程时使用
func RegisterProcessor(procName string, f ProcessorBinder) {
if _, ok := procByName[procName]; ok {
panic("duplicate peer type: " + procName)
}
procByName[procName] = f
}
// 获取处理器列表
func ProcessorList() (ret []string) {
for name := range procByName {
ret = append(ret, name)
}
sort.Strings(ret)
return
}
func getPackageByCodecName(name string) string {
switch name {
case "gorillaws.ltv":
return "github.com/davyxu/cellnet/proc/gorillaws"
case "http":
return "github.com/davyxu/cellnet/proc/http"
case "tcp.ltv":
return "github.com/davyxu/cellnet/proc/tcp"
case "udp.ltv":
return "github.com/davyxu/cellnet/proc/udp"
default:
return "package/to/your/proc"
}
}
// 绑定固定回调处理器, procName来源于RegisterProcessor注册的处理器,形如: 'tcp.ltv'
func BindProcessorHandler(peer cellnet.Peer, procName string, userCallback cellnet.EventCallback) {
if proc, ok := procByName[procName]; ok {
bundle := peer.(ProcessorBundle)
proc(bundle, userCallback)
} else {
panic(fmt.Sprintf("processor not found '%s'\ntry to add code below:\nimport (\n _ \"%s\"\n)\n\n",
procName,
getPackageByCodecName(procName)))
}
}