maeqtt/mqtt/packets/Connack.go
2021-09-01 22:36:43 +02:00

70 lines
2.2 KiB
Go

package packets
import (
"bytes"
"io"
"badat.dev/maeqtt/v2/mqtt/properties"
"badat.dev/maeqtt/v2/mqtt/types"
)
type ConnectReasonCode byte
const (
ConnectReasonCodeSuccess ConnectReasonCode = 0
ConnectReasonCodeUnspecified = 128
ConnectReasonCodeMalformedPacket = 129
ConnectReasonCodeProtocolError = 130
ConnectReasonCodeImplErorr = 131
ConnectReasonCodeUnsupportedProtoVer = 132
ConnectReasonCodeClientIDNotValid = 133
ConnectReasonCodeBadUsernameOrPassword = 134
ConnectReasonCodeNotAuthorized = 135
ConnectReasonCodeServerUnavaliable = 136
ConnectReasonCodeServerBusy = 137
ConnectReasonCodeBanned = 138
ConnectReasonCodeBadAuthenticationMethod = 140
ConnectReasonCodeTopicNameInvalid = 144
ConnectReasonCodePacketTooLarge = 149
ConnectReasonCodeQuotaExceeded = 151
ConnectReasonCodePayloadFormatInvalid = 153
ConnectReasonCodeRetainNotSupported = 154
ConnectReasonCodeQoSNotSupported = 155
ConnectReasonCodeUseAnotherServer = 156
ConnectReasonCodeServerMoved = 157
ConnectReasonCodeConnectionRateExceeded = 159
)
type ConnackPacket struct {
ResonCode ConnectReasonCode
SessionPresent bool
Properties properties.ConnackPacketProperties
}
func (p ConnackPacket) Write(w io.Writer) error {
buf := bytes.NewBuffer([]byte{})
var ackFlags [8]bool
ackFlags[0] = p.SessionPresent
err := types.WriteBits(buf, ackFlags)
if err != nil {
return err
}
err = buf.WriteByte(byte(p.ResonCode))
if err != nil {
return err
}
err = properties.WriteProps(buf, p.Properties.ArrayOf())
if err != nil {
return err
}
conPack := controlPacket{
packetType: PacketTypeConnack,
flags: 0,
reader: buf,
}
return conPack.write(w)
}