forked from VolantMQ/volantmq
/
publish.go
298 lines (241 loc) · 6.93 KB
/
publish.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
// Copyright (c) 2014 The SurgeMQ Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package packet
// Publish A PUBLISH Control Packet is sent from a Client to a Server or from Server to a Client
// to transport an Application Message.
type Publish struct {
header
payload []byte
topic string
}
var _ Provider = (*Publish)(nil)
func newPublish() *Publish {
return &Publish{}
}
// Set topic/payload/qos/retained/bool
func (msg *Publish) Set(t string, p []byte, q QosType, r bool, d bool) error {
if !ValidTopic(t) {
return ErrInvalidTopic
}
if !q.IsValid() {
return ErrInvalidQoS
}
msg.mFlags &= ^maskPublishFlagQoS
msg.mFlags |= byte(q) << 1
msg.topic = t
msg.payload = p
if d {
msg.mFlags |= maskPublishFlagDup
} else {
msg.mFlags &= ^maskPublishFlagDup
}
if r {
msg.mFlags |= maskPublishFlagRetain
} else {
msg.mFlags &= ^maskPublishFlagRetain
}
return nil
}
// Dup returns the value specifying the duplicate delivery of a PUBLISH Control Packet.
// If the DUP flag is set to 0, it indicates that this is the first occasion that the
// Client or Server has attempted to send this MQTT PUBLISH Packet. If the DUP flag is
// set to 1, it indicates that this might be re-delivery of an earlier attempt to send
// the Packet.
func (msg *Publish) Dup() bool {
return (msg.mFlags & maskPublishFlagDup) != 0
}
// SetDup sets the value specifying the duplicate delivery of a PUBLISH Control Packet.
func (msg *Publish) SetDup(v bool) {
if v {
msg.mFlags |= maskPublishFlagDup // 0x8 // 00001000
} else {
msg.mFlags &= ^maskPublishFlagDup // 247 // 11110111
}
}
// Retain returns the value of the RETAIN flag. This flag is only used on the PUBLISH
// Packet. If the RETAIN flag is set to 1, in a PUBLISH Packet sent by a Client to a
// Server, the Server MUST store the Application Message and its QoS, so that it can be
// delivered to future subscribers whose subscriptions match its topic name.
func (msg *Publish) Retain() bool {
return (msg.mFlags & maskPublishFlagRetain) != 0
}
// SetRetain sets the value of the RETAIN flag.
func (msg *Publish) SetRetain(v bool) {
if v {
msg.mFlags |= maskPublishFlagRetain
} else {
msg.mFlags &= ^maskPublishFlagRetain
}
}
// QoS returns the field that indicates the level of assurance for delivery of an
// Application Message. The values are QoS0, QoS1 and QoS2.
func (msg *Publish) QoS() QosType {
return QosType((msg.mFlags & maskPublishFlagQoS) >> offsetPublishFlagQoS)
}
// SetQoS sets the field that indicates the level of assurance for delivery of an
// Application Message. The values are QoS0, QoS1 and QoS2.
// An error is returned if the value is not one of these.
func (msg *Publish) SetQoS(v QosType) error {
if !v.IsValid() {
return ErrInvalidQoS
}
msg.mFlags &= ^maskPublishFlagQoS
msg.mFlags |= byte(v) << 1
return nil
}
// Topic returns the the topic name that identifies the information channel to which
// payload data is published.
func (msg *Publish) Topic() string {
return msg.topic
}
// SetTopic sets the the topic name that identifies the information channel to which
// payload data is published. An error is returned if ValidTopic() is falbase.
func (msg *Publish) SetTopic(v string) error {
if !ValidTopic(v) {
return ErrInvalidTopic
}
msg.topic = v
return nil
}
// Payload returns the application message that's part of the PUBLISH message.
func (msg *Publish) Payload() []byte {
return msg.payload
}
// SetPayload sets the application message that's part of the PUBLISH message.
func (msg *Publish) SetPayload(v []byte) {
msg.payload = v
}
// SetPacketID sets the ID of the packet.
func (msg *Publish) SetPacketID(v IDType) {
msg.setPacketID(v)
}
func (msg *Publish) decodeMessage(src []byte) (int, error) {
var err error
var n int
var buf []byte
total := 0
if !msg.QoS().IsValid() {
var rejectCode ReasonCode
if msg.version == ProtocolV50 {
rejectCode = CodeProtocolError
} else {
rejectCode = CodeRefusedServerUnavailable
}
return total, rejectCode
}
// [MQTT-3.3.1-2]
if msg.QoS() == QoS0 && msg.Dup() {
var rejectCode ReasonCode
if msg.version == ProtocolV50 {
rejectCode = CodeProtocolError
} else {
rejectCode = CodeRefusedServerUnavailable
}
return total, rejectCode
}
buf, n, err = ReadLPBytes(src[total:])
total += n
if err != nil {
return total, err
}
if !ValidTopic(string(buf)) {
return total, ErrInvalidTopic
}
msg.topic = string(buf)
// The packet identifier field is only present in the PUBLISH packets where the
// QoS level is 1 or 2
if msg.QoS() != QoS0 {
total += msg.decodePacketID(src[total:])
}
if msg.version == ProtocolV50 {
msg.properties, n, err = decodeProperties(msg.Type(), buf[total:])
total += n
if err != nil {
return total, err
}
}
pLen := int(msg.remLen) - total
// check payload len is not malformed
if pLen < 0 {
var rejectCode ReasonCode
if msg.version == ProtocolV50 {
rejectCode = CodeMalformedPacket
} else {
rejectCode = CodeRefusedServerUnavailable
}
return total, rejectCode
}
// check payload is not malformed
if len(src[total:]) < pLen {
var rejectCode ReasonCode
if msg.version == ProtocolV50 {
rejectCode = CodeMalformedPacket
} else {
rejectCode = CodeRefusedServerUnavailable
}
return total, rejectCode
}
if pLen > 0 {
msg.payload = make([]byte, pLen)
copy(msg.payload, src[total:total+pLen])
total += pLen
}
return total, nil
}
func (msg *Publish) encodeMessage(dst []byte) (int, error) {
if !ValidTopic(msg.topic) {
return 0, ErrInvalidTopic
}
if !msg.QoS().IsValid() {
return 0, ErrInvalidQoS
}
if msg.QoS() == QoS0 && msg.Dup() {
return 0, ErrDupViolation
}
// [MQTT-2.3.1]
if msg.QoS() != QoS0 && len(msg.packetID) == 0 {
return 0, ErrPackedIDZero
}
var err error
var n int
total := 0
if n, err = WriteLPBytes(dst[total:], []byte(msg.topic)); err != nil {
return total, err
}
total += n
if msg.QoS() != QoS0 {
total += msg.encodePacketID(dst[total:])
}
// V5.0 [MQTT-3.1.2.11]
if msg.version == ProtocolV50 {
if n, err = encodeProperties(msg.properties, dst[total:]); err != nil {
return total + n, err
}
total += n
}
total += copy(dst[total:], msg.payload)
return total, nil
}
func (msg *Publish) size() int {
total := 2 + len(msg.topic) + len(msg.payload)
if msg.QoS() != 0 {
total += 2
}
// v5.0 [MQTT-3.1.2.11]
if msg.version == ProtocolV50 {
pLen, _ := encodeProperties(msg.properties, []byte{})
total += pLen
}
return total
}