maeqtt/mqtt/packets/ControlPacket.go
2021-10-17 21:01:13 +02:00

38 lines
649 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
}
err = types.WriteDataWithVarIntLen(buf, data)
if err != nil {
return err
}
_, err = w.Write(buf.Bytes())
if err != nil {
return err
}
return nil
}