forked from yuxingfirst/tcputil
/
gateway_link.go
306 lines (244 loc) · 5.94 KB
/
gateway_link.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package netutil
import (
"errors"
"sync"
"sync/atomic"
)
const (
_GATEWAY_COMMAND_NONE_ = 0
_GATEWAY_COMMAND_ADD_CLIENT_ = 1
_GATEWAY_COMMAND_DEL_CLIENT_ = 2
_GATEWAY_COMMAND_BROADCAST_ = 3
_GATEWAY_MAX_CLIENT_ID_ = 0x00FFFFFF
)
type GatewayLink struct {
owner *GatewayFrontend
linkId uint32
backendId uint32
net string
addr string
maxClientNum int
takeClientAddr bool
conn *Conn
clients []*GatewayClient
clientsMutex sync.RWMutex
maxClientId int
closed int32
}
func newGatewayLink(owner *GatewayFrontend, backend *GatewayBackendInfo, pack, maxPackSize int) (*GatewayLink, error) {
var (
this *GatewayLink
conn *Conn
err error
linkIdMsg []byte
linkId uint32
)
if conn, err = Connect(backend.Net, backend.Addr, pack, 0, maxPackSize); err != nil {
return nil, err
}
if linkIdMsg = conn.Read(); linkIdMsg == nil {
return nil, errors.New("wait link id failed")
}
linkId = getUint32(linkIdMsg)
this = &GatewayLink{
owner: owner,
linkId: linkId,
backendId: backend.Id,
net: backend.Net,
addr: backend.Addr,
maxClientNum: backend.MaxClientNum,
takeClientAddr: backend.TakeClientAddr,
conn: conn,
clients: make([]*GatewayClient, backend.MaxClientNum),
}
go func() {
defer this.Close(true)
for {
var msg = this.conn.ReadPackage()
if msg == nil {
break
}
switch msg.ReadUint8() {
case _GATEWAY_COMMAND_NONE_:
var clientId = msg.ReadUint32()
if client := this.GetClient(clientId); client != nil {
if this.owner.counterOn {
atomic.AddUint64(&this.owner.outPack, uint64(1))
atomic.AddUint64(&this.owner.outByte, uint64(len(msg.Data)))
}
client.Send(msg.Data)
}
case _GATEWAY_COMMAND_DEL_CLIENT_:
var clientId = msg.ReadUint32()
this.DelClient(clientId)
case _GATEWAY_COMMAND_BROADCAST_:
var (
idNum = int(msg.ReadUint16())
realMsg = msg.Data[4*idNum:]
realSendNum = 0
)
for i := 0; i < idNum; i++ {
var clientId = msg.ReadUint32()
if client := this.GetClient(clientId); client != nil {
realSendNum += 1
client.Send(realMsg)
}
}
if this.owner.counterOn {
var bytes = uint64(realSendNum * len(realMsg))
atomic.AddUint64(&this.owner.outPack, uint64(realSendNum))
atomic.AddUint64(&this.owner.outByte, bytes)
atomic.AddUint64(&this.owner.broPack, uint64(1))
atomic.AddUint64(&this.owner.broByte, bytes)
}
}
}
}()
return this, nil
}
func (this *GatewayLink) AddClient(conn *Conn) *GatewayClient {
this.clientsMutex.Lock()
defer this.clientsMutex.Unlock()
this.maxClientId += 1
if this.maxClientId == this.maxClientNum {
this.maxClientId = 0
}
var clientId = -1
if this.clients[this.maxClientId] == nil {
clientId = int(this.maxClientId)
} else {
for i := 0; i < len(this.clients); i++ {
if this.clients[i] != nil {
clientId = i
break
}
}
}
if clientId >= 0 {
var client = newGatewayClient(this, this.linkId+uint32(clientId), conn)
this.clients[clientId] = client
return client
}
return nil
}
func (this *GatewayLink) DelClient(clientId uint32) {
this.clientsMutex.Lock()
defer this.clientsMutex.Unlock()
var index = int(clientId - this.linkId)
if index >= this.maxClientNum || index < 0 {
return
}
var client = this.clients[index]
this.clients[index] = nil
if client != nil {
client.Close(true)
}
}
func (this *GatewayLink) GetClient(clientId uint32) *GatewayClient {
this.clientsMutex.RLock()
defer this.clientsMutex.RUnlock()
var index = int(clientId - this.linkId)
if index >= this.maxClientNum || index < 0 {
return nil
}
return this.clients[index]
}
func (this *GatewayLink) SendDelClient(clientId uint32) {
this.conn.NewPackage(4).WriteUint32(clientId).Send()
}
func (this *GatewayLink) SendToBackend(msg []byte) error {
return this.conn.sendRaw(msg)
}
func (this *GatewayLink) Close(bySelf bool) {
if atomic.CompareAndSwapInt32(&this.closed, 0, 1) {
this.conn.Close()
for _, client := range this.clients {
if client != nil {
client.conn.Close()
}
}
// 如果是自己关闭的,需要从列表移除
if bySelf {
this.owner.delLink(this.backendId)
}
}
}
type GatewayClient struct {
owner *GatewayLink
id uint32
conn *Conn
sendChan chan []byte
closeChan chan int
closed int32
}
func newGatewayClient(owner *GatewayLink, id uint32, conn *Conn) *GatewayClient {
if owner.takeClientAddr {
var addr = conn.conn.RemoteAddr().String()
var addrMsg = conn.NewPackage(4 + 2 + len(addr))
addrMsg.WriteUint32(id).WriteUint8(uint8(len(addr))).WriteBytes([]byte(addr))
owner.SendToBackend(addrMsg.buff)
}
var this = &GatewayClient{
owner,
id,
conn,
make(chan []byte, 1000),
make(chan int, 2),
0,
}
go func() {
defer this.Close(false)
L:
for {
select {
case data := <-this.sendChan:
if conn.sendRaw(data) != nil {
break L
}
case <-this.closeChan:
break L
}
}
}()
return this
}
func (this *GatewayClient) Transport() {
defer this.Close(false)
var (
link = this.owner
front = link.owner
pack = front.pack
msg []byte
)
for {
msg = this.conn.ReadInto(msg)
if msg == nil {
break
}
setUint(msg, pack, len(msg)-pack)
setUint32(msg[pack:], this.id)
if front.counterOn {
atomic.AddUint64(&front.inPack, uint64(1))
atomic.AddUint64(&front.inByte, uint64(len(msg)))
}
link.SendToBackend(msg)
}
}
func (this *GatewayClient) Send(data []byte) {
select {
case this.sendChan <- data:
default:
this.Close(false)
}
}
func (this *GatewayClient) Close(byBackend bool) {
if atomic.CompareAndSwapInt32(&this.closed, 0, 1) {
// 如果不是后端关闭,需要从列表移除
if byBackend == false {
this.owner.DelClient(this.id)
this.owner.SendDelClient(this.id)
}
this.conn.Close()
this.closeChan <- 1
}
}