-
Notifications
You must be signed in to change notification settings - Fork 0
/
publishresponse.go
495 lines (411 loc) · 15.3 KB
/
publishresponse.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package mqtt
import (
"bytes"
"fmt"
"io"
"github.com/srishina/mqtt.go/internal/mqttutil"
"github.com/srishina/mqtt.go/internal/packettype"
"github.com/srishina/mqtt.go/internal/properties"
"github.com/srishina/mqtt.go/internal/reasoncode"
)
// PublishResponseProperties MQTT PUBACK, PUBREC, PUBREL, PUBCOMP properties
type PublishResponseProperties struct {
ReasonString string
UserProperty map[string]string
}
func (sp *PublishResponseProperties) length() uint32 {
propertyLen := uint32(0)
propertyLen += properties.EncodedSize.FromUTF8String(sp.ReasonString)
propertyLen += properties.EncodedSize.FromUTF8StringPair(sp.UserProperty)
return propertyLen
}
func (sp *PublishResponseProperties) encode(buf *bytes.Buffer, propertyLen uint32) error {
if err := properties.Encoder.FromUTF8String(
buf, properties.ReasonStringID, sp.ReasonString); err != nil {
return err
}
if err := properties.Encoder.FromUTF8StringPair(
buf, properties.UserPropertyID, sp.UserProperty); err != nil {
return err
}
return nil
}
func (sp *PublishResponseProperties) decode(r io.Reader, propertyLen uint32) error {
var id uint32
var err error
for err == nil && propertyLen > 0 {
id, _, err = mqttutil.DecodeVarUint32(r)
if err != nil {
return err
}
propertyLen -= mqttutil.EncodedVarUint32Size(id)
propID := properties.PropertyID(id)
switch propID {
case properties.ReasonStringID:
sp.ReasonString, err = properties.DecoderOnlyOnce.ToUTF8String(r, propID, sp.ReasonString)
propertyLen -= uint32(len(sp.ReasonString) + 2)
case properties.UserPropertyID:
if sp.UserProperty == nil {
sp.UserProperty = make(map[string]string)
}
key, value, err2 := properties.Decoder.ToUTF8StringPair(r)
if err2 != nil {
return err2
}
sp.UserProperty[key] = value
propertyLen -= uint32(len(key) + len(value) + 4)
default:
return fmt.Errorf("wrong property with identifier %d", id)
}
}
return err
}
// PubAckReasonCode MQTT reason code that indicates the result of PUBLISH operation
type PubAckReasonCode byte
const (
PubAckReasonCodeSuccess PubAckReasonCode = PubAckReasonCode(reasoncode.Success)
PubAckReasonCodeNoMatchingSubscribers PubAckReasonCode = PubAckReasonCode(reasoncode.NoMatchingSubscribers)
PubAckUnspecifiedError PubAckReasonCode = PubAckReasonCode(reasoncode.UnspecifiedError)
PubAckImplSpecificError PubAckReasonCode = PubAckReasonCode(reasoncode.ImplSpecificError)
PubAckImplNotAuthorized PubAckReasonCode = PubAckReasonCode(reasoncode.NotAuthorized)
PubAckTopicNameInvalid PubAckReasonCode = PubAckReasonCode(reasoncode.TopicNameInvalid)
PubAckPacketIdentifierInUse PubAckReasonCode = PubAckReasonCode(reasoncode.PacketIdentifierInUse)
PubAckQuotaExceeded PubAckReasonCode = PubAckReasonCode(reasoncode.QuotaExceeded)
PubAckPayloadFormatInvalid PubAckReasonCode = PubAckReasonCode(reasoncode.PayloadFormatInvalid)
)
var pubAckReasonCodeText = map[PubAckReasonCode]string{
PubAckReasonCodeSuccess: reasoncode.Success.Text(),
PubAckReasonCodeNoMatchingSubscribers: reasoncode.NoMatchingSubscribers.Text(),
PubAckUnspecifiedError: reasoncode.UnspecifiedError.Text(),
PubAckImplSpecificError: reasoncode.ImplSpecificError.Text(),
PubAckImplNotAuthorized: reasoncode.NotAuthorized.Text(),
PubAckTopicNameInvalid: reasoncode.TopicNameInvalid.Text(),
PubAckPacketIdentifierInUse: reasoncode.PacketIdentifierInUse.Text(),
PubAckQuotaExceeded: reasoncode.QuotaExceeded.Text(),
PubAckPayloadFormatInvalid: reasoncode.PayloadFormatInvalid.Text(),
}
var pubAckReasonCodeDesc = map[PubAckReasonCode]string{
PubAckReasonCodeSuccess: "The message is accepted. Publication of the QoS 1 message proceeds.",
PubAckReasonCodeNoMatchingSubscribers: `The message is accepted but there are no subscribers. This is sent only
by the Server. If the Server knows that there are no matching subscribers,
it MAY use this Reason Code instead of 0x00 (Success).`,
PubAckUnspecifiedError: `The receiver does not accept the publish but either does not want to reveal the reason,
or it does not match one of the other values.`,
PubAckImplSpecificError: "The PUBLISH is valid but the receiver is not willing to accept it. ",
PubAckImplNotAuthorized: "The PUBLISH is not authorized.",
PubAckTopicNameInvalid: "The Topic Name is not malformed, but is not accepted by this Client or Server.",
PubAckPacketIdentifierInUse: "The Packet Identifier is already in use. This might indicate a mismatch in the Session State between the Client and Server.",
PubAckQuotaExceeded: "An implementation or administrative imposed limit has been exceeded.",
PubAckPayloadFormatInvalid: "The payload format does not match the specified Payload Format Indicator.",
}
// Text returns a text for the MQTT reason code. Returns the empty
// string if the reason code is unknown.
func (code PubAckReasonCode) Text() string {
return pubAckReasonCodeText[code]
}
// Desc returns a description for the MQTT reason code. Returns the empty
// string if the reason code is unknown.
func (code PubAckReasonCode) Desc() string {
return pubAckReasonCodeDesc[code]
}
func encodePublishResponse(byte0 byte, id uint16, code byte, propEncoder publishResponsePropertyEncoder) (*bytes.Buffer, error) {
propertyLen := propEncoder.propertyLength()
// calculate the remaining length
remainingLength := uint32(2) // packet id
// The Reason Code or Property Length or both can be omitted
// if the Reason Code is 0x00 (Success) and there are no Properties, then both can be
// omitted and the remaining length is 2
// If the reason code is not 0x00 and there are no properties then the
// remaining length is 3
// If the reason code is 0x00 and there are properties then the
// remaining length is greather than 3
if propertyLen != 0 {
remainingLength += (1 + propertyLen + mqttutil.EncodedVarUint32Size(propertyLen))
} else if code != 0 {
remainingLength += uint32(1)
}
var packet bytes.Buffer
packet.Grow(int(1 + remainingLength + mqttutil.EncodedVarUint32Size(remainingLength)))
if err := mqttutil.EncodeByte(&packet, byte0); err != nil {
return nil, err
}
if err := mqttutil.EncodeVarUint32(&packet, remainingLength); err != nil {
return nil, err
}
if err := mqttutil.EncodeBigEndianUint16(&packet, id); err != nil {
return nil, err
}
if remainingLength > 2 {
// encode the reason code
if err := mqttutil.EncodeByte(&packet, code); err != nil {
return nil, err
}
if remainingLength > 3 {
if err := propEncoder.encodeProperties(&packet, propertyLen); err != nil {
return nil, err
}
}
}
return &packet, nil
}
func decodePublishResponse(r io.Reader, remainingLen uint32) (uint16, byte, *PublishResponseProperties, error) {
var props *PublishResponseProperties
var code byte
packetID, err := mqttutil.DecodeBigEndianUint16(r)
if err != nil {
return 0, 0, nil, err
}
if remainingLen > 2 {
code, err = mqttutil.DecodeByte(r)
if err != nil {
return 0, 0, nil, err
}
if remainingLen > 3 {
propertyLen, _, err := mqttutil.DecodeVarUint32(r)
if err != nil {
return 0, 0, nil, err
}
if propertyLen > 0 {
props = &PublishResponseProperties{}
err := props.decode(r, propertyLen)
if err != nil {
return 0, 0, nil, err
}
}
}
}
return packetID, code, props, nil
}
type publishResponsePropertyEncoder interface {
propertyLength() uint32
encodeProperties(buf *bytes.Buffer, propertyLen uint32) error
}
// PubAck MQTT PUBACK packet
type PubAck struct {
packetID uint16
ReasonCode PubAckReasonCode
Properties *PublishResponseProperties
}
func (pa *PubAck) propertyLength() uint32 {
if pa.Properties != nil {
return pa.Properties.length()
}
return 0
}
func (pa *PubAck) encodeProperties(buf *bytes.Buffer, propertyLen uint32) error {
if err := mqttutil.EncodeVarUint32(buf, propertyLen); err != nil {
return err
}
if pa.Properties != nil {
return pa.Properties.encode(buf, propertyLen)
}
return nil
}
func (pa *PubAck) encode(w io.Writer) error {
packet, err := encodePublishResponse(byte(packettype.PUBACK<<4), pa.packetID, byte(pa.ReasonCode), pa)
if err != nil {
return err
}
_, err = packet.WriteTo(w)
return err
}
func (pa *PubAck) decode(r io.Reader, remainingLen uint32) error {
id, code, props, err := decodePublishResponse(r, remainingLen)
if err != nil {
return err
}
pa.packetID = id
pa.ReasonCode = PubAckReasonCode(code)
pa.Properties = props
return nil
}
type PubRecReasonCode byte
const (
PubRecReasonCodeSuccess PubRecReasonCode = PubRecReasonCode(reasoncode.Success)
PubRecReasonCodeNoMatchingSubscribers PubRecReasonCode = PubRecReasonCode(reasoncode.NoMatchingSubscribers)
PubRecUnspecifiedError PubRecReasonCode = PubRecReasonCode(reasoncode.UnspecifiedError)
PubRecImplSpecificError PubRecReasonCode = PubRecReasonCode(reasoncode.ImplSpecificError)
PubRecImplNotAuthorized PubRecReasonCode = PubRecReasonCode(reasoncode.NotAuthorized)
PubRecTopicNameInvalid PubRecReasonCode = PubRecReasonCode(reasoncode.TopicNameInvalid)
PubRecPacketIdentifierInUse PubRecReasonCode = PubRecReasonCode(reasoncode.PacketIdentifierInUse)
PubRecQuotaExceeded PubRecReasonCode = PubRecReasonCode(reasoncode.QuotaExceeded)
PubRecPayloadFormatInvalid PubRecReasonCode = PubRecReasonCode(reasoncode.PayloadFormatInvalid)
)
// Text returns a text for the MQTT reason code. Returns the empty
// string if the reason code is unknown.
func (code PubRecReasonCode) Text() string {
return pubAckReasonCodeText[PubAckReasonCode(code)]
}
// Desc returns a description for the MQTT reason code. Returns the empty
// string if the reason code is unknown.
func (code PubRecReasonCode) Desc() string {
return pubAckReasonCodeDesc[PubAckReasonCode(code)]
}
// PubRec MQTT PUBACK packet
type PubRec struct {
packetID uint16
ReasonCode PubRecReasonCode
Properties *PublishResponseProperties
}
func (pr *PubRec) propertyLength() uint32 {
if pr.Properties != nil {
return pr.Properties.length()
}
return 0
}
func (pr *PubRec) encodeProperties(buf *bytes.Buffer, propertyLen uint32) error {
if err := mqttutil.EncodeVarUint32(buf, propertyLen); err != nil {
return err
}
if pr.Properties != nil {
return pr.Properties.encode(buf, propertyLen)
}
return nil
}
func (pr *PubRec) encode(w io.Writer) error {
packet, err := encodePublishResponse(byte(packettype.PUBREC<<4), pr.packetID, byte(pr.ReasonCode), pr)
if err != nil {
return err
}
_, err = packet.WriteTo(w)
return err
}
func (pr *PubRec) decode(r io.Reader, remainingLen uint32) error {
id, code, props, err := decodePublishResponse(r, remainingLen)
if err != nil {
return err
}
pr.packetID = id
pr.ReasonCode = PubRecReasonCode(code)
pr.Properties = props
return nil
}
// PubRelReasonCode MQTT reason code that indicates the result of PUBLISH operation
type PubRelReasonCode byte
const (
PubRelReasonCodeSuccess PubRelReasonCode = PubRelReasonCode(reasoncode.Success)
PubRelPacketIdentifierNotFound PubRelReasonCode = PubRelReasonCode(reasoncode.PacketIdentifierNotFound)
)
var pubRelReasonCodeText = map[PubRelReasonCode]string{
PubRelReasonCodeSuccess: reasoncode.Success.Text(),
PubRelPacketIdentifierNotFound: reasoncode.PacketIdentifierNotFound.Text(),
}
var pubRelReasonCodeDesc = map[PubRelReasonCode]string{
PubRelReasonCodeSuccess: "Message released.",
PubRelPacketIdentifierNotFound: `The Packet Identifier is not known.
This is not an error during recovery, but at other times indicates a mismatch between the Session State on the Client and Server.`,
}
// Text returns a text for the MQTT reason code. Returns the empty
// string if the reason code is unknown.
func (code PubRelReasonCode) Text() string {
return pubRelReasonCodeText[code]
}
// Desc returns a description for the MQTT reason code. Returns the empty
// string if the reason code is unknown.
func (code PubRelReasonCode) Desc() string {
return pubRelReasonCodeDesc[code]
}
// PubRel MQTT PUBREL packet
type PubRel struct {
packetID uint16
ReasonCode PubRelReasonCode
Properties *PublishResponseProperties
}
func (pr *PubRel) propertyLength() uint32 {
if pr.Properties != nil {
return pr.Properties.length()
}
return 0
}
func (pr *PubRel) encodeProperties(buf *bytes.Buffer, propertyLen uint32) error {
if err := mqttutil.EncodeVarUint32(buf, propertyLen); err != nil {
return err
}
if pr.Properties != nil {
return pr.Properties.encode(buf, propertyLen)
}
return nil
}
func (pr *PubRel) encode(w io.Writer) error {
const fixedHeader = byte(0x62) // 01100010
packet, err := encodePublishResponse(fixedHeader, pr.packetID, byte(pr.ReasonCode), pr)
if err != nil {
return err
}
_, err = packet.WriteTo(w)
return err
}
func (pr *PubRel) decode(r io.Reader, remainingLen uint32) error {
id, code, props, err := decodePublishResponse(r, remainingLen)
if err != nil {
return err
}
pr.packetID = id
pr.ReasonCode = PubRelReasonCode(code)
pr.Properties = props
return nil
}
// PubCompReasonCode MQTT reason code that indicates the result of PUBLISH operation
type PubCompReasonCode byte
const (
PubCompReasonCodeSuccess PubCompReasonCode = PubCompReasonCode(reasoncode.Success)
PubCompPacketIdentifierNotFound PubCompReasonCode = PubCompReasonCode(reasoncode.PacketIdentifierNotFound)
)
var pubCompReasonCodeText = map[PubCompReasonCode]string{
PubCompReasonCodeSuccess: reasoncode.Success.Text(),
PubCompPacketIdentifierNotFound: reasoncode.PacketIdentifierNotFound.Text(),
}
var pubCompReasonCodeDesc = map[PubCompReasonCode]string{
PubCompReasonCodeSuccess: "Packet Identifier released. Publication of QoS 2 message is complete.",
PubCompPacketIdentifierNotFound: `The Packet Identifier is not known.
This is not an error during recovery, but at other times indicates a mismatch between the Session State on the Client and Server.`,
}
// Text returns a text for the MQTT reason code. Returns the empty
// string if the reason code is unknown.
func (code PubCompReasonCode) Text() string {
return pubCompReasonCodeText[code]
}
// Desc returns a description for the MQTT reason code. Returns the empty
// string if the reason code is unknown.
func (code PubCompReasonCode) Desc() string {
return pubCompReasonCodeDesc[code]
}
// PubComp MQTT PUBCOMP packet
type PubComp struct {
packetID uint16
ReasonCode PubCompReasonCode
Properties *PublishResponseProperties
}
func (pc *PubComp) propertyLength() uint32 {
if pc.Properties != nil {
return pc.Properties.length()
}
return 0
}
func (pc *PubComp) encodeProperties(buf *bytes.Buffer, propertyLen uint32) error {
if err := mqttutil.EncodeVarUint32(buf, propertyLen); err != nil {
return err
}
if pc.Properties != nil {
return pc.Properties.encode(buf, propertyLen)
}
return nil
}
func (pc *PubComp) encode(w io.Writer) error {
packet, err := encodePublishResponse(byte(packettype.PUBCOMP<<4), pc.packetID, byte(pc.ReasonCode), pc)
if err != nil {
return err
}
_, err = packet.WriteTo(w)
return err
}
func (pc *PubComp) decode(r io.Reader, remainingLen uint32) error {
id, code, props, err := decodePublishResponse(r, remainingLen)
if err != nil {
return err
}
pc.packetID = id
pc.ReasonCode = PubCompReasonCode(code)
pc.Properties = props
return nil
}