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

32 lines
568 B
Go

package packets
import (
"bytes"
"io"
"badat.dev/maeqtt/v2/mqtt/types"
)
type controlPacket struct {
packetType PacketType
flags uint
reader io.Reader
}
func (c controlPacket) write(w io.Writer) error {
buf := bytes.NewBuffer([]byte{})
var fixedHeader byte = (byte(c.packetType) << 4 & 0b11110000) + (byte(c.flags) & 0b1111)
_, err := buf.Write([]byte{fixedHeader})
if err != nil {
return err
}
data, err := io.ReadAll(c.reader)
if err != nil {
return err
}
types.WriteDataWithVarIntLen(buf, data)
w.Write(buf.Bytes())
return nil
}