-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnack.go
121 lines (106 loc) · 3.7 KB
/
connack.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
package mqttpackets
import (
"bytes"
"io"
"net"
)
// Connack is the Variable Header definition for a connack control packet
type Connack struct {
Properties *Properties
ReasonCode byte
SessionPresent bool
}
//Unpack is the implementation of the interface required function for a packet
func (c *Connack) Unpack(r *bytes.Buffer) error {
connackFlags, err := r.ReadByte()
if err != nil {
return err
}
c.SessionPresent = connackFlags&0x01 > 0
c.ReasonCode, err = r.ReadByte()
if err != nil {
return err
}
if c.Properties == nil {
return nil
}
err = c.Properties.Unpack(r, CONNACK)
if err != nil {
return err
}
return nil
}
// Buffers is the implementation of the interface required function for a packet
func (c *Connack) Buffers() net.Buffers {
var header bytes.Buffer
if c.SessionPresent {
header.WriteByte(1)
} else {
header.WriteByte(0)
}
header.WriteByte(c.ReasonCode)
if c.Properties == nil {
return net.Buffers{header.Bytes()}
}
idvp := c.Properties.Pack(CONNACK)
propLen := encodeVBI(len(idvp))
n := net.Buffers{header.Bytes(), propLen}
if len(idvp) > 0 {
n = append(n, idvp)
}
return n
}
// WriteTo is the implementation of the interface required function for a packet
func (c *Connack) WriteTo(w io.Writer) (int64, error) {
cp := &ControlPacket{FixedHeader: FixedHeader{Type: CONNACK}}
cp.Content = c
return cp.WriteTo(w)
}
// Reason returns a string representation of the meaning of the ReasonCode
func (c *Connack) Reason() string {
switch c.ReasonCode {
case 0:
return "Success - The Connection is accepted."
case 128:
return "Unspecified error - The Server does not wish to reveal the reason for the failure, or none of the other Reason Codes apply."
case 129:
return "Malformed Packet - Data within the CONNECT packet could not be correctly parsed."
case 130:
return "Protocol Error - Data in the CONNECT packet does not conform to this specification."
case 131:
return "Implementation specific error - The CONNECT is valid but is not accepted by this Server."
case 132:
return "Unsupported Protocol Version - The Server does not support the version of the MQTT protocol requested by the Client."
case 133:
return "Client Identifier not valid - The Client Identifier is a valid string but is not allowed by the Server."
case 134:
return "Bad User Name or Password - The Server does not accept the User Name or Password specified by the Client"
case 135:
return "Not authorized - The Client is not authorized to connect."
case 136:
return "Server unavailable - The MQTT Server is not available."
case 137:
return "Server busy - The Server is busy. Try again later."
case 138:
return "Banned - This Client has been banned by administrative action. Contact the server administrator."
case 140:
return "Bad authentication method - The authentication method is not supported or does not match the authentication method currently in use."
case 144:
return "Topic Name invalid - The Will Topic Name is not malformed, but is not accepted by this Server."
case 149:
return "Packet too large - The CONNECT packet exceeded the maximum permissible size."
case 151:
return "Quota exceeded - An implementation or administrative imposed limit has been exceeded."
case 154:
return "Retain not supported - The Server does not support retained messages, and Will Retain was set to 1."
case 155:
return "QoS not supported - The Server does not support the QoS set in Will QoS."
case 156:
return "Use another server - The Client should temporarily use another server."
case 157:
return "Server moved - The Client should permanently use another server."
case 159:
return "Connection rate exceeded - The connection rate limit has been exceeded."
}
return ""
}