forked from davyxu/cellnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeer.go
48 lines (33 loc) · 959 Bytes
/
peer.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
package cellnet
// 发起和接受连接的通讯端,某些Peer实现了SessionAccessor
type Peer interface {
// 开启端,传入地址
Start() Peer
// 停止通讯端
Stop()
// Peer的名称(自定义名称,会出现在msglog中)
Name() string
// Peer的类型(protocol.type),例如tcp.Connector/udp.Acceptor
TypeName() string
// 侦听或者连接的地址:端口
Address() string
// 队列
EventQueue() EventQueue
}
type PeerCreateFunc func() Peer
var creatorByTypeName = map[string]PeerCreateFunc{}
func RegisterPeerCreator(f PeerCreateFunc) {
// 临时实例化一个,获取类型
dummyPeer := f()
if _, ok := creatorByTypeName[dummyPeer.TypeName()]; ok {
panic("Duplicate peer type")
}
creatorByTypeName[dummyPeer.TypeName()] = f
}
func NewPeer(peerType string) Peer {
peerCreator := creatorByTypeName[peerType]
if peerCreator == nil {
panic("Peer name not found: " + peerType)
}
return peerCreator()
}