2024-04-03 18:52:04 -06:00
|
|
|
package pnyx
|
|
|
|
|
|
|
|
import (
|
2024-04-09 17:08:46 -06:00
|
|
|
"encoding"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2024-04-03 19:31:58 -06:00
|
|
|
)
|
|
|
|
|
2024-04-09 17:08:46 -06:00
|
|
|
type PacketType uint8
|
|
|
|
const (
|
2024-04-12 18:06:57 -06:00
|
|
|
PACKET_SERVER_COMMAND PacketType = iota
|
|
|
|
PACKET_CHANNEL_COMMAND
|
2024-04-09 17:08:46 -06:00
|
|
|
PACKET_CHANNEL_DATA
|
|
|
|
PACKET_CHANNEL_PEER
|
|
|
|
|
2024-04-12 18:06:57 -06:00
|
|
|
CHANNEL_HEADER_LEN int = 2
|
2024-04-11 21:05:50 -06:00
|
|
|
CHANNEL_COMMAND_LEN = CHANNEL_HEADER_LEN + COMMAND_LENGTH + REQ_ID_LENGTH
|
|
|
|
CHANNEL_PEER_LEN = CHANNEL_HEADER_LEN + PEER_ID_LENGTH
|
2024-04-12 18:06:57 -06:00
|
|
|
|
|
|
|
SERVER_COMMAND_ADD_CHANNEL byte = iota
|
|
|
|
SERVER_COMMAND_DEL_CHANNEL
|
2024-04-09 17:08:46 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type Payload interface {
|
|
|
|
encoding.BinaryMarshaler
|
|
|
|
}
|
|
|
|
|
2024-04-08 11:28:52 -06:00
|
|
|
type Packet struct {
|
2024-04-09 17:08:46 -06:00
|
|
|
Type PacketType
|
|
|
|
Payload Payload
|
|
|
|
}
|
|
|
|
|
|
|
|
func(packet Packet) MarshalBinary() ([]byte, error) {
|
|
|
|
payload, err := packet.Payload.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return append([]byte{byte(packet.Type)}, payload...), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParsePacket(data []byte) (Payload, error) {
|
|
|
|
if len(data) < 1 {
|
|
|
|
return nil, fmt.Errorf("Packet too short to parse - %d/1", len(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
switch PacketType(data[0]) {
|
2024-04-12 18:06:57 -06:00
|
|
|
case PACKET_SERVER_COMMAND:
|
|
|
|
return ParseServerCommandPacket(data[1:])
|
2024-04-09 17:08:46 -06:00
|
|
|
case PACKET_CHANNEL_DATA:
|
|
|
|
return ParseChannelDataPacket(data[1:])
|
|
|
|
case PACKET_CHANNEL_COMMAND:
|
|
|
|
return ParseChannelCommandPacket(data[1:])
|
|
|
|
case PACKET_CHANNEL_PEER:
|
|
|
|
return ParseChannelPeerPacket(data[1:])
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Don't know how to parse packet type 0x%02x", data[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-12 18:06:57 -06:00
|
|
|
type ServerCommandPacket struct {
|
|
|
|
ReqID uuid.UUID
|
|
|
|
Command byte
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (packet ServerCommandPacket) MarshalBinary() ([]byte, error) {
|
|
|
|
p := make([]byte, 17 + len(packet.Data))
|
|
|
|
copy(p, packet.ReqID[:])
|
|
|
|
p[16] = packet.Command
|
|
|
|
copy(p[17:], packet.Data)
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServerCommandPacket(command byte, data []byte) (*Packet, uuid.UUID) {
|
|
|
|
req_id := uuid.New()
|
|
|
|
return &Packet{
|
|
|
|
Type: PACKET_SERVER_COMMAND,
|
|
|
|
Payload: ServerCommandPacket{
|
|
|
|
ReqID: req_id,
|
|
|
|
Command: command,
|
|
|
|
Data: data,
|
|
|
|
},
|
|
|
|
}, req_id
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseServerCommandPacket(data []byte) (ServerCommandPacket, error) {
|
|
|
|
if len(data) < 17 {
|
|
|
|
return ServerCommandPacket{}, fmt.Errorf("Not enough data to decode ServerCommandPacket: %d/%d", len(data), 17)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ServerCommandPacket{
|
|
|
|
ReqID: uuid.UUID(data[0:16]),
|
|
|
|
Command: data[16],
|
|
|
|
Data: data[17:],
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-04-09 17:08:46 -06:00
|
|
|
type ChannelHeader struct {
|
2024-04-08 11:28:52 -06:00
|
|
|
Channel ChannelID
|
|
|
|
Mode ModeID
|
2024-04-09 17:08:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func(packet ChannelHeader) MarshalBinary() ([]byte, error) {
|
2024-04-12 18:06:57 -06:00
|
|
|
return []byte{byte(packet.Channel), byte(packet.Mode)}, nil
|
2024-04-09 17:08:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func ParseChannelHeader(data []byte) (ChannelHeader, error) {
|
2024-04-12 18:06:57 -06:00
|
|
|
if len(data) < 2 {
|
2024-04-09 17:08:46 -06:00
|
|
|
return ChannelHeader{}, fmt.Errorf("Not enough bytes to parse ChannelPacket(%d/%d)", len(data), 6)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ChannelHeader{
|
2024-04-12 18:06:57 -06:00
|
|
|
Channel: ChannelID(data[0]),
|
|
|
|
Mode: ModeID(data[1]),
|
2024-04-09 17:08:46 -06:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ChannelCommandPacket struct {
|
|
|
|
ChannelHeader
|
2024-04-08 11:28:52 -06:00
|
|
|
Command byte
|
2024-04-09 17:08:46 -06:00
|
|
|
ReqID uuid.UUID
|
2024-04-08 11:28:52 -06:00
|
|
|
Data []byte
|
2024-04-06 16:38:14 -06:00
|
|
|
}
|
|
|
|
|
2024-04-12 18:06:57 -06:00
|
|
|
func NewChannelCommandPacket(request_id uuid.UUID, channel ChannelID, mode ModeID, command byte, data []byte) *Packet {
|
2024-04-09 17:08:46 -06:00
|
|
|
return &Packet{
|
|
|
|
Type: PACKET_CHANNEL_COMMAND,
|
|
|
|
Payload: ChannelCommandPacket{
|
|
|
|
ChannelHeader: ChannelHeader{
|
|
|
|
Channel: channel,
|
|
|
|
Mode: mode,
|
|
|
|
},
|
|
|
|
Command: command,
|
|
|
|
ReqID: request_id,
|
|
|
|
Data: data,
|
|
|
|
},
|
2024-04-12 18:06:57 -06:00
|
|
|
}
|
2024-04-06 16:38:14 -06:00
|
|
|
}
|
2024-04-03 18:52:04 -06:00
|
|
|
|
2024-04-09 17:08:46 -06:00
|
|
|
func(packet ChannelCommandPacket) MarshalBinary() ([]byte, error) {
|
|
|
|
header, err := packet.ChannelHeader.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-04-12 18:06:57 -06:00
|
|
|
data := make([]byte, len(header) + len(packet.Data) + REQ_ID_LENGTH + COMMAND_LENGTH)
|
|
|
|
copy(data, header)
|
|
|
|
data[CHANNEL_HEADER_LEN] = packet.Command
|
|
|
|
copy(data[CHANNEL_HEADER_LEN + COMMAND_LENGTH:], packet.ReqID[:])
|
|
|
|
copy(data[CHANNEL_HEADER_LEN + COMMAND_LENGTH + REQ_ID_LENGTH:], packet.Data)
|
|
|
|
return data, nil
|
2024-04-06 16:38:14 -06:00
|
|
|
}
|
2024-04-03 18:52:04 -06:00
|
|
|
|
2024-04-09 17:08:46 -06:00
|
|
|
func ParseChannelCommandPacket(data []byte) (ChannelCommandPacket, error) {
|
|
|
|
if len(data) < CHANNEL_COMMAND_LEN {
|
|
|
|
return ChannelCommandPacket{}, fmt.Errorf("Not enough data to decode channel command packet %d/%d", len(data), CHANNEL_COMMAND_LEN)
|
|
|
|
}
|
|
|
|
|
|
|
|
header, err := ParseChannelHeader(data[:CHANNEL_HEADER_LEN])
|
|
|
|
if err != nil {
|
|
|
|
return ChannelCommandPacket{}, err
|
2024-04-06 16:38:14 -06:00
|
|
|
}
|
|
|
|
|
2024-04-09 17:08:46 -06:00
|
|
|
command := data[CHANNEL_HEADER_LEN]
|
|
|
|
request_id := uuid.UUID(data[CHANNEL_HEADER_LEN+COMMAND_LENGTH:])
|
|
|
|
return ChannelCommandPacket{
|
|
|
|
ChannelHeader: header,
|
|
|
|
Command: command,
|
|
|
|
ReqID: request_id,
|
|
|
|
Data: data[CHANNEL_COMMAND_LEN:],
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ChannelPeerPacket struct {
|
|
|
|
ChannelHeader
|
|
|
|
Peer PeerID
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewChannelPeerPacket(peer PeerID, channel ChannelID, mode ModeID, data []byte) *Packet {
|
2024-04-08 11:28:52 -06:00
|
|
|
return &Packet{
|
2024-04-09 17:08:46 -06:00
|
|
|
Type: PACKET_CHANNEL_PEER,
|
|
|
|
Payload: ChannelPeerPacket{
|
|
|
|
ChannelHeader: ChannelHeader{
|
|
|
|
Channel: channel,
|
|
|
|
Mode: mode,
|
|
|
|
},
|
|
|
|
Peer: peer,
|
|
|
|
Data: data,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func(packet ChannelPeerPacket) MarshalBinary() ([]byte, error) {
|
|
|
|
header, err := packet.ChannelHeader.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
data := append(header, packet.Peer[:]...)
|
|
|
|
return append(data, packet.Data...), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseChannelPeerPacket(data []byte) (ChannelPeerPacket, error) {
|
|
|
|
if len(data) < CHANNEL_PEER_LEN {
|
2024-04-11 21:05:50 -06:00
|
|
|
return ChannelPeerPacket{}, fmt.Errorf("Not enough bytes to parse ServerChannelPacket: %d/%d", len(data), PEER_ID_LENGTH)
|
2024-04-09 17:08:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
header, err := ParseChannelHeader(data)
|
|
|
|
if err != nil {
|
|
|
|
return ChannelPeerPacket{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ChannelPeerPacket{
|
|
|
|
ChannelHeader: header,
|
|
|
|
Peer: PeerID(data[CHANNEL_HEADER_LEN:]),
|
|
|
|
Data: data[CHANNEL_PEER_LEN:],
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type ChannelDataPacket struct {
|
|
|
|
ChannelHeader
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewChannelDataPacket(channel ChannelID, mode ModeID, data []byte) *Packet {
|
|
|
|
return &Packet{
|
|
|
|
Type: PACKET_CHANNEL_DATA,
|
|
|
|
Payload: ChannelDataPacket{
|
|
|
|
ChannelHeader: ChannelHeader{
|
|
|
|
Channel: channel,
|
|
|
|
Mode: mode,
|
|
|
|
},
|
|
|
|
Data: data,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func(packet ChannelDataPacket) MarshalBinary() ([]byte, error) {
|
|
|
|
header, err := packet.ChannelHeader.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return append(header, packet.Data...), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseChannelDataPacket(data []byte) (ChannelDataPacket, error) {
|
|
|
|
if len(data) < CHANNEL_HEADER_LEN {
|
|
|
|
return ChannelDataPacket{}, fmt.Errorf("Not enough data to parse ChannelDataPacket")
|
|
|
|
}
|
|
|
|
|
|
|
|
header, err := ParseChannelHeader(data)
|
|
|
|
if err != nil {
|
|
|
|
return ChannelDataPacket{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ChannelDataPacket{
|
|
|
|
ChannelHeader: header,
|
|
|
|
Data: data[CHANNEL_HEADER_LEN:],
|
2024-04-08 11:28:52 -06:00
|
|
|
}, nil
|
2024-04-03 18:52:04 -06:00
|
|
|
}
|