mirror of
https://github.com/mochi-mqtt/server.git
synced 2025-10-16 05:00:38 +08:00
expose packets library
This commit is contained in:
@@ -1,59 +0,0 @@
|
||||
package packets
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
)
|
||||
|
||||
// FixedHeader contains the values of the fixed header portion of the MQTT packet.
|
||||
type FixedHeader struct {
|
||||
Type byte // the type of the packet (PUBLISH, SUBSCRIBE, etc) from bits 7 - 4 (byte 1).
|
||||
Dup bool // indicates if the packet was already sent at an earlier time.
|
||||
Qos byte // indicates the quality of service expected.
|
||||
Retain bool // whether the message should be retained.
|
||||
Remaining int // the number of remaining bytes in the payload.
|
||||
}
|
||||
|
||||
// Encode encodes the FixedHeader and returns a bytes buffer.
|
||||
func (fh *FixedHeader) Encode(buf *bytes.Buffer) {
|
||||
buf.WriteByte(fh.Type<<4 | encodeBool(fh.Dup)<<3 | fh.Qos<<1 | encodeBool(fh.Retain))
|
||||
encodeLength(buf, fh.Remaining)
|
||||
}
|
||||
|
||||
// decode extracts the specification bits from the header byte.
|
||||
func (fh *FixedHeader) Decode(headerByte byte) error {
|
||||
fh.Type = headerByte >> 4 // Get the message type from the first 4 bytes.
|
||||
|
||||
switch fh.Type {
|
||||
case Publish:
|
||||
fh.Dup = (headerByte>>3)&0x01 > 0 // Extract flags. Check if message is duplicate.
|
||||
fh.Qos = (headerByte >> 1) & 0x03 // Extract QoS flag.
|
||||
fh.Retain = headerByte&0x01 > 0 // Extract retain flag.
|
||||
case Pubrel:
|
||||
fh.Qos = (headerByte >> 1) & 0x03
|
||||
case Subscribe:
|
||||
fh.Qos = (headerByte >> 1) & 0x03
|
||||
case Unsubscribe:
|
||||
fh.Qos = (headerByte >> 1) & 0x03
|
||||
default:
|
||||
if (headerByte>>3)&0x01 > 0 || (headerByte>>1)&0x03 > 0 || headerByte&0x01 > 0 {
|
||||
return ErrInvalidFlags
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// encodeLength writes length bits for the header.
|
||||
func encodeLength(buf *bytes.Buffer, length int) {
|
||||
for {
|
||||
digit := byte(length % 128)
|
||||
length /= 128
|
||||
if length > 0 {
|
||||
digit |= 0x80
|
||||
}
|
||||
buf.WriteByte(digit)
|
||||
if length == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user