pnyx/packet.go

38 lines
831 B
Go

2024-04-03 18:52:04 -06:00
package pnyx
import (
"encoding/binary"
"fmt"
2024-04-03 19:31:58 -06:00
)
type Packet struct {
Channel ChannelID
Mode ModeID
Command byte
Data []byte
}
func(packet Packet) String() string {
return fmt.Sprintf("{Channel: %x, Mode: %x, Data: %x}", packet.Channel, packet.Mode, packet.Data)
}
2024-04-03 18:52:04 -06:00
func(packet Packet) MarshalBinary() ([]byte, error) {
p := binary.BigEndian.AppendUint32(nil, uint32(packet.Channel))
p = append(p, byte(packet.Mode))
p = append(p, byte(packet.Command))
return append(p, packet.Data...), nil
}
2024-04-03 18:52:04 -06:00
func ParsePacket(data []byte) (*Packet, error) {
if len(data) < 6 {
return nil, fmt.Errorf("Not enough bytes to parse Packet(%d/%d)", len(data), 6)
}
return &Packet{
Channel: ChannelID(binary.BigEndian.Uint32(data)),
Mode: ModeID(data[4]),
Command: data[5],
Data: data[6:],
}, nil
2024-04-03 18:52:04 -06:00
}