/
client.go
328 lines (294 loc) · 9.17 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
package client
import (
"context"
"errors"
"github.com/BAN1ce/Tree/proto"
"github.com/BAN1ce/skyTree/inner/broker/client/topic"
"github.com/BAN1ce/skyTree/inner/facade"
"github.com/BAN1ce/skyTree/logger"
"github.com/BAN1ce/skyTree/pkg"
"github.com/BAN1ce/skyTree/pkg/broker"
"github.com/BAN1ce/skyTree/pkg/errs"
"github.com/BAN1ce/skyTree/pkg/packet"
"github.com/BAN1ce/skyTree/pkg/pool"
"github.com/BAN1ce/skyTree/pkg/retry"
"github.com/BAN1ce/skyTree/pkg/state"
"github.com/BAN1ce/skyTree/pkg/util"
"github.com/eclipse/paho.golang/packets"
"go.uber.org/zap"
"net"
"strings"
"sync"
"time"
)
type PacketIDFactory interface {
Generate() uint16
}
type Config struct {
WindowSize int
ReadStoreTimeout time.Duration
}
type Handler interface {
HandlePacket(*Client, *packets.ControlPacket)
}
type Client struct {
ID string `json:"id"`
connectProperties *broker.SessionConnectProperties
ctx context.Context
mux sync.RWMutex
conn net.Conn
handler Handler
state state.State
options *Options
packetIDFactory PacketIDFactory
publishBucket *util.Bucket
messages chan pkg.Message
topics *topic.Topics
identifierIDTopic map[uint16]string
QoS2 *HandleQoS2
topicAlias map[uint16]string
subIdentifier map[string]int
willFlag bool
keepAlive time.Duration
}
func NewClient(conn net.Conn, option ...Option) *Client {
var (
c = &Client{
conn: conn,
options: new(Options),
identifierIDTopic: map[uint16]string{},
topicAlias: map[uint16]string{},
}
)
for _, o := range option {
o(c.options)
}
c.packetIDFactory = util.NewPacketIDFactory()
c.messages = make(chan pkg.Message, c.options.cfg.WindowSize)
c.publishBucket = util.NewBucket(c.options.cfg.WindowSize)
c.QoS2 = NewHandleQoS2()
return c
}
func (c *Client) Run(ctx context.Context, handler Handler) {
c.ctx = ctx
c.handler = handler
var (
controlPacket *packets.ControlPacket
err error
)
for {
controlPacket, err = packets.ReadPacket(c.conn)
if err != nil {
logger.Logger.Info("read controlPacket error = ", zap.Error(err), zap.String("client", c.MetaString()))
c.afterClose()
return
}
handler.HandlePacket(c, controlPacket)
}
}
func (c *Client) HandleSub(subscribe *packets.Subscribe) map[string]byte {
c.mux.Lock()
defer c.mux.Unlock()
var (
topics = subscribe.Subscriptions
failed = map[string]byte{}
subIdentifier int
)
// store topic's subIdentifier
if tmp := subscribe.Properties.SubscriptionIdentifier; tmp != nil {
subIdentifier = *tmp
for _, t := range topics {
c.subIdentifier[t.Topic] = subIdentifier
}
}
// create topic instance
for _, t := range topics {
c.topics.CreateTopic(t.Topic, &proto.SubOption{
QoS: int32(t.QoS),
NoLocal: t.NoLocal,
RetainAsPublished: t.RetainAsPublished,
})
failed[t.Topic] = t.QoS
}
return failed
}
func (c *Client) HandleUnSub(topicName string) {
c.mux.Lock()
defer c.mux.Unlock()
c.options.session.DeleteSubTopic(topicName)
c.topics.DeleteTopic(topicName)
}
func (c *Client) HandlePubAck(pubAck *packets.Puback) {
topicName := c.identifierIDTopic[pubAck.PacketID]
if len(topicName) == 0 {
logger.Logger.Warn("pubAck packetID not found store", zap.String("client", c.MetaString()), zap.Uint16("packetID", pubAck.PacketID))
return
}
c.topics.HandlePublishAck(topicName, pubAck)
}
func (c *Client) HandlePubRec(pubRec *packets.Pubrec) {
topicName := c.identifierIDTopic[pubRec.PacketID]
if len(topicName) == 0 {
logger.Logger.Warn("pubRec packetID not found store", zap.String("client", c.MetaString()), zap.Uint16("packetID", pubRec.PacketID))
return
}
c.topics.HandlePublishRec(topicName, pubRec)
}
func (c *Client) HandlePubComp(pubRel *packets.Pubcomp) {
topicName := c.identifierIDTopic[pubRel.PacketID]
if len(topicName) == 0 {
logger.Logger.Warn("pubComp packetID not found store", zap.String("client", c.MetaString()), zap.Uint16("packetID", pubRel.PacketID))
return
}
c.topics.HandelPublishComp(topicName, pubRel)
}
func (c *Client) Close() error {
return c.conn.Close()
}
func (c *Client) afterClose() {
logger.Logger.Info("client close", zap.String("clientID", c.ID))
defer func() {
c.options.notifyClose.NotifyClientClose(c)
}()
if err := c.conn.Close(); err != nil {
logger.Logger.Info("close conn error", zap.Error(err), zap.String("client", c.MetaString()))
}
if err := c.topics.Close(); err != nil {
logger.Logger.Warn("close topics error", zap.Error(err), zap.String("client", c.MetaString()))
}
if c.options.session != nil {
willMessage, err := c.options.session.GetWillMessage()
if err != nil {
if !errors.Is(err, errs.ErrSessionWillMessageNotFound) {
logger.Logger.Error("get will message error", zap.Error(err), zap.String("client", c.MetaString()))
}
return
}
if willMessage.Property.WillDelayInterval == 0 {
c.options.notifyClose.NotifyWillMessage(willMessage)
return
}
// create a delay task to publish will message
if err := facade.GetWillDelay().Create(&retry.Task{
MaxTimes: 1,
MaxTime: 0,
IntervalTime: time.Duration(willMessage.Property.WillDelayInterval) * time.Second,
Key: willMessage.DelayTaskID,
Data: willMessage,
Job: func(task *retry.Task) {
if m, ok := task.Data.(*broker.WillMessage); ok {
logger.Logger.Debug("will message delay task", zap.String("client", c.MetaString()), zap.String("delayTaskID", task.Key))
c.options.notifyClose.NotifyWillMessage(m)
} else {
logger.Logger.Error("will message type error", zap.String("client", c.MetaString()))
}
},
TimeoutJob: nil,
}); err != nil {
logger.Logger.Error("create will delay task error", zap.Error(err), zap.String("client", c.MetaString()))
return
}
logger.Logger.Debug("create will delay task success", zap.String("client", c.MetaString()),
zap.Int64("delay time", willMessage.Property.WillDelayInterval), zap.String("delayTaskID", willMessage.DelayTaskID))
}
}
func (c *Client) SetID(id string) {
c.mux.Lock()
defer c.mux.Unlock()
c.ID = id
}
func (c *Client) WritePacket(packet packets.Packet) {
if packet == nil {
return
}
c.mux.Lock()
defer c.mux.Unlock()
logger.Logger.Debug("write packet", zap.String("client", c.MetaString()), zap.Any("packet", packet))
c.writePacket(packet)
}
func (c *Client) Write(data []byte) error {
// TODO: check size
_, err := c.conn.Write(data)
return err
}
func (c *Client) writePacket(packet packets.Packet) {
var (
buf = pool.ByteBufferPool.Get()
prepareWriteSize int64
err error
topicName string
)
defer pool.ByteBufferPool.Put(buf)
// publishAck, subscribeAck, unsubscribeAck should use the same packetID as the original packet
switch p := packet.(type) {
case *packets.Publish:
topicName = p.Topic
// generate new packetID and store
p.PacketID = c.packetIDFactory.Generate()
c.identifierIDTopic[p.PacketID] = p.Topic
logger.Logger.Debug("publish to client", zap.Uint16("packetID", p.PacketID), zap.String("client", c.MetaString()), zap.String("store", p.Topic))
// append subscriptionIdentifier
if subIdentifier, ok := c.subIdentifier[p.Topic]; ok {
p.Properties.SubscriptionIdentifier = &subIdentifier
}
}
if prepareWriteSize, err = packet.WriteTo(buf); err != nil {
logger.Logger.Info("write packet error", zap.Error(err), zap.String("client", c.MetaString()))
// TODO: check maximum packet size should close client ?
}
// TODO: check maximum packet size
if _, err = c.conn.Write(buf.Bytes()); err != nil {
logger.Logger.Info("write packet error", zap.Error(err), zap.String("client", c.MetaString()), zap.Int64("prepareWriteSize", prepareWriteSize), zap.String("topicName", topicName))
// TODO: check maximum packet size should close client ?
}
}
func (c *Client) SetMeta(session broker.Session, keepAlive time.Duration) error {
c.mux.Lock()
c.options.session = session
c.topics = topic.NewTopicWithSession(c.ctx, c.options.session, topic.WithWriter(c))
c.keepAlive = keepAlive
c.mux.Unlock()
return nil
}
func (c *Client) GetSession() broker.Session {
c.mux.RLock()
defer c.mux.RUnlock()
return c.options.session
}
func (c *Client) SetWill(message *broker.WillMessage) error {
c.willFlag = true
return c.options.session.SetWillMessage(message)
}
func (c *Client) SetTopicAlias(topic string, alias uint16) {
c.mux.Lock()
c.topicAlias[alias] = topic
c.mux.Unlock()
}
func (c *Client) SetConnectProperties(properties *broker.SessionConnectProperties) {
c.mux.Lock()
c.connectProperties = properties
c.options.session.SetConnectProperties(properties)
c.mux.Unlock()
}
func (c *Client) GetID() string {
return c.ID
}
func (c *Client) MetaString() string {
var (
s strings.Builder
)
s.WriteString("clientID: ")
s.WriteString(c.ID)
s.WriteString(" ")
s.WriteString("remoteAddr: ")
s.WriteString(c.conn.RemoteAddr().String())
return s.String()
}
func (c *Client) GetTopicAlias(u uint16) string {
c.mux.RLock()
defer c.mux.RUnlock()
return c.topicAlias[u]
}
func (c *Client) Publish(topic string, message *packet.PublishMessage) error {
return c.topics.Publish(topic, message)
}