-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisconnect.go
77 lines (63 loc) · 1.65 KB
/
disconnect.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
package mq
import (
"fmt"
"io"
)
// NewDisconnect returns a disconnect packet with reason code
// NormalDisconnect.
func NewDisconnect() *Disconnect {
return &Disconnect{fixed: bits(DISCONNECT)}
}
type Disconnect struct {
fixed bits
reasonCode wuint8
UserProperties
}
func (p *Disconnect) SetReasonCode(v ReasonCode) { p.reasonCode = wuint8(v) }
func (p *Disconnect) ReasonCode() ReasonCode { return ReasonCode(p.reasonCode) }
func (p *Disconnect) String() string {
return withReason(p, fmt.Sprintf("%s %v bytes",
firstByte(p.fixed).String(),
p.width(),
))
}
func (p *Disconnect) dump(w io.Writer) {
fmt.Fprintf(w, "ReasonCode: %v\n", p.ReasonCode())
p.UserProperties.dump(w)
}
func (p *Disconnect) 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 *Disconnect) width() int {
return p.fill(_LEN, 0)
}
func (p *Disconnect) 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 *Disconnect) variableHeader(b []byte, i int) int {
n := i
proplen := p.properties(_LEN, 0)
if p.reasonCode == 0 && proplen == 0 {
return 0
}
i += p.reasonCode.fill(b, i)
i += vbint(proplen).fill(b, i)
i += p.properties(b, i)
return i - n
}
func (p *Disconnect) UnmarshalBinary(data []byte) error {
b := &buffer{data: data}
b.get(&p.reasonCode)
b.getAny(p.propertyMap(), p.appendUserProperty)
return b.err
}
func (p *Disconnect) propertyMap() map[Ident]func() wireType {
return map[Ident]func() wireType{}
}