maeqtt/mqtt/packets/Connack.go

71 lines
2.2 KiB
Go
Raw Normal View History

2021-08-26 15:08:24 +02:00
package packets
import (
"bytes"
"io"
"badat.dev/maeqtt/v2/mqtt/properties"
"badat.dev/maeqtt/v2/mqtt/types"
)
type ConnectReasonCode byte
const (
ConnectReasonCodeSuccess ConnectReasonCode = 0
2021-10-17 21:09:44 +02:00
ConnectReasonCodeUnspecified ConnectReasonCode = 128
ConnectReasonCodeMalformedPacket ConnectReasonCode = 129
ConnectReasonCodeProtocolError ConnectReasonCode = 130
ConnectReasonCodeImplErorr ConnectReasonCode = 131
ConnectReasonCodeUnsupportedProtoVer ConnectReasonCode = 132
ConnectReasonCodeClientIDNotValid ConnectReasonCode = 133
ConnectReasonCodeBadUsernameOrPassword ConnectReasonCode = 134
ConnectReasonCodeNotAuthorized ConnectReasonCode = 135
ConnectReasonCodeServerUnavaliable ConnectReasonCode = 136
ConnectReasonCodeServerBusy ConnectReasonCode = 137
ConnectReasonCodeBanned ConnectReasonCode = 138
ConnectReasonCodeBadAuthenticationMethod ConnectReasonCode = 140
ConnectReasonCodeTopicNameInvalid ConnectReasonCode = 144
ConnectReasonCodePacketTooLarge ConnectReasonCode = 149
ConnectReasonCodeQuotaExceeded ConnectReasonCode = 151
ConnectReasonCodePayloadFormatInvalid ConnectReasonCode = 153
ConnectReasonCodeRetainNotSupported ConnectReasonCode = 154
ConnectReasonCodeQoSNotSupported ConnectReasonCode = 155
ConnectReasonCodeUseAnotherServer ConnectReasonCode = 156
ConnectReasonCodeServerMoved ConnectReasonCode = 157
ConnectReasonCodeConnectionRateExceeded ConnectReasonCode = 159
2021-08-26 15:08:24 +02:00
)
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)
}