-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunsubscribe.go
68 lines (58 loc) · 1.43 KB
/
unsubscribe.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
package mqttpackets
import (
"bytes"
"io"
"net"
)
// Unsubscribe is the Variable Header definition for a Unsubscribe control packet
type Unsubscribe struct {
Topics []string
Properties *Properties
PacketID uint16
}
// Unpack is the implementation of the interface required function for a packet
func (u *Unsubscribe) Unpack(r *bytes.Buffer) error {
var err error
u.PacketID, err = readUint16(r)
if err != nil {
return err
}
if u.Properties != nil {
err = u.Properties.Unpack(r, UNSUBSCRIBE)
if err != nil {
return err
}
}
for {
t, err := readString(r)
if err != nil && err != io.EOF {
return err
}
if err == io.EOF {
break
}
u.Topics = append(u.Topics, t)
}
return nil
}
// Buffers is the implementation of the interface required function for a packet
func (u *Unsubscribe) Buffers() net.Buffers {
var b bytes.Buffer
writeUint16(u.PacketID, &b)
var topics bytes.Buffer
for _, t := range u.Topics {
writeString(t, &topics)
}
if u.Properties == nil {
return net.Buffers{b.Bytes(), topics.Bytes()}
}
idvp := u.Properties.Pack(UNSUBSCRIBE)
propLen := encodeVBI(len(idvp))
return net.Buffers{b.Bytes(), propLen, idvp, topics.Bytes()}
}
// WriteTo is the implementation of the interface required function for a packet
func (u *Unsubscribe) WriteTo(w io.Writer) (int64, error) {
cp := &ControlPacket{FixedHeader: FixedHeader{Type: UNSUBSCRIBE, Flags: 2}}
cp.Content = u
return cp.WriteTo(w)
}