forked from davyxu/cellnet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlerchainmgr.go
166 lines (117 loc) · 3.53 KB
/
handlerchainmgr.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
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
package cellnet
import (
"bytes"
"sync"
)
type HandlerChainManager interface {
// 添加一条接收处理链
AddChainRecv(recv *HandlerChain) int64
// 移除接收处理链, 根据添加时的id
RemoveChainRecv(id int64)
// 接收处理链是否存在
ChainRecvExists(id int64) bool
// 获取当前的处理链(乱序)
ChainListRecv() HandlerChainList
// 设置发送处理链
SetChainSend(chain *HandlerChain)
// 获取当前发送处理链
ChainSend() *HandlerChain
// 读写链
CreateChainWrite() *HandlerChain
CreateChainRead() *HandlerChain
// 设置读写链
SetReadWriteChain(read, write func() *HandlerChain)
}
// Peer间的共享数据
type HandlerChainManagerImplement struct {
recvChainByID map[int64]*HandlerChain
recvChainGuard sync.Mutex
chainIDAcc int64
recvChainListDirty bool
recvChainList HandlerChainList
sendChain *HandlerChain
sendChainGuard sync.RWMutex
readChainCreator func() *HandlerChain
writeChainCreator func() *HandlerChain
rwChainGuard sync.RWMutex
}
func (self *HandlerChainManagerImplement) ChainRecvExists(id int64) bool {
self.recvChainGuard.Lock()
_, ok := self.recvChainByID[id]
self.recvChainGuard.Unlock()
return ok
}
func (self *HandlerChainManagerImplement) AddChainRecv(recv *HandlerChain) (autoID int64) {
self.recvChainGuard.Lock()
self.chainIDAcc++
// autoID这里是流水生成,每次添加要变化
// HandlerChain.id是固定id,用于调试用
autoID = self.chainIDAcc
self.recvChainByID[autoID] = recv
self.recvChainListDirty = true
self.recvChainGuard.Unlock()
return
}
func (self *HandlerChainManagerImplement) RemoveChainRecv(id int64) {
self.recvChainGuard.Lock()
delete(self.recvChainByID, id)
self.recvChainListDirty = true
self.recvChainGuard.Unlock()
}
func (self *HandlerChainManagerImplement) SetChainSend(chain *HandlerChain) {
self.sendChainGuard.Lock()
self.sendChain = chain
self.sendChainGuard.Unlock()
}
func (self *HandlerChainManagerImplement) CreateChainWrite() *HandlerChain {
self.rwChainGuard.Lock()
defer self.rwChainGuard.Unlock()
return self.writeChainCreator()
}
func (self *HandlerChainManagerImplement) CreateChainRead() *HandlerChain {
self.rwChainGuard.Lock()
defer self.rwChainGuard.Unlock()
return self.readChainCreator()
}
func (self *HandlerChainManagerImplement) SetReadWriteChain(read, write func() *HandlerChain) {
self.rwChainGuard.Lock()
if read != nil {
self.readChainCreator = read
}
if write != nil {
self.writeChainCreator = write
}
self.rwChainGuard.Unlock()
}
func (self *HandlerChainManagerImplement) ChainSend() *HandlerChain {
self.sendChainGuard.Lock()
defer self.sendChainGuard.Unlock()
return self.sendChain
}
func (self *HandlerChainManagerImplement) ChainListRecv() HandlerChainList {
self.recvChainGuard.Lock()
defer self.recvChainGuard.Unlock()
if self.recvChainListDirty {
self.recvChainList = make(HandlerChainList, len(self.recvChainByID))
index := 0
for _, chain := range self.recvChainByID {
self.recvChainList[index] = chain
index++
}
self.recvChainListDirty = false
}
return self.recvChainList
}
func (self *HandlerChainManagerImplement) ChainString() string {
var buff bytes.Buffer
buff.WriteString("ChainRecv:\n")
buff.WriteString(self.ChainListRecv().String())
buff.WriteString("ChainSend:\n")
buff.WriteString(self.ChainSend().String())
return buff.String()
}
func NewHandlerChainManager() *HandlerChainManagerImplement {
return &HandlerChainManagerImplement{
recvChainByID: make(map[int64]*HandlerChain),
}
}