-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpublish.go
197 lines (177 loc) · 4.74 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
// Copyright 2019 The mqtt-go authors.
//
// 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 mqtt
import (
"context"
"errors"
)
type publishFlag byte
const (
publishFlagRetain publishFlag = 0x01
publishFlagQoS0 publishFlag = 0x00
publishFlagQoS1 publishFlag = 0x02
publishFlagQoS2 publishFlag = 0x04
publishFlagQoSMask publishFlag = 0x06
publishFlagDup publishFlag = 0x08
)
// ErrPayloadLenExceeded means the payload length is too large.
var ErrPayloadLenExceeded = errors.New("payload length exceeded")
// ErrInvalidQoS means the QoS value is not allowed.
var ErrInvalidQoS = errors.New("invalid QoS")
type pktPublish struct {
Message *Message
}
func (p *pktPublish) Parse(flag byte, contents []byte) (*pktPublish, error) {
p.Message = &Message{
Dup: (publishFlag(flag) & publishFlagDup) != 0,
Retain: (publishFlag(flag) & publishFlagRetain) != 0,
}
switch publishFlag(flag) & publishFlagQoSMask {
case publishFlagQoS0:
p.Message.QoS = QoS0
case publishFlagQoS1:
p.Message.QoS = QoS1
case publishFlagQoS2:
p.Message.QoS = QoS2
default:
return nil, wrapErrorf(
ErrInvalidPacket, "parsing PUBLISH: QoS %d", int(publishFlag(flag)&publishFlagQoSMask),
)
}
var n, nID int
var err error
n, p.Message.Topic, err = unpackString(contents)
if err != nil {
return nil, err
}
if p.Message.QoS != QoS0 {
if len(contents)-n < 2 {
return nil, wrapError(ErrInvalidPacketLength, "parsing PUBLISH")
}
nID, p.Message.ID = unpackUint16(contents[n:])
}
p.Message.Payload = contents[n+nID:]
return p, nil
}
func (p *pktPublish) Pack() []byte {
pktHeader := packetPublish.b()
if p.Message.Retain {
pktHeader |= byte(publishFlagRetain)
}
switch p.Message.QoS {
case QoS0:
pktHeader |= byte(publishFlagQoS0)
case QoS1:
pktHeader |= byte(publishFlagQoS1)
case QoS2:
pktHeader |= byte(publishFlagQoS2)
default:
panic("invalid QoS")
}
if p.Message.Dup {
pktHeader |= byte(publishFlagDup)
}
header := make([]byte, 0, packetBufferCap)
header = appendString(header, p.Message.Topic)
if p.Message.QoS != QoS0 {
header = appendUint16(header, p.Message.ID)
}
return pack(
pktHeader,
header,
p.Message.Payload,
)
}
// ValidateMessage validates given message.
func (c *BaseClient) ValidateMessage(message *Message) error {
if c.MaxPayloadLen != 0 && len(message.Payload) >= c.MaxPayloadLen {
return wrapErrorf(ErrPayloadLenExceeded, "payload length %d", len(message.Payload))
}
if message.QoS > QoS2 {
return wrapErrorf(ErrInvalidQoS, "QoS%d", int(message.QoS))
}
return nil
}
// Publish a message to the broker.
// ID field of the message is filled if zero.
func (c *BaseClient) Publish(ctx context.Context, message *Message) error {
if err := c.ValidateMessage(message); err != nil {
return err
}
c.muConnecting.RLock()
defer c.muConnecting.RUnlock()
if message.ID == 0 {
message.ID = c.newID()
}
var chPubAck chan *pktPubAck
var chPubRec chan *pktPubRec
var chPubComp chan *pktPubComp
switch message.QoS {
case QoS1:
chPubAck = make(chan *pktPubAck, 1)
c.sig.mu.Lock()
if c.sig.chPubAck == nil {
c.sig.chPubAck = make(map[uint16]chan *pktPubAck)
}
c.sig.chPubAck[message.ID] = chPubAck
c.sig.mu.Unlock()
case QoS2:
chPubRec = make(chan *pktPubRec, 1)
chPubComp = make(chan *pktPubComp, 1)
c.sig.mu.Lock()
if c.sig.chPubRec == nil {
c.sig.chPubRec = make(map[uint16]chan *pktPubRec)
}
c.sig.chPubRec[message.ID] = chPubRec
if c.sig.chPubComp == nil {
c.sig.chPubComp = make(map[uint16]chan *pktPubComp)
}
c.sig.chPubComp[message.ID] = chPubComp
c.sig.mu.Unlock()
}
pkt := (&pktPublish{Message: message}).Pack()
if err := c.write(pkt); err != nil {
return wrapError(err, "sending PUBLISH")
}
switch message.QoS {
case QoS1:
select {
case <-c.connClosed:
return ErrClosedTransport
case <-ctx.Done():
return ctx.Err()
case <-chPubAck:
}
case QoS2:
select {
case <-c.connClosed:
return ErrClosedTransport
case <-ctx.Done():
return ctx.Err()
case <-chPubRec:
}
pktPubRel := (&pktPubRel{ID: message.ID}).Pack()
if err := c.write(pktPubRel); err != nil {
return wrapError(err, "sending PUBREL")
}
select {
case <-c.connClosed:
return ErrClosedTransport
case <-ctx.Done():
return ctx.Err()
case <-chPubComp:
}
}
return nil
}