-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connack.go
260 lines (217 loc) · 9.32 KB
/
connack.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
package mq
import (
"bytes"
"fmt"
"io"
)
func NewConnAck() *ConnAck {
return &ConnAck{
fixed: bits(CONNACK),
}
}
type ConnAck struct {
fixed bits
flags bits // sessionPresent as 7-1 are reserved
reasonCode wuint8
// properties
sessionExpiryInterval wuint32
receiveMax wuint16
maxQoS wuint8 // 0 or 1, 2
retainAvailable wbool
maxPacketSize wuint32
assignedClientID wstring
topicAliasMax wuint16
reasonString wstring
UserProperties
wildcardSubAvailable wbool
subIdentifiersAvailable wbool
sharedSubAvailable wbool
serverKeepAlive wuint16
responseInformation wstring
serverReference wstring
authMethod wstring
authData bindata
}
func (p *ConnAck) HasFlag(v byte) bool { return p.flags.Has(v) }
func (p *ConnAck) SetSessionPresent(v bool) { p.flags.toggle(1, true) }
func (p *ConnAck) SessionPresent() bool { return p.flags.Has(1) }
func (p *ConnAck) SetSessionExpiryInterval(v uint32) { p.sessionExpiryInterval = wuint32(v) }
func (p *ConnAck) SessionExpiryInterval() uint32 { return uint32(p.sessionExpiryInterval) }
func (p *ConnAck) SetReceiveMax(v uint16) { p.receiveMax = wuint16(v) }
func (p *ConnAck) ReceiveMax() uint16 { return uint16(p.receiveMax) }
func (p *ConnAck) SetMaxQoS(v uint8) { p.maxQoS = wuint8(v) }
func (p *ConnAck) MaxQoS() uint8 { return uint8(p.maxQoS) }
func (p *ConnAck) SetRetainAvailable(v bool) { p.retainAvailable = wbool(v) }
func (p *ConnAck) RetainAvailable() bool { return bool(p.retainAvailable) }
func (p *ConnAck) SetMaxPacketSize(v uint32) { p.maxPacketSize = wuint32(v) }
func (p *ConnAck) MaxPacketSize() uint32 { return uint32(p.maxPacketSize) }
func (p *ConnAck) SetAssignedClientID(v string) { p.assignedClientID = wstring(v) }
func (p *ConnAck) AssignedClientID() string { return string(p.assignedClientID) }
func (p *ConnAck) SetTopicAliasMax(v uint16) { p.topicAliasMax = wuint16(v) }
func (p *ConnAck) TopicAliasMax() uint16 { return uint16(p.topicAliasMax) }
func (p *ConnAck) SetReasonCode(v ReasonCode) { p.reasonCode = wuint8(v) }
func (p *ConnAck) ReasonCode() ReasonCode { return ReasonCode(p.reasonCode) }
func (p *ConnAck) SetReasonString(v string) { p.reasonString = wstring(v) }
func (p *ConnAck) ReasonString() string { return string(p.reasonString) }
func (p *ConnAck) SetWildcardSubAvailable(v bool) { p.wildcardSubAvailable = wbool(v) }
func (p *ConnAck) WildcardSubAvailable() bool { return bool(p.wildcardSubAvailable) }
func (p *ConnAck) SetSubIdentifiersAvailable(v bool) { p.subIdentifiersAvailable = wbool(v) }
func (p *ConnAck) SubIdentifiersAvailable() bool { return bool(p.subIdentifiersAvailable) }
func (p *ConnAck) SetSharedSubAvailable(v bool) { p.sharedSubAvailable = wbool(v) }
func (p *ConnAck) SharedSubAvailable() bool { return bool(p.sharedSubAvailable) }
func (p *ConnAck) SetServerKeepAlive(v uint16) { p.serverKeepAlive = wuint16(v) }
func (p *ConnAck) ServerKeepAlive() uint16 { return uint16(p.serverKeepAlive) }
func (p *ConnAck) SetResponseInformation(v string) { p.responseInformation = wstring(v) }
func (p *ConnAck) ResponseInformation() string { return string(p.responseInformation) }
func (p *ConnAck) SetServerReference(v string) { p.serverReference = wstring(v) }
func (p *ConnAck) ServerReference() string { return string(p.serverReference) }
func (p *ConnAck) SetAuthMethod(v string) { p.authMethod = wstring(v) }
func (p *ConnAck) AuthMethod() string { return string(p.authMethod) }
func (p *ConnAck) SetAuthData(v []byte) { p.authData = bindata(v) }
func (p *ConnAck) AuthData() []byte { return []byte(p.authData) }
// end settings
// ----------------------------------------
func (p *ConnAck) String() string {
return withReason(p, fmt.Sprintf("%s %s %s %v bytes",
firstByte(p.fixed).String(),
connAckFlags(p.flags),
p.assignedClientID,
p.width(),
))
}
func withReason(p HasReason, v string) string {
if code := p.ReasonCode(); code >= 0x80 {
if p, ok := p.(interface{ ReasonString() string }); ok {
if r := p.ReasonString(); r != "" {
return fmt.Sprintf("%s %s! %s", v, code.String(), r)
}
}
return fmt.Sprintf("%s %s!", v, code.String())
}
return v
}
func (p *ConnAck) dump(w io.Writer) {
fmt.Fprintf(w, "AssignedClientID: %q\n", p.AssignedClientID())
fmt.Fprintf(w, "AuthData: %q\n", string(p.AuthData()))
fmt.Fprintf(w, "AuthMethod: %q\n", p.AuthMethod())
fmt.Fprintf(w, "MaxPacketSize: %v\n", p.MaxPacketSize())
fmt.Fprintf(w, "MaxQoS: %v\n", p.MaxQoS())
fmt.Fprintf(w, "ReasonCode: %v\n", p.ReasonCode())
fmt.Fprintf(w, "ReasonString: %q\n", p.ReasonString())
fmt.Fprintf(w, "ReceiveMax: %v\n", p.ReceiveMax())
fmt.Fprintf(w, "ResponseInformation: %q\n", p.ResponseInformation())
fmt.Fprintf(w, "RetainAvailable: %v\n", p.RetainAvailable())
fmt.Fprintf(w, "ServerKeepAlive: %v\n", p.ServerKeepAlive())
fmt.Fprintf(w, "ServerReference: %q\n", p.ServerReference())
fmt.Fprintf(w, "SessionExpiryInterval: %v\n", p.SessionExpiryInterval())
fmt.Fprintf(w, "SessionPresent: %v\n", p.SessionPresent())
fmt.Fprintf(w, "SharedSubAvailable: %v\n", p.SharedSubAvailable())
fmt.Fprintf(w, "SubIdentifiersAvailable: %v\n", p.SubIdentifiersAvailable())
fmt.Fprintf(w, "TopicAliasMax: %v\n", p.TopicAliasMax())
fmt.Fprintf(w, "WildcardSubAvailable: %v\n", p.WildcardSubAvailable())
p.UserProperties.dump(w)
}
// ---------------------------------------- protocol
// WriteTo writes this connect control packet in wire format to the
// given writer.
func (p *ConnAck) WriteTo(w io.Writer) (int64, error) {
// allocate full size of entire packet
b := make([]byte, p.fill(_LEN, 0))
p.fill(b, 0)
n, err := w.Write(b)
return int64(n), err
}
func (p *ConnAck) fill(b []byte, i int) int {
i += p.fixed.fill(b, i) // firstByte header
i += vbint(p.variableHeader(_LEN, 0)).fill(b, i) // remaining length
i += p.variableHeader(b, i) // variable header
return i
}
func (p *ConnAck) variableHeader(b []byte, i int) int {
n := i
i += p.flags.fill(b, i) // acknowledge flags
i += p.reasonCode.fill(b, i)
i += vbint(p.properties(_LEN, 0)).fill(b, i) // Properties len
i += p.properties(b, i)
return i - n
}
func (p *ConnAck) properties(b []byte, i int) int {
n := i
i += p.receiveMax.fillProp(b, i, ReceiveMax)
i += p.sessionExpiryInterval.fillProp(b, i, SessionExpiryInterval)
i += p.maxQoS.fillProp(b, i, MaxQoS)
i += p.retainAvailable.fillProp(b, i, RetainAvailable)
i += p.maxPacketSize.fillProp(b, i, MaxPacketSize)
i += p.assignedClientID.fillProp(b, i, AssignedClientID)
i += p.topicAliasMax.fillProp(b, i, TopicAliasMax)
i += p.reasonString.fillProp(b, i, ReasonString)
i += p.wildcardSubAvailable.fillProp(b, i, WildcardSubAvailable)
i += p.subIdentifiersAvailable.fillProp(b, i, SubIDsAvailable)
i += p.sharedSubAvailable.fillProp(b, i, SharedSubAvailable)
i += p.serverKeepAlive.fillProp(b, i, ServerKeepAlive)
i += p.responseInformation.fillProp(b, i, ResponseInformation)
i += p.serverReference.fillProp(b, i, ServerReference)
i += p.authMethod.fillProp(b, i, AuthMethod)
i += p.authData.fillProp(b, i, AuthData)
i += p.UserProperties.properties(b, i)
return i - n
}
func (p *ConnAck) UnmarshalBinary(data []byte) error {
b := &buffer{data: data}
b.get(&p.flags)
b.get(&p.reasonCode)
b.getAny(p.propertyMap(), p.appendUserProperty)
return b.err
}
func (p *ConnAck) propertyMap() map[Ident]func() wireType {
return map[Ident]func() wireType{
ReceiveMax: func() wireType { return &p.receiveMax },
SessionExpiryInterval: func() wireType { return &p.sessionExpiryInterval },
MaxQoS: func() wireType { return &p.maxQoS },
RetainAvailable: func() wireType { return &p.retainAvailable },
MaxPacketSize: func() wireType { return &p.maxPacketSize },
AssignedClientID: func() wireType { return &p.assignedClientID },
TopicAliasMax: func() wireType { return &p.topicAliasMax },
ReasonString: func() wireType { return &p.reasonString },
WildcardSubAvailable: func() wireType { return &p.wildcardSubAvailable },
SubIDsAvailable: func() wireType { return &p.subIdentifiersAvailable },
SharedSubAvailable: func() wireType { return &p.sharedSubAvailable },
ServerKeepAlive: func() wireType { return &p.serverKeepAlive },
ResponseInformation: func() wireType { return &p.responseInformation },
ServerReference: func() wireType { return &p.serverReference },
AuthMethod: func() wireType { return &p.authMethod },
AuthData: func() wireType { return &p.authData },
}
}
func (p *ConnAck) width() int {
return p.fill(_LEN, 0)
}
// ----------------------------------------
type connAckFlags byte
// String returns flags represented with a letter.
// Improper flags are marked with '!' and unset are marked with '-'.
//
// SessionPresent s
// Reserved !
func (c connAckFlags) String() string {
flags := bytes.Repeat([]byte("-"), 8)
mark := func(i int, flag byte, v byte) {
if !bits(c).Has(flag) {
return
}
flags[i] = v
}
mark(0, 1<<7, '!')
mark(1, 1<<6, '!')
mark(2, 1<<5, '!')
mark(3, 1<<4, '!')
mark(4, 1<<3, '!')
mark(5, 1<<2, '!')
mark(6, 1<<1, '!')
mark(7, 1<<0, 's')
return string(flags) // + fmt.Sprintf(" %08b", c)
}
// ----------------------------------------
const (
SessionPresent uint8 = 1
)