2024-04-03 18:52:04 -06:00
|
|
|
package pnyx
|
|
|
|
|
|
|
|
import (
|
2024-04-08 11:28:52 -06:00
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
2024-04-03 19:31:58 -06:00
|
|
|
)
|
|
|
|
|
2024-04-08 11:28:52 -06:00
|
|
|
type Packet struct {
|
|
|
|
Channel ChannelID
|
|
|
|
Mode ModeID
|
|
|
|
Command byte
|
|
|
|
Data []byte
|
2024-04-06 16:38:14 -06:00
|
|
|
}
|
|
|
|
|
2024-04-08 11:28:52 -06:00
|
|
|
func(packet Packet) String() string {
|
|
|
|
return fmt.Sprintf("{Channel: %x, Mode: %x, Data: %x}", packet.Channel, packet.Mode, packet.Data)
|
2024-04-06 16:38:14 -06:00
|
|
|
}
|
2024-04-03 18:52:04 -06:00
|
|
|
|
2024-04-08 11:28:52 -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-06 16:38:14 -06:00
|
|
|
}
|
2024-04-03 18:52:04 -06:00
|
|
|
|
2024-04-08 11:28:52 -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)
|
2024-04-06 16:38:14 -06:00
|
|
|
}
|
|
|
|
|
2024-04-08 11:28:52 -06:00
|
|
|
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
|
|
|
}
|