Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #2 from villins/bytelength

Bytelength
  • Loading branch information...
commit 61c9cd4a6fe31aec00aa4e807d20b8e78326acdf 2 parents 4ef0b35 + e20fab2
plucury authored

Showing 1 changed file with 304 additions and 288 deletions. Show diff stats Hide diff stats

  1. 592  mqtt.go
592  mqtt.go
... ...
@@ -1,332 +1,348 @@
1 1
 package mqtt
2 2
 
3  
-import ("bytes"
4  
-        "errors")
  3
+import (
  4
+	"bytes"
  5
+	"errors"
  6
+)
5 7
 
6 8
 type MessageType uint8
7 9
 type ReturnCode uint8
8  
-type Header struct{
9  
-    MessageType MessageType
10  
-    DupFlag, Retain bool
11  
-    QosLevel uint8
12  
-    Length uint32
  10
+type Header struct {
  11
+	MessageType     MessageType
  12
+	DupFlag, Retain bool
  13
+	QosLevel        uint8
  14
+	Length          uint32
13 15
 }
14  
-type ConnectFlags struct{
15  
-    UsernameFlag, PasswordFlag, WillRetain, WillFlag, CleanSession bool
16  
-    WillQos uint8
  16
+type ConnectFlags struct {
  17
+	UsernameFlag, PasswordFlag, WillRetain, WillFlag, CleanSession bool
  18
+	WillQos                                                        uint8
17 19
 }
18  
-type Mqtt struct{
19  
-    Header *Header
20  
-    ProtocolName, TopicName, ClientId, WillTopic, WillMessage, Username, Password string
21  
-    ProtocolVersion uint8
22  
-    ConnectFlags *ConnectFlags
23  
-    KeepAliveTimer, MessageId uint16
24  
-    Data []byte
25  
-    Topics []string
26  
-    Topics_qos []uint8
27  
-    ReturnCode ReturnCode
  20
+type Mqtt struct {
  21
+	Header                                                                        *Header
  22
+	ProtocolName, TopicName, ClientId, WillTopic, WillMessage, Username, Password string
  23
+	ProtocolVersion                                                               uint8
  24
+	ConnectFlags                                                                  *ConnectFlags
  25
+	KeepAliveTimer, MessageId                                                     uint16
  26
+	Data                                                                          []byte
  27
+	Topics                                                                        []string
  28
+	Topics_qos                                                                    []uint8
  29
+	ReturnCode                                                                    ReturnCode
28 30
 }
29 31
 
30  
-const(
31  
-    CONNECT = MessageType(iota + 1)
32  
-    CONNACK
33  
-    PUBLISH
34  
-    PUBACK
35  
-    PUBREC
36  
-    PUBREL
37  
-    PUBCOMP
38  
-    SUBSCRIBE
39  
-    SUBACK
40  
-    UNSUBSCRIBE
41  
-    UNSUBACK
42  
-    PINGREQ
43  
-    PINGRESP
44  
-    DISCONNECT
  32
+const (
  33
+	CONNECT = MessageType(iota + 1)
  34
+	CONNACK
  35
+	PUBLISH
  36
+	PUBACK
  37
+	PUBREC
  38
+	PUBREL
  39
+	PUBCOMP
  40
+	SUBSCRIBE
  41
+	SUBACK
  42
+	UNSUBSCRIBE
  43
+	UNSUBACK
  44
+	PINGREQ
  45
+	PINGRESP
  46
+	DISCONNECT
45 47
 )
46 48
 
47  
-const(
48  
-    ACCEPTED = ReturnCode(iota)
49  
-    UNACCEPTABLE_PROTOCOL_VERSION
50  
-    IDENTIFIER_REJECTED
51  
-    SERVER_UNAVAILABLE
52  
-    BAD_USERNAME_OR_PASSWORD
53  
-    NOT_AUTHORIZED
  49
+const (
  50
+	ACCEPTED = ReturnCode(iota)
  51
+	UNACCEPTABLE_PROTOCOL_VERSION
  52
+	IDENTIFIER_REJECTED
  53
+	SERVER_UNAVAILABLE
  54
+	BAD_USERNAME_OR_PASSWORD
  55
+	NOT_AUTHORIZED
54 56
 )
55 57
 
56  
-func getUint8(b []byte, p *int)uint8{
57  
-    *p += 1
58  
-    return uint8(b[*p-1])
  58
+func getUint8(b []byte, p *int) uint8 {
  59
+	*p += 1
  60
+	return uint8(b[*p-1])
59 61
 }
60 62
 
61  
-func getUint16(b []byte, p *int)uint16{
62  
-    *p += 2
63  
-    return uint16(b[*p-2] << 8) + uint16(b[*p-1])
  63
+func getUint16(b []byte, p *int) uint16 {
  64
+	*p += 2
  65
+	return uint16(b[*p-2]<<8) + uint16(b[*p-1])
64 66
 }
65 67
 
66  
-func getString(b []byte, p *int)string{
67  
-    length := int(getUint16(b, p))
68  
-    *p += length
69  
-    return string(b[*p-length:*p])
  68
+func getString(b []byte, p *int) string {
  69
+	length := int(getUint16(b, p))
  70
+	*p += length
  71
+	return string(b[*p-length : *p])
70 72
 }
71 73
 
72  
-func getHeader(b []byte, p *int)*Header{
73  
-    byte1 := b[*p]
74  
-    *p += 1
75  
-    header := new(Header)
76  
-    header.MessageType = MessageType(byte1 & 0xF0 >> 4)
77  
-    header.DupFlag = byte1 & 0x08 > 0
78  
-    header.QosLevel = uint8(byte1 & 0x06 >> 1)
79  
-    header.Retain = byte1 & 0x01 > 0
80  
-    header.Length = decodeLength(b, p)
81  
-    return header
  74
+func getHeader(b []byte, p *int) *Header {
  75
+	byte1 := b[*p]
  76
+	*p += 1
  77
+	header := new(Header)
  78
+	header.MessageType = MessageType(byte1 & 0xF0 >> 4)
  79
+	header.DupFlag = byte1&0x08 > 0
  80
+	header.QosLevel = uint8(byte1 & 0x06 >> 1)
  81
+	header.Retain = byte1&0x01 > 0
  82
+	header.Length = decodeLength(b, p)
  83
+	return header
82 84
 }
83 85
 
84  
-func getConnectFlags(b []byte, p *int)*ConnectFlags{
85  
-    bit := b[*p]
86  
-    *p += 1
87  
-    flags := new(ConnectFlags)
88  
-    flags.UsernameFlag = bit & 0x80 > 0
89  
-    flags.PasswordFlag = bit & 0x40 > 0
90  
-    flags.WillRetain = bit & 0x20 > 0
91  
-    flags.WillQos = uint8(bit & 0x18 >> 3)
92  
-    flags.WillFlag = bit & 0x04 > 0
93  
-    flags.CleanSession = bit & 0x02 > 0
94  
-    return flags
  86
+func getConnectFlags(b []byte, p *int) *ConnectFlags {
  87
+	bit := b[*p]
  88
+	*p += 1
  89
+	flags := new(ConnectFlags)
  90
+	flags.UsernameFlag = bit&0x80 > 0
  91
+	flags.PasswordFlag = bit&0x40 > 0
  92
+	flags.WillRetain = bit&0x20 > 0
  93
+	flags.WillQos = uint8(bit & 0x18 >> 3)
  94
+	flags.WillFlag = bit&0x04 > 0
  95
+	flags.CleanSession = bit&0x02 > 0
  96
+	return flags
95 97
 }
96 98
 
97  
-func Decode(b []byte)(*Mqtt, error){
98  
-    mqtt := new(Mqtt)
99  
-    inx := 0
100  
-    mqtt.Header = getHeader(b, &inx)
101  
-    if mqtt.Header.Length != uint32(len(b) - inx){
102  
-        return nil, errors.New("Message length is wrong!")
103  
-    }
104  
-    if msgType := uint8(mqtt.Header.MessageType); msgType < 1 || msgType > 14{
105  
-        return nil, errors.New("Message Type is invalid!")
106  
-    }
107  
-    switch mqtt.Header.MessageType{
108  
-        case CONNECT:{
109  
-            mqtt.ProtocolName = getString(b, &inx)
110  
-            mqtt.ProtocolVersion = getUint8(b, &inx)
111  
-            mqtt.ConnectFlags = getConnectFlags(b, &inx)
112  
-            mqtt.KeepAliveTimer = getUint16(b, &inx)
113  
-            mqtt.ClientId = getString(b, &inx)
114  
-            if mqtt.ConnectFlags.WillFlag{
115  
-                mqtt.WillTopic = getString(b, &inx)
116  
-                mqtt.WillMessage = getString(b, &inx)
117  
-            }
118  
-            if mqtt.ConnectFlags.UsernameFlag && inx < len(b){
119  
-                mqtt.Username = getString(b, &inx)
120  
-            }
121  
-            if mqtt.ConnectFlags.PasswordFlag && inx < len(b){
122  
-                mqtt.Password = getString(b, &inx)
123  
-            }
124  
-        }
125  
-        case CONNACK:{
126  
-            inx += 1
127  
-            mqtt.ReturnCode = ReturnCode(getUint8(b, &inx))
128  
-            if code := uint8(mqtt.ReturnCode);code > 5{
129  
-                return nil, errors.New("ReturnCode is invalid!")
130  
-            }
131  
-        }
132  
-        case PUBLISH:{
133  
-            mqtt.TopicName = getString(b, &inx)
134  
-            if qos := mqtt.Header.QosLevel;qos == 1 || qos == 2{
135  
-                mqtt.MessageId = getUint16(b, &inx)
136  
-            }
137  
-            mqtt.Data = b[inx:len(b)]
138  
-            inx = len(b)
139  
-        }
140  
-        case PUBACK, PUBREC, PUBREL, PUBCOMP, UNSUBACK:{
141  
-            mqtt.MessageId = getUint16(b, &inx)
142  
-        }
143  
-        case SUBSCRIBE:{
144  
-            if qos := mqtt.Header.QosLevel;qos == 1 || qos == 2{
145  
-                mqtt.MessageId = getUint16(b, &inx)
146  
-            }
147  
-            topics := make([]string, 0)
148  
-            topics_qos := make([]uint8, 0)
149  
-            for ; inx < len(b);{
150  
-                topics = append(topics, getString(b, &inx))
151  
-                topics_qos = append(topics_qos, getUint8(b, &inx))
152  
-            }
153  
-            mqtt.Topics = topics
154  
-            mqtt.Topics_qos = topics_qos
155  
-        }
156  
-        case SUBACK:{
157  
-            mqtt.MessageId = getUint16(b, &inx)
158  
-            topics_qos := make([]uint8, 0)
159  
-            for ; inx < len(b);{
160  
-                topics_qos = append(topics_qos, getUint8(b, &inx))
161  
-            }
162  
-            mqtt.Topics_qos = topics_qos
163  
-        }
164  
-        case UNSUBSCRIBE:{
165  
-            if qos := mqtt.Header.QosLevel;qos == 1 || qos == 2{
166  
-                mqtt.MessageId = getUint16(b, &inx)
167  
-            }
168  
-            topics := make([]string, 0)
169  
-            for ; inx < len(b);{
170  
-                topics = append(topics, getString(b, &inx))
171  
-            }
172  
-            mqtt.Topics = topics
173  
-        }
174  
-    }
175  
-    return mqtt, nil
  99
+func Decode(b []byte) (*Mqtt, error) {
  100
+	mqtt := new(Mqtt)
  101
+	inx := 0
  102
+	mqtt.Header = getHeader(b, &inx)
  103
+	if mqtt.Header.Length != uint32(len(b)-inx) {
  104
+		return nil, errors.New("Message length is wrong!")
  105
+	}
  106
+	if msgType := uint8(mqtt.Header.MessageType); msgType < 1 || msgType > 14 {
  107
+		return nil, errors.New("Message Type is invalid!")
  108
+	}
  109
+	switch mqtt.Header.MessageType {
  110
+	case CONNECT:
  111
+		{
  112
+			mqtt.ProtocolName = getString(b, &inx)
  113
+			mqtt.ProtocolVersion = getUint8(b, &inx)
  114
+			mqtt.ConnectFlags = getConnectFlags(b, &inx)
  115
+			mqtt.KeepAliveTimer = getUint16(b, &inx)
  116
+			mqtt.ClientId = getString(b, &inx)
  117
+			if mqtt.ConnectFlags.WillFlag {
  118
+				mqtt.WillTopic = getString(b, &inx)
  119
+				mqtt.WillMessage = getString(b, &inx)
  120
+			}
  121
+			if mqtt.ConnectFlags.UsernameFlag && inx < len(b) {
  122
+				mqtt.Username = getString(b, &inx)
  123
+			}
  124
+			if mqtt.ConnectFlags.PasswordFlag && inx < len(b) {
  125
+				mqtt.Password = getString(b, &inx)
  126
+			}
  127
+		}
  128
+	case CONNACK:
  129
+		{
  130
+			inx += 1
  131
+			mqtt.ReturnCode = ReturnCode(getUint8(b, &inx))
  132
+			if code := uint8(mqtt.ReturnCode); code > 5 {
  133
+				return nil, errors.New("ReturnCode is invalid!")
  134
+			}
  135
+		}
  136
+	case PUBLISH:
  137
+		{
  138
+			mqtt.TopicName = getString(b, &inx)
  139
+			if qos := mqtt.Header.QosLevel; qos == 1 || qos == 2 {
  140
+				mqtt.MessageId = getUint16(b, &inx)
  141
+			}
  142
+			mqtt.Data = b[inx:len(b)]
  143
+			inx = len(b)
  144
+		}
  145
+	case PUBACK, PUBREC, PUBREL, PUBCOMP, UNSUBACK:
  146
+		{
  147
+			mqtt.MessageId = getUint16(b, &inx)
  148
+		}
  149
+	case SUBSCRIBE:
  150
+		{
  151
+			if qos := mqtt.Header.QosLevel; qos == 1 || qos == 2 {
  152
+				mqtt.MessageId = getUint16(b, &inx)
  153
+			}
  154
+			topics := make([]string, 0)
  155
+			topics_qos := make([]uint8, 0)
  156
+			for inx < len(b) {
  157
+				topics = append(topics, getString(b, &inx))
  158
+				topics_qos = append(topics_qos, getUint8(b, &inx))
  159
+			}
  160
+			mqtt.Topics = topics
  161
+			mqtt.Topics_qos = topics_qos
  162
+		}
  163
+	case SUBACK:
  164
+		{
  165
+			mqtt.MessageId = getUint16(b, &inx)
  166
+			topics_qos := make([]uint8, 0)
  167
+			for inx < len(b) {
  168
+				topics_qos = append(topics_qos, getUint8(b, &inx))
  169
+			}
  170
+			mqtt.Topics_qos = topics_qos
  171
+		}
  172
+	case UNSUBSCRIBE:
  173
+		{
  174
+			if qos := mqtt.Header.QosLevel; qos == 1 || qos == 2 {
  175
+				mqtt.MessageId = getUint16(b, &inx)
  176
+			}
  177
+			topics := make([]string, 0)
  178
+			for inx < len(b) {
  179
+				topics = append(topics, getString(b, &inx))
  180
+			}
  181
+			mqtt.Topics = topics
  182
+		}
  183
+	}
  184
+	return mqtt, nil
176 185
 }
177 186
 
178  
-func setUint8(val uint8, buf *bytes.Buffer){
179  
-    buf.WriteByte(byte(val))
  187
+func setUint8(val uint8, buf *bytes.Buffer) {
  188
+	buf.WriteByte(byte(val))
180 189
 }
181 190
 
182  
-func setUint16(val uint16, buf *bytes.Buffer){
183  
-    buf.WriteByte(byte(val & 0xff00 >> 8))
184  
-    buf.WriteByte(byte(val & 0x00ff))
  191
+func setUint16(val uint16, buf *bytes.Buffer) {
  192
+	buf.WriteByte(byte(val & 0xff00 >> 8))
  193
+	buf.WriteByte(byte(val & 0x00ff))
185 194
 }
186 195
 
187  
-func setString(val string, buf *bytes.Buffer){
188  
-    length := uint16(len(val))
189  
-    setUint16(length, buf)
190  
-    buf.WriteString(val)
  196
+func setString(val string, buf *bytes.Buffer) {
  197
+	length := uint16(len(val))
  198
+	setUint16(length, buf)
  199
+	buf.WriteString(val)
191 200
 }
192 201
 
193  
-func setHeader(header *Header, buf *bytes.Buffer){
194  
-    val := byte(uint8(header.MessageType)) << 4
195  
-    val |= (boolToByte(header.DupFlag) << 3)
196  
-    val |= byte(header.QosLevel) << 1
197  
-    val |= boolToByte(header.Retain)
198  
-    buf.WriteByte(val)
  202
+func setHeader(header *Header, buf *bytes.Buffer) {
  203
+	val := byte(uint8(header.MessageType)) << 4
  204
+	val |= (boolToByte(header.DupFlag) << 3)
  205
+	val |= byte(header.QosLevel) << 1
  206
+	val |= boolToByte(header.Retain)
  207
+	buf.WriteByte(val)
199 208
 }
200 209
 
201  
-func setConnectFlags(flags *ConnectFlags, buf *bytes.Buffer){
202  
-    val := boolToByte(flags.UsernameFlag) << 7
203  
-    val |= boolToByte(flags.PasswordFlag) << 6
204  
-    val |= boolToByte(flags.WillRetain) << 5
205  
-    val |= byte(flags.WillQos) << 3
206  
-    val |= boolToByte(flags.WillFlag) << 2
207  
-    val |= boolToByte(flags.CleanSession) << 1
208  
-    buf.WriteByte(val)
  210
+func setConnectFlags(flags *ConnectFlags, buf *bytes.Buffer) {
  211
+	val := boolToByte(flags.UsernameFlag) << 7
  212
+	val |= boolToByte(flags.PasswordFlag) << 6
  213
+	val |= boolToByte(flags.WillRetain) << 5
  214
+	val |= byte(flags.WillQos) << 3
  215
+	val |= boolToByte(flags.WillFlag) << 2
  216
+	val |= boolToByte(flags.CleanSession) << 1
  217
+	buf.WriteByte(val)
209 218
 }
210 219
 
211  
-func boolToByte(val bool)byte{
212  
-    if val{
213  
-        return byte(1)
214  
-    }
215  
-    return byte(0)
  220
+func boolToByte(val bool) byte {
  221
+	if val {
  222
+		return byte(1)
  223
+	}
  224
+	return byte(0)
216 225
 }
217 226
 
218  
-func Encode(mqtt *Mqtt)([]byte, error){
219  
-    err := valid(mqtt)
220  
-    if err != nil{
221  
-        return nil, err
222  
-    }
223  
-    var headerbuf, buf bytes.Buffer
224  
-    setHeader(mqtt.Header, &headerbuf)
225  
-    switch mqtt.Header.MessageType{
226  
-        case CONNECT:{
227  
-            setString(mqtt.ProtocolName, &buf)
228  
-            setUint8(mqtt.ProtocolVersion, &buf)
229  
-            setConnectFlags(mqtt.ConnectFlags, &buf)
230  
-            setUint16(mqtt.KeepAliveTimer, &buf)
231  
-            setString(mqtt.ClientId, &buf)
232  
-            if mqtt.ConnectFlags.WillFlag{
233  
-                setString(mqtt.WillTopic, &buf)
234  
-                setString(mqtt.WillMessage, &buf)
235  
-            }
236  
-            if mqtt.ConnectFlags.UsernameFlag && len(mqtt.Username) > 0{
237  
-                setString(mqtt.Username, &buf)
238  
-            }
239  
-            if mqtt.ConnectFlags.PasswordFlag && len(mqtt.Password) > 0{
240  
-                setString(mqtt.Password, &buf)
241  
-            }
242  
-        }
243  
-        case CONNACK:{
244  
-            buf.WriteByte(byte(0))
245  
-            setUint8(uint8(mqtt.ReturnCode), &buf)
246  
-        }
247  
-        case PUBLISH:{
248  
-            setString(mqtt.TopicName, &buf)
249  
-            if qos := mqtt.Header.QosLevel;qos == 1 || qos == 2{
250  
-                setUint16(mqtt.MessageId, &buf)
251  
-            }
252  
-            buf.Write(mqtt.Data)
253  
-        }
254  
-        case PUBACK, PUBREC, PUBREL, PUBCOMP, UNSUBACK:{
255  
-            setUint16(mqtt.MessageId, &buf)
256  
-        }
257  
-        case SUBSCRIBE:{
258  
-            if qos := mqtt.Header.QosLevel;qos == 1 || qos == 2{
259  
-                setUint16(mqtt.MessageId, &buf)
260  
-            }
261  
-            for i := 0;i < len(mqtt.Topics);i += 1{
262  
-                setString(mqtt.Topics[i], &buf)
263  
-                setUint8(mqtt.Topics_qos[i], &buf)
264  
-            }
265  
-        }
266  
-        case SUBACK:{
267  
-            setUint16(mqtt.MessageId, &buf)
268  
-            for i := 0;i < len(mqtt.Topics_qos);i += 1{
269  
-                setUint8(mqtt.Topics_qos[i], &buf)
270  
-            }
271  
-        }
272  
-        case UNSUBSCRIBE:{
273  
-            if qos := mqtt.Header.QosLevel;qos == 1 || qos == 2{
274  
-                setUint16(mqtt.MessageId, &buf)
275  
-            }
276  
-            for i := 0;i < len(mqtt.Topics); i += 1{
277  
-                setString(mqtt.Topics[i], &buf)
278  
-            }
279  
-        }
280  
-    }
281  
-    if buf.Len() > 268435455{
282  
-        return nil, errors.New("Message is too long!")
283  
-    }
284  
-    encodeLength(uint32(buf.Len()), &headerbuf)
285  
-    headerbuf.Write(buf.Bytes())
286  
-    return headerbuf.Bytes(), nil
  227
+func Encode(mqtt *Mqtt) ([]byte, error) {
  228
+	err := valid(mqtt)
  229
+	if err != nil {
  230
+		return nil, err
  231
+	}
  232
+	var headerbuf, buf bytes.Buffer
  233
+	setHeader(mqtt.Header, &headerbuf)
  234
+	switch mqtt.Header.MessageType {
  235
+	case CONNECT:
  236
+		{
  237
+			setString(mqtt.ProtocolName, &buf)
  238
+			setUint8(mqtt.ProtocolVersion, &buf)
  239
+			setConnectFlags(mqtt.ConnectFlags, &buf)
  240
+			setUint16(mqtt.KeepAliveTimer, &buf)
  241
+			setString(mqtt.ClientId, &buf)
  242
+			if mqtt.ConnectFlags.WillFlag {
  243
+				setString(mqtt.WillTopic, &buf)
  244
+				setString(mqtt.WillMessage, &buf)
  245
+			}
  246
+			if mqtt.ConnectFlags.UsernameFlag && len(mqtt.Username) > 0 {
  247
+				setString(mqtt.Username, &buf)
  248
+			}
  249
+			if mqtt.ConnectFlags.PasswordFlag && len(mqtt.Password) > 0 {
  250
+				setString(mqtt.Password, &buf)
  251
+			}
  252
+		}
  253
+	case CONNACK:
  254
+		{
  255
+			buf.WriteByte(byte(0))
  256
+			setUint8(uint8(mqtt.ReturnCode), &buf)
  257
+		}
  258
+	case PUBLISH:
  259
+		{
  260
+			setString(mqtt.TopicName, &buf)
  261
+			if qos := mqtt.Header.QosLevel; qos == 1 || qos == 2 {
  262
+				setUint16(mqtt.MessageId, &buf)
  263
+			}
  264
+			buf.Write(mqtt.Data)
  265
+		}
  266
+	case PUBACK, PUBREC, PUBREL, PUBCOMP, UNSUBACK:
  267
+		{
  268
+			setUint16(mqtt.MessageId, &buf)
  269
+		}
  270
+	case SUBSCRIBE:
  271
+		{
  272
+			if qos := mqtt.Header.QosLevel; qos == 1 || qos == 2 {
  273
+				setUint16(mqtt.MessageId, &buf)
  274
+			}
  275
+			for i := 0; i < len(mqtt.Topics); i += 1 {
  276
+				setString(mqtt.Topics[i], &buf)
  277
+				setUint8(mqtt.Topics_qos[i], &buf)
  278
+			}
  279
+		}
  280
+	case SUBACK:
  281
+		{
  282
+			setUint16(mqtt.MessageId, &buf)
  283
+			for i := 0; i < len(mqtt.Topics_qos); i += 1 {
  284
+				setUint8(mqtt.Topics_qos[i], &buf)
  285
+			}
  286
+		}
  287
+	case UNSUBSCRIBE:
  288
+		{
  289
+			if qos := mqtt.Header.QosLevel; qos == 1 || qos == 2 {
  290
+				setUint16(mqtt.MessageId, &buf)
  291
+			}
  292
+			for i := 0; i < len(mqtt.Topics); i += 1 {
  293
+				setString(mqtt.Topics[i], &buf)
  294
+			}
  295
+		}
  296
+	}
  297
+	if buf.Len() > 268435455 {
  298
+		return nil, errors.New("Message is too long!")
  299
+	}
  300
+	encodeLength(uint32(buf.Len()), &headerbuf)
  301
+	headerbuf.Write(buf.Bytes())
  302
+	return headerbuf.Bytes(), nil
287 303
 }
288 304
 
289  
-func valid(mqtt *Mqtt)error{
290  
-    if msgType := uint8(mqtt.Header.MessageType);msgType < 1 || msgType > 14{
291  
-        return errors.New("MessageType is invalid!")
292  
-    }
293  
-    if mqtt.Header.QosLevel > 3 {
294  
-        return errors.New("Qos Level is invalid!")
295  
-    }
296  
-    if mqtt.ConnectFlags != nil && mqtt.ConnectFlags.WillQos > 3{
297  
-        return errors.New("Will Qos Level is invalid!")
298  
-    }
299  
-    return nil
  305
+func valid(mqtt *Mqtt) error {
  306
+	if msgType := uint8(mqtt.Header.MessageType); msgType < 1 || msgType > 14 {
  307
+		return errors.New("MessageType is invalid!")
  308
+	}
  309
+	if mqtt.Header.QosLevel > 3 {
  310
+		return errors.New("Qos Level is invalid!")
  311
+	}
  312
+	if mqtt.ConnectFlags != nil && mqtt.ConnectFlags.WillQos > 3 {
  313
+		return errors.New("Will Qos Level is invalid!")
  314
+	}
  315
+	return nil
300 316
 }
301 317
 
302  
-func decodeLength(b []byte, p *int)uint32{
303  
-    m := uint32(1)
304  
-    v := uint32(b[*p] & 0x7f)
305  
-    *p += 1
306  
-    for ; b[*p-1] & 0x80 > 0 ;{
307  
-        m *= 128
308  
-        v += uint32(b[*p] & 0x7f) * m
309  
-        *p += 1
310  
-    }
311  
-    return v
  318
+func decodeLength(b []byte, p *int) uint32 {
  319
+	m := uint32(1)
  320
+	v := uint32(b[*p] & 0x7f)
  321
+	*p += 1
  322
+	for b[*p-1]&0x80 > 0 {
  323
+		m *= 128
  324
+		v += uint32(b[*p]&0x7f) * m
  325
+		*p += 1
  326
+	}
  327
+	return v
312 328
 }
313 329
 
314  
-func encodeLength(length uint32, buf *bytes.Buffer){
315  
-    if length == 0{
316  
-        buf.WriteByte(byte(0))
317  
-        return
318  
-    }
319  
-    var lbuf bytes.Buffer
320  
-    for ; length > 0;{
321  
-        digit := length % 128
322  
-        length = length / 128
323  
-        if length > 0{
324  
-            digit = digit | 0x80
325  
-        }
326  
-        lbuf.WriteByte(byte(digit))
327  
-    }
328  
-    blen := lbuf.Bytes()
329  
-    for i := 1;i <= len(blen);i += 1{
330  
-        buf.WriteByte(blen[len(blen)-i])
331  
-    }
  330
+func encodeLength(length uint32, buf *bytes.Buffer) {
  331
+	if length == 0 {
  332
+		buf.WriteByte(byte(0))
  333
+		return
  334
+	}
  335
+	var lbuf bytes.Buffer
  336
+	for length > 0 {
  337
+		digit := length % 128
  338
+		length = length / 128
  339
+		if length > 0 {
  340
+			digit = digit | 0x80
  341
+		}
  342
+		lbuf.WriteByte(byte(digit))
  343
+	}
  344
+	blen := lbuf.Bytes()
  345
+	for i := 0; i < len(blen); i += 1 {
  346
+		buf.WriteByte(blen[i])
  347
+	}
332 348
 }

0 notes on commit 61c9cd4

Please sign in to comment.
Something went wrong with that request. Please try again.