-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpacketunsubscribe.go
142 lines (112 loc) · 4.01 KB
/
packetunsubscribe.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
130
131
132
133
134
135
136
137
138
139
140
141
142
package mqtt
import (
"bytes"
"fmt"
)
////////////////////Interface//////////////////////////////
type PacketUnsubscribe interface {
Packet
//Variable Header
GetPacketId() uint16
SetPacketId(id uint16)
//Payload
GetUnsubscribeTopics() []string
SetUnsubscribeTopics([]string)
}
////////////////////Implementation////////////////////////
type packet_unsubscribe struct {
packet
packetId uint16
topics []string
}
func NewPacketUnsubscribe() *packet_unsubscribe {
this := packet_unsubscribe{}
this.IBytizer = &this
this.IParser = &this
this.packetType = PACKET_UNSUBSCRIBE
this.packetFlag = 2
return &this
}
func (this *packet_unsubscribe) IBytize() []byte {
var buffer bytes.Buffer
var buffer2 bytes.Buffer
//1st Pass
//Variable Header
buffer2.WriteByte(byte(this.packetId >> 8))
buffer2.WriteByte(byte(this.packetId & 0xFF))
//Payload
for i := 0; i < len(this.topics); i++ {
topicLength := uint16(len(this.topics[i]))
buffer2.WriteByte(byte(topicLength >> 8))
buffer2.WriteByte(byte(topicLength & 0xFF))
buffer2.WriteString(this.topics[i])
}
//2nd Pass
//Fixed Header
buffer.WriteByte((byte(this.packetType) << 4) | (this.packetFlag & 0x0F))
buf2 := buffer2.Bytes()
remainingLength := uint32(len(buf2))
x, _ := this.EncodingRemainingLength(remainingLength)
buffer.Write(x)
//Viariable Header + Payload
buffer.Write(buf2)
return buffer.Bytes()
}
func (this *packet_unsubscribe) IParse(buffer []byte) error {
var err error
var bufferLength, remainingLength, consumedBytes, topicLength uint32
bufferLength = uint32(len(buffer))
if buffer == nil || bufferLength < 4+3 {
return fmt.Errorf("Invalid %s Control Packet Size %x\n", PACKET_TYPE_STRINGS[this.packetType], bufferLength)
}
//Fixed Header
if packetType := PacketType((buffer[0] >> 4) & 0x0F); packetType != this.packetType {
return fmt.Errorf("Invalid %s Control Packet Type %x\n", PACKET_TYPE_STRINGS[this.packetType], packetType)
}
if packetFlag := buffer[0] & 0x0F; packetFlag != this.packetFlag {
return fmt.Errorf("Invalid %s Control Packet Flags %x\n", PACKET_TYPE_STRINGS[this.packetType], packetFlag)
}
if remainingLength, consumedBytes, err = this.DecodingRemainingLength(buffer[1:]); err != nil {
return fmt.Errorf("Invalid %s Control Packet DecodingRemainingLength %s\n", PACKET_TYPE_STRINGS[this.packetType], err.Error())
}
if consumedBytes += 1; bufferLength < consumedBytes+remainingLength {
return fmt.Errorf("Invalid %s Control Packet Remaining Length %x\n", PACKET_TYPE_STRINGS[this.packetType], remainingLength)
}
buffer = buffer[:consumedBytes+remainingLength]
bufferLength = consumedBytes + remainingLength
//Variable Header
if this.packetId = ((uint16(buffer[consumedBytes])) << 8) | uint16(buffer[consumedBytes+1]); this.packetId == 0 {
return fmt.Errorf("Invalid %s Control Packet PacketId 0\n", PACKET_TYPE_STRINGS[this.packetType])
}
if consumedBytes += 2; bufferLength < consumedBytes+3 {
return fmt.Errorf("Invalid %s Control Packet Must Have at least One Topic\n", PACKET_TYPE_STRINGS[this.packetType])
}
//Payload
this.topics = nil
for bufferLength > consumedBytes {
if bufferLength < consumedBytes+3 {
return fmt.Errorf("Invalid %s Control Packet Payload Length\n", PACKET_TYPE_STRINGS[this.packetType])
}
topicLength = ((uint32(buffer[consumedBytes])) << 8) | uint32(buffer[consumedBytes+1])
if consumedBytes += 2; bufferLength < consumedBytes+topicLength || topicLength == 0 {
return fmt.Errorf("Invalid %s Control Packet Topic Length\n", PACKET_TYPE_STRINGS[this.packetType])
}
this.topics = append(this.topics, string(buffer[consumedBytes:consumedBytes+topicLength]))
consumedBytes += topicLength
}
return nil
}
//Variable Header
func (this *packet_unsubscribe) GetPacketId() uint16 {
return this.packetId
}
func (this *packet_unsubscribe) SetPacketId(id uint16) {
this.packetId = id
}
//Payload
func (this *packet_unsubscribe) GetUnsubscribeTopics() []string {
return this.topics
}
func (this *packet_unsubscribe) SetUnsubscribeTopics(topics []string) {
this.topics = topics
}