-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscribe.go
67 lines (63 loc) · 1.31 KB
/
subscribe.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
package packets
import (
"bytes"
"io"
)
type Subscription struct {
Topic string
ShareName string
RetainHandling byte
RetainAsPublished bool
NoLocal bool
Qos byte
SubID uint32
}
type Subscribe struct {
FixHeader *FixHeader
Version byte
PacketID uint16
Subscriptions []Subscription
Properties *Properties
}
// Pack Subscribe Packet
func (s *Subscribe) Pack(w io.Writer) error {
return nil
}
// Unpack Subscribe Packet
func (s *Subscribe) Unpack(r io.Reader) error {
buf := make([]byte, s.FixHeader.RemainLen)
if _, err := io.ReadFull(r, buf); err != nil {
return err
}
bufr := bytes.NewBuffer(buf)
s.PacketID = readUint16(bufr)
if s.Version == V5 {
s.Properties = &Properties{}
if err := s.Properties.Unpack(bufr); err != nil {
return err
}
}
for {
if bufr.Len() == 0 {
return nil
}
name := decodeString(bufr)
sub := Subscription{Topic: name}
opts, err := bufr.ReadByte()
if err != nil {
return ErrMalformed
}
if s.Version == V5 {
sub.RetainHandling = (opts >> 4) & 0x03
sub.RetainAsPublished = opts&0x08 > 0
sub.NoLocal = opts&0x04 > 0
sub.Qos = opts & 0x03
} else {
sub.Qos = opts
}
if sub.Qos > Qos2 {
return ErrProtocol
}
s.Subscriptions = append(s.Subscriptions, sub)
}
}