2021-08-26 15:08:24 +02:00
|
|
|
package packets
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"badat.dev/maeqtt/v2/mqtt/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PacketVisitor interface {
|
2021-09-28 12:30:32 +02:00
|
|
|
VisitConnect(ConnectPacket)
|
|
|
|
VisitPublish(PublishPacket)
|
|
|
|
VisitDisconnect(DisconnectPacket)
|
|
|
|
VisitSubscribe(SubscribePacket)
|
|
|
|
VisitUnsubscribe(UnsubscribePacket)
|
|
|
|
VisitPing(PingreqPacket)
|
|
|
|
VisitPubackPacket(PubackPacket)
|
|
|
|
VisitPubrecPacket(PubrecPacket)
|
|
|
|
VisitPubrelPacket(PubrelPacket)
|
|
|
|
VisitPubcompPacket(PubcompPacket)
|
2021-08-26 15:08:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type ClientPacket interface {
|
2021-09-28 12:30:32 +02:00
|
|
|
Visit(PacketVisitor)
|
2021-08-26 15:08:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type ServerPacket interface {
|
2021-09-28 12:30:32 +02:00
|
|
|
Write(w io.Writer) error
|
2021-08-26 15:08:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type PacketType byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
PacketTypeReserved PacketType = 0 // Forbidden
|
2021-10-17 21:09:44 +02:00
|
|
|
PacketTypeConnect PacketType = 1
|
|
|
|
PacketTypeConnack PacketType = 2
|
|
|
|
PacketTypePublish PacketType = 3
|
|
|
|
PacketTypePuback PacketType = 4
|
|
|
|
PacketTypePubrec PacketType = 5
|
|
|
|
PacketTypePubrel PacketType = 6
|
|
|
|
PacketTypePubcomp PacketType = 7
|
|
|
|
PacketTypeSubscribe PacketType = 8
|
|
|
|
PacketTypeSuback PacketType = 9
|
|
|
|
PacketTypeUnsubscribe PacketType = 10
|
|
|
|
PacketTypeUnsuback PacketType = 11
|
|
|
|
PacketTypePingreq PacketType = 12
|
|
|
|
PacketTypePingresp PacketType = 13
|
|
|
|
PacketTypeDisconnect PacketType = 14
|
|
|
|
PacketTypeAuth PacketType = 15
|
2021-08-26 15:08:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func ReadPacket(r *bufio.Reader) (*ClientPacket, error) {
|
|
|
|
fixedHeader, err := r.ReadByte()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
highestFourBits := uint((fixedHeader >> 4) & 0b1111)
|
|
|
|
lowerFourBits := uint(fixedHeader & 0b1111)
|
|
|
|
|
|
|
|
dataLength, err := types.DecodeVariableByteInt(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
reader := io.LimitReader(r, int64(dataLength))
|
2021-10-01 22:18:48 +02:00
|
|
|
|
2021-08-26 15:08:24 +02:00
|
|
|
control := controlPacket{
|
|
|
|
packetType: PacketType(highestFourBits),
|
|
|
|
flags: lowerFourBits,
|
|
|
|
reader: reader,
|
|
|
|
}
|
|
|
|
|
|
|
|
var packet ClientPacket
|
|
|
|
switch control.packetType {
|
|
|
|
case PacketTypeConnect:
|
|
|
|
packet, err = parseConnectPacket(control)
|
|
|
|
case PacketTypePublish:
|
|
|
|
packet, err = parsePublishPacket(control)
|
|
|
|
case PacketTypeDisconnect:
|
|
|
|
packet, err = parseDisconnectPacket(control)
|
2021-09-28 12:30:32 +02:00
|
|
|
case PacketTypeSubscribe:
|
|
|
|
packet, err = parseSubscribePacket(control)
|
|
|
|
case PacketTypeUnsubscribe:
|
|
|
|
packet, err = parseUnsubscribePacket(control)
|
|
|
|
case PacketTypePingreq:
|
|
|
|
packet, err = parsePingreq(control)
|
|
|
|
case PacketTypePuback:
|
|
|
|
panic("Puback packet parsing unimplemented")
|
|
|
|
case PacketTypePubrec:
|
|
|
|
panic("Pubrec packet parsing unimplemented")
|
|
|
|
case PacketTypePubrel:
|
|
|
|
panic("Pubrel packet parsing unimplemented")
|
|
|
|
case PacketTypePubcomp:
|
|
|
|
panic("Pubcomp packet parsing unimplemented")
|
2021-08-26 15:08:24 +02:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Unknown packet type %v", control.packetType)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &packet, err
|
|
|
|
}
|