-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unsuback.go
117 lines (96 loc) · 2.58 KB
/
unsuback.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
package mq
import (
"fmt"
"io"
)
// UnsubAck and SubAck are exactly the same except for the fixed
// byte. Keep for now.
func NewUnsubAck() *UnsubAck {
return &UnsubAck{fixed: bits(UNSUBACK)}
}
type UnsubAck struct {
fixed bits
packetID wuint16
UserProperties
reasonString wstring
reasonCodes []uint8
}
func (p *UnsubAck) String() string {
return fmt.Sprintf("%s p%v %v bytes",
firstByte(p.fixed).String(),
p.packetID,
p.width(),
)
}
func (p *UnsubAck) dump(w io.Writer) {
fmt.Fprintf(w, "PacketID: %v\n", p.PacketID())
fmt.Fprintf(w, "ReasonString: %v\n", p.ReasonString())
fmt.Fprintf(w, "ReasonCodes: %v\n", p.ReasonCodes())
p.UserProperties.dump(w)
}
func (p *UnsubAck) SetPacketID(v uint16) { p.packetID = wuint16(v) }
func (p *UnsubAck) PacketID() uint16 { return uint16(p.packetID) }
func (p *UnsubAck) SetReasonString(v string) { p.reasonString = wstring(v) }
func (p *UnsubAck) ReasonString() string { return string(p.reasonString) }
func (p *UnsubAck) AddReasonCode(v ReasonCode) {
p.reasonCodes = append(p.reasonCodes, uint8(v))
}
func (p *UnsubAck) ReasonCodes() []uint8 { return p.reasonCodes }
func (p *UnsubAck) WriteTo(w io.Writer) (int64, error) {
b := make([]byte, p.width())
p.fill(b, 0)
n, err := w.Write(b)
return int64(n), err
}
func (p *UnsubAck) width() int {
return p.fill(_LEN, 0)
}
func (p *UnsubAck) fill(b []byte, i int) int {
remainingLen := vbint(
p.variableHeader(_LEN, 0) + p.payload(_LEN, 0),
)
i += p.fixed.fill(b, i) // firstByte header
i += remainingLen.fill(b, i) // remaining length
i += p.variableHeader(b, i)
i += p.payload(b, i)
return i
}
func (p *UnsubAck) variableHeader(b []byte, i int) int {
n := i
i += p.packetID.fill(b, i)
i += vbint(p.properties(_LEN, 0)).fill(b, i)
i += p.properties(b, i)
return i - n
}
func (p *UnsubAck) properties(b []byte, i int) int {
n := i
for id, v := range p.propertyMap() {
i += v().fillProp(b, i, id)
}
i += p.UserProperties.properties(b, i)
return i - n
}
func (p *UnsubAck) payload(b []byte, i int) int {
n := i
for j, _ := range p.reasonCodes {
i += wuint8(p.reasonCodes[j]).fill(b, i)
}
return i - n
}
func (p *UnsubAck) UnmarshalBinary(data []byte) error {
b := &buffer{data: data}
b.get(&p.packetID)
b.getAny(p.propertyMap(), p.appendUserProperty)
p.reasonCodes = make([]uint8, len(data)-b.i)
for i, _ := range p.reasonCodes {
var v wuint8
b.get(&v)
p.reasonCodes[i] = uint8(v)
}
return b.err
}
func (p *UnsubAck) propertyMap() map[Ident]func() wireType {
return map[Ident]func() wireType{
ReasonString: func() wireType { return &p.reasonString },
}
}