/
client.go
436 lines (388 loc) · 9.1 KB
/
client.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package broker
import (
"context"
"fmt"
"net"
"sync"
"sync/atomic"
"time"
"github.com/eclipse/paho.mqtt.golang/packets"
uuid "github.com/satori/go.uuid"
"golang.org/x/net/websocket"
"github.com/covine/bifrost/broker/session"
"github.com/covine/bifrost/plugins/bridge"
)
func (b *Broker) serve(conn net.Conn, connType ConnType) {
/// review newServer() free() store() drop() online() offline() goroutines
/// store drop -> list map
if connType == ConnTypClient && atomic.LoadUint64(&b.info.ClientCount) >= b.config.Client.Connection.Max {
_ = conn.Close()
return
}
if connType == ConnTypClient {
atomic.AddUint64(&b.info.ClientCount, 1)
defer atomic.AddUint64(&b.info.ClientCount, ^uint64(0))
} else {
atomic.AddUint64(&b.info.ProxyCount, 1)
defer atomic.AddUint64(&b.info.ProxyCount, ^uint64(0))
}
c := newServer(b, conn, connType)
defer c.free()
defer c.close()
ctx, cancel := context.WithTimeout(context.TODO(), c.broker.connectWaitTime)
go c.waitConnect(ctx)
packet, err := packets.ReadPacket(c.connection)
if err != nil {
cancel()
return
}
connect, ok := packet.(*packets.ConnectPacket)
if !ok {
cancel()
return
}
if err := c.handleConnect(connect); err != nil {
cancel()
return
}
cancel()
c.store()
defer c.drop()
c.online()
defer c.offline()
c.workers.Add(4)
go c.onSend()
go c.beat()
go c.onReceivePubSub()
go c.receive()
c.workers.Wait()
}
type server struct {
// broker
broker *Broker
// tcp connection
connection net.Conn
// server property
id string
localIP string
localPort string
remoteIP string
remotePort string
peer bool
status ConnStatus
mu sync.RWMutex
// worker
workers sync.WaitGroup
ctx context.Context
cancel context.CancelFunc
// receive
keepalive uint16
recvChan chan packets.ControlPacket
recvChanLen int
// send
ackChan chan packets.ControlPacket
// send.publish
pubChan chan *packets.PublishPacket
pubChanLen int
pubMu sync.RWMutex
// mid
midStore *midStore
// connect
connectReceived bool
connected bool
withClientID bool
peerID string
peerConnID string
clientID string
cleanSession bool
will *packets.PublishPacket
username string
password []byte
// session
session *session.Session
// sub/pub map
subMap map[string]*subscription
pubMap map[string]*publish
}
func (s *server) free() {
fmt.Printf("free client: %s\n", s.clientID)
s.midStore.close()
s.midStore = nil
s.subMap = nil
s.pubMap = nil
}
func newServer(b *Broker, connection net.Conn, connType ConnType) *server {
/// review
recvChanLen := b.config.Receiver.Client.RecvChan
if connType != ConnTypClient {
recvChanLen = b.config.Receiver.Peer.RecvChan
}
ackChanLen := b.config.Sender.Client.AckChan
pubChanLen := b.config.Sender.Client.PubChan
if connType != ConnTypClient {
ackChanLen = b.config.Sender.Peer.AckChan
pubChanLen = b.config.Sender.Peer.PubChan
}
s := server{
broker: b,
connection: connection,
id: uuid.NewV4().String(),
status: ConnStatusConnected,
// recv
recvChanLen: recvChanLen,
recvChan: make(chan packets.ControlPacket, recvChanLen),
// send
ackChan: make(chan packets.ControlPacket, ackChanLen),
// send.publish
pubChanLen: pubChanLen,
pubChan: make(chan *packets.PublishPacket, pubChanLen),
// message id store
midStore: newMidStore(),
// connect
cleanSession: true,
withClientID: false,
clientID: "",
connected: false,
subMap: make(map[string]*subscription),
pubMap: make(map[string]*publish),
}
s.ctx, s.cancel = context.WithCancel(context.Background())
s.localIP, s.localPort, _ = net.SplitHostPort(s.connection.LocalAddr().String())
remoteAddr := s.connection.RemoteAddr()
network := remoteAddr.Network()
if network != "websocket" {
s.remoteIP, s.remotePort, _ = net.SplitHostPort(remoteAddr.String())
} else {
ws := s.connection.(*websocket.Conn)
s.remoteIP, s.remotePort, _ = net.SplitHostPort(ws.Request().RemoteAddr)
}
if connType != ConnTypClient {
s.peer = true
}
return &s
}
func (s *server) waitConnect(ctx context.Context) {
select {
case <-ctx.Done():
if !s.connectReceived {
fmt.Printf("no <connect> packet after %d secs, close server\n", s.broker.config.Client.WaitConnect)
s.close()
}
case <-s.ctx.Done():
}
}
func (s *server) store() {
/// 实现有序map
if !s.peer {
s.broker.clientsMu.Lock()
pre, ok := s.broker.clients[s.clientID]
s.broker.clients[s.clientID] = s
s.broker.clientsMu.Unlock()
if ok {
fmt.Printf("close previous client id: %s\n", pre.clientID)
pre.close()
}
} else {
s.broker.proxyMesMu.Lock()
pre, ok := s.broker.proxyMes[s.clientID]
s.broker.proxyMes[s.clientID] = s
s.broker.proxyMesMu.Unlock()
if ok {
fmt.Printf("close previous peer me id: %s\n", pre.clientID)
pre.close()
}
}
}
func (s *server) drop() {
if !s.peer {
s.broker.clientsMu.Lock()
if cli, ok := s.broker.clients[s.clientID]; ok {
if cli == s {
delete(s.broker.clients, s.clientID)
}
}
s.broker.clientsMu.Unlock()
} else {
s.broker.proxyMesMu.Lock()
if cli, ok := s.broker.proxyMes[s.clientID]; ok {
if cli == s {
delete(s.broker.proxyMes, s.clientID)
}
}
s.broker.proxyMesMu.Unlock()
}
}
func (s *server) online() {
if !s.peer {
s.broker.publishOnOffLine(s.clientID, s.remoteIP, s.username, true)
if s.broker.bridgeProvider != nil {
err := s.broker.bridgeProvider.Publish(&bridge.Elements{
ClientID: s.clientID,
Username: s.username,
Action: bridge.Connect,
Timestamp: time.Now().Unix(),
})
if err != nil {
fmt.Printf("bridge mq error: %v\n", err)
}
}
}
}
func (s *server) offline() {
for topic, sub := range s.subMap {
err := s.broker.topicCore.Unsubscribe([]byte(sub.topic), sub)
if err != nil {
fmt.Printf("unsubscribe error: %v server id: %s\n ", err, s.clientID)
}
if !s.peer {
s.broker.proxyUnSub(s.id, topic)
if s.broker.bridgeProvider != nil {
err := s.broker.bridgeProvider.Publish(&bridge.Elements{
ClientID: s.clientID,
Username: s.username,
Action: bridge.Unsubscribe,
Timestamp: time.Now().Unix(),
Topic: topic,
})
if err != nil {
fmt.Printf("bridge mq error: %v\n", err)
}
}
}
}
if !s.peer {
if s.cleanSession {
_ = s.broker.sessionStore.Remove(s.clientID, s.id)
s.session.Free()
}
if s.will != nil {
s.broker.publish(s.will)
}
s.broker.publishOnOffLine(s.clientID, s.remoteIP, s.username, false)
if s.broker.bridgeProvider != nil {
err := s.broker.bridgeProvider.Publish(&bridge.Elements{
ClientID: s.clientID,
Username: s.username,
Action: bridge.Disconnect,
Timestamp: time.Now().Unix(),
})
if err != nil {
fmt.Printf("bridge mq error: %v\n", err)
}
}
} else {
_ = s.broker.sessionStore.Remove(s.clientID, s.id)
s.session.Free()
}
}
func (s *server) close() {
s.mu.Lock()
defer s.mu.Unlock()
s.status = ConnStatusClosed
s.cancel()
_ = s.connection.Close()
}
func (s *server) sendPub(p *packets.PublishPacket) {
s.pubMu.Lock()
defer s.pubMu.Unlock()
switch p.Qos {
case 1:
// give space for retry
if len(s.pubChan) < (s.pubChanLen/7*6) && s.session.Inflight().SpaceLeft() {
p.MessageID = s.midStore.alloc()
select {
case <-s.ctx.Done():
case s.pubChan <- p:
fp := &flyPacket{
Packet: p,
Time: time.Now(),
}
s.session.Inflight().PushBack(p.MessageID, fp, false, false)
}
} else {
s.session.PushBackOT(p)
}
case 0:
if len(s.pubChan) < (s.pubChanLen / 7 * 1) {
select {
case <-s.ctx.Done():
case s.pubChan <- p:
}
} else {
// if congestion occurs
s.session.PushBackZ(p)
}
}
}
func (s *server) pubRetain(p *packets.PublishPacket, qos byte) {
s.mu.RLock()
defer s.mu.RUnlock()
if s.status != ConnStatusConnected {
return
}
pub := s.broker.newPublish()
pub.TopicName = p.TopicName
pub.Payload = p.Payload
pub.Qos = qos
pub.Retain = true
s.sendPub(pub)
}
func (s *server) pub(p *packets.PublishPacket, qos byte) {
s.mu.RLock()
defer s.mu.RUnlock()
if s.status != ConnStatusConnected {
return
}
pub := s.broker.newPublish()
pub.TopicName = p.TopicName
pub.Payload = p.Payload
pub.Qos = qos
s.sendPub(pub)
}
func (s *server) sharePub(p *packets.PublishPacket, qos byte, group string) {
s.mu.RLock()
defer s.mu.RUnlock()
if s.status != ConnStatusConnected {
return
}
pub := s.broker.newPublish()
pub.TopicName = p.TopicName
pub.Payload = p.Payload
pub.Qos = qos
if s.peer {
if p.Retain {
pub.Retain = true
}
pub.TopicName = "$share/" + group + "/" + pub.TopicName
}
s.sendPub(pub)
}
func (s *server) ackPub(packet *packets.PublishPacket) {
s.mu.RLock()
defer s.mu.RUnlock()
if s.status != ConnStatusConnected {
return
}
switch packet.Qos {
case 2:
case 1:
pa := s.broker.newPuback()
pa.MessageID = packet.MessageID
select {
case <-s.ctx.Done():
case s.ackChan <- pa:
}
case 0:
}
}
func (s *server) ack(p packets.ControlPacket) {
s.mu.RLock()
defer s.mu.RUnlock()
if s.status != ConnStatusConnected {
return
}
select {
case <-s.ctx.Done():
case s.ackChan <- p:
}
}