-
Notifications
You must be signed in to change notification settings - Fork 2
/
chunk_client_msg.go
129 lines (112 loc) · 2.86 KB
/
chunk_client_msg.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
package mosquittodb
import (
"encoding/binary"
"fmt"
"io"
"unsafe"
)
type ClientMsgChunk struct {
StoreID DBID
MID uint16
QoS uint8
State uint8
Retain bool
Dup bool
Direction uint8
ClientID string
SubscriptionID uint32 // from MQTT_PROP_SUBSCRIPTION_IDENTIFIER
Properties []Property
}
func (c ClientMsgChunk) String() string {
return fmt.Sprintf("[ClientMsg](q:%d, ret:%v, dup:%v, dir:%d)-'%s' MID:%d State:%d",
c.QoS, c.Retain, c.Dup, c.Direction, c.ClientID, c.MID, c.State)
}
func (d *DB) readClientMsgChunkV56(hdr *ChunkHeader, chunk *ClientMsgChunk) error {
length := hdr.Length
var err error
chunk.StoreID, err = readDBID(d.reader, d.Config.StoreIDSize)
if err != nil {
return err
}
pfClientMsg := struct {
MID uint16
IDLen uint16
QoS uint8
State uint8
RetainDup uint8
Direction uint8
}{}
if err := binary.Read(d.reader, binary.BigEndian, &pfClientMsg); err != nil {
return err
}
chunk.MID = pfClientMsg.MID
chunk.QoS = pfClientMsg.QoS
chunk.State = pfClientMsg.State
chunk.Retain = (pfClientMsg.RetainDup & 0xF0) > 0
chunk.Dup = (pfClientMsg.RetainDup & 0x0F) > 0
chunk.Direction = pfClientMsg.Direction
length -= (uint32(d.Config.StoreIDSize) & 0xFF) +
uint32(unsafe.Sizeof(pfClientMsg)) +
(uint32(pfClientMsg.IDLen) & 0xFFFF)
chunk.ClientID, err = readStringLen(d.reader, pfClientMsg.IDLen)
if err != nil {
return err
}
if length > 0 {
// Todo: Read all properties
data := make([]byte, length)
_, err = io.ReadFull(d.reader, data)
if err != nil {
return err
}
}
return nil
}
func (d *DB) readClientMsgChunkV234(hdr *ChunkHeader, chunk *ClientMsgChunk) error {
var err error
retain, dup := uint8(0), uint8(0)
chunk.ClientID, err = readString(d.reader)
if err != nil {
return err
}
chunk.StoreID, err = readDBID(d.reader, d.Config.StoreIDSize)
if err != nil {
return err
}
if err := binary.Read(d.reader, binary.BigEndian, &chunk.MID); err != nil {
return err
}
if err := binary.Read(d.reader, binary.BigEndian, &chunk.QoS); err != nil {
return err
}
if err := binary.Read(d.reader, binary.BigEndian, &retain); err != nil {
return err
}
if retain > 0 {
chunk.Retain = true
}
if err := binary.Read(d.reader, binary.BigEndian, &chunk.Direction); err != nil {
return err
}
if err := binary.Read(d.reader, binary.BigEndian, &chunk.State); err != nil {
return err
}
if err := binary.Read(d.reader, binary.BigEndian, &dup); err != nil {
return err
}
if dup > 0 {
chunk.Dup = true
}
return nil
}
func (d *DB) ReadClientMsgChunk(hdr *ChunkHeader, chunk *ClientMsgChunk) error {
if hdr.Type != DBChunkClientMsg {
return ErrBadChunkID
}
switch d.Version() {
case MosqDbVersion5, MosqDbVersion6:
return d.readClientMsgChunkV56(hdr, chunk)
default:
return d.readClientMsgChunkV234(hdr, chunk)
}
}