-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpubrel.go
104 lines (87 loc) · 2.49 KB
/
pubrel.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
package mq
import (
"fmt"
"io"
)
// NewPubRel returns control packet with type PUBREL
func NewPubRel() *PubRel {
p := &PubRel{}
p.fixed = bits(PUBREL | 1<<1)
return p
}
// A PubRel packet is the response to a Publish packets, depending on
// the fixed header it can be one of PUBACK, PUBREC, PUBREL or PUBCOMP
type PubRel struct {
fixed bits
packetID wuint16
reasonCode wuint8
reason wstring
UserProperties
}
func (p *PubRel) String() string {
return withReason(p, fmt.Sprintf("%s p%v %v bytes",
firstByte(p.fixed).String(),
p.packetID,
p.width(),
))
}
func (p *PubRel) dump(w io.Writer) {
fmt.Fprintf(w, "PacketID: %v\n", p.PacketID())
fmt.Fprintf(w, "ReasonString: %v\n", p.ReasonString())
fmt.Fprintf(w, "ReasonCode: %v\n", p.ReasonCode())
p.UserProperties.dump(w)
}
func (p *PubRel) SetPacketID(v uint16) { p.packetID = wuint16(v) }
func (p *PubRel) PacketID() uint16 { return uint16(p.packetID) }
func (p *PubRel) SetReasonCode(v ReasonCode) { p.reasonCode = wuint8(v) }
func (p *PubRel) ReasonCode() ReasonCode { return ReasonCode(p.reasonCode) }
func (p *PubRel) SetReasonString(v string) { p.reason = wstring(v) }
func (p *PubRel) ReasonString() string { return string(p.reason) }
func (p *PubRel) WriteTo(w io.Writer) (int64, error) {
b := make([]byte, p.fill(_LEN, 0))
p.fill(b, 0)
n, err := w.Write(b)
return int64(n), err
}
func (p *PubRel) width() int {
return p.fill(_LEN, 0)
}
func (p *PubRel) fill(b []byte, i int) int {
remainingLen := vbint(p.variableHeader(_LEN, 0))
i += p.fixed.fill(b, i) // firstByte header
i += remainingLen.fill(b, i) // remaining length
i += p.variableHeader(b, i)
return i
}
func (p *PubRel) variableHeader(b []byte, i int) int {
n := i
i += p.packetID.fill(b, i)
i += p.reasonCode.fillOpt(b, i)
propl := vbint(p.properties(_LEN, 0))
if propl > 0 {
i += propl.fill(b, i) // Properties len
i += p.properties(b, i) // Properties
}
return i - n
}
func (p *PubRel) properties(b []byte, i int) int {
n := i
i += p.reason.fillProp(b, i, ReasonString)
i += p.UserProperties.properties(b, i)
return i - n
}
func (p *PubRel) UnmarshalBinary(data []byte) error {
b := &buffer{data: data}
b.get(&p.packetID)
// no more data, see 3.4.2.1 PUBACK Reason Code
if len(data) > 2 {
b.get(&p.reasonCode)
b.getAny(p.propertyMap(), p.appendUserProperty)
}
return b.err
}
func (p *PubRel) propertyMap() map[Ident]func() wireType {
return map[Ident]func() wireType{
ReasonString: func() wireType { return &p.reason },
}
}