forked from hsg4ok/mqtt
/
unsubscribepacket.go
51 lines (44 loc) · 1009 Bytes
/
unsubscribepacket.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
package mqtt
import (
"bytes"
"errors"
"io"
)
type Unsubscription struct {
Topic
}
func (s Unsubscription) write(w io.Writer) (err error) {
err = s.Topic.write(w)
return
}
func readUnsubscription(r io.Reader) (s Unsubscription, err error) {
s.Topic, err = readTopic(r)
return
}
type UnsubscribePacket struct {
Unsubscriptions []Unsubscription
}
func (p UnsubscribePacket) write(w io.Writer) (err error) {
for _, unsub := range p.Unsubscriptions {
err = unsub.write(w)
if err != nil {
return
}
}
return
}
func readUnsubscribePayload(r *bytes.Reader) (s UnsubscribePacket, err error) {
if r.Len() == 0 {
err = errors.New("cannot unsubscribe from zero topics")
return
}
var unsub Unsubscription
for r.Len() > 0 {
unsub, err = readUnsubscription(r)
if err != nil {
return
}
s.Unsubscriptions = append(s.Unsubscriptions, unsub)
}
return
}