2024-04-06 16:38:14 -06:00
|
|
|
package pnyx
|
|
|
|
|
|
|
|
import (
|
2024-04-08 11:28:52 -06:00
|
|
|
"slices"
|
2024-04-09 17:08:46 -06:00
|
|
|
"fmt"
|
2024-04-06 16:38:14 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type ChannelID uint32
|
|
|
|
|
2024-04-08 11:28:52 -06:00
|
|
|
const (
|
2024-04-09 17:08:46 -06:00
|
|
|
MODE_CHANNEL ModeID = iota
|
|
|
|
MODE_RAW
|
2024-04-08 11:28:52 -06:00
|
|
|
|
2024-04-09 17:08:46 -06:00
|
|
|
CHANNEL_JOIN byte = iota
|
|
|
|
CHANNEL_LEAVE
|
|
|
|
CHANNEL_MEMBERS
|
2024-04-08 11:28:52 -06:00
|
|
|
|
2024-04-09 17:08:46 -06:00
|
|
|
RAW_DATA = iota
|
2024-04-08 11:28:52 -06:00
|
|
|
)
|
2024-04-06 16:38:14 -06:00
|
|
|
|
|
|
|
type ModeID uint8
|
|
|
|
type CommandID uint8
|
2024-04-09 17:08:46 -06:00
|
|
|
type Permission string
|
2024-04-06 16:38:14 -06:00
|
|
|
|
|
|
|
type Channel struct {
|
2024-04-09 17:08:46 -06:00
|
|
|
id ChannelID
|
2024-04-06 16:38:14 -06:00
|
|
|
modes map[ModeID]Mode
|
2024-04-09 17:08:46 -06:00
|
|
|
sessions []SessionID
|
|
|
|
}
|
|
|
|
|
|
|
|
func(channel *Channel) Data(session *Session, mode ModeID, data []byte) []SendPacket {
|
|
|
|
m, has_mode := channel.modes[mode]
|
|
|
|
if has_mode == false {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return m.Data(session, channel, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func(channel *Channel) Command(session *Session, mode ModeID, command byte, data []byte) ([]SendPacket, error) {
|
|
|
|
if mode == MODE_CHANNEL {
|
|
|
|
switch command {
|
|
|
|
case CHANNEL_JOIN:
|
|
|
|
if slices.Contains(channel.sessions, session.ID) {
|
|
|
|
return nil, fmt.Errorf("Session %s already in channel %d, can't join", session.ID, channel.id)
|
|
|
|
} else {
|
|
|
|
channel.sessions = append(channel.sessions, session.ID)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
case CHANNEL_LEAVE:
|
|
|
|
idx := slices.Index(channel.sessions, session.ID)
|
|
|
|
if idx == -1 {
|
|
|
|
return nil, fmt.Errorf("Session %s not in channel %d, can't leave", session.ID, channel.id)
|
|
|
|
} else {
|
|
|
|
channel.sessions = slices.Delete(channel.sessions, idx, idx+1)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Unknown MODE_CHANNEL command: 0x%02x", command)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mode, has_mode := channel.modes[mode]
|
|
|
|
if has_mode == false {
|
|
|
|
return nil, fmt.Errorf("Channel has no mode 0x%02x", mode)
|
|
|
|
} else {
|
|
|
|
return mode.Command(session, channel, command, data)
|
|
|
|
}
|
|
|
|
}
|
2024-04-08 11:28:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type SendPacket struct {
|
|
|
|
Packet *Packet
|
|
|
|
Session SessionID
|
2024-04-06 16:38:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type Mode interface {
|
2024-04-08 11:28:52 -06:00
|
|
|
// Process takes incoming packets from a session and returns a list of packets to send
|
2024-04-09 17:08:46 -06:00
|
|
|
Command(session *Session, channel *Channel, command byte, data []byte) ([]SendPacket, error)
|
|
|
|
Data(session *Session, channel *Channel, data []byte) []SendPacket
|
2024-04-08 11:28:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func multiplex(session *Session, packet *Packet, sessions []SessionID) []SendPacket {
|
|
|
|
send_packets := make([]SendPacket, len(sessions))
|
|
|
|
for i, session_id := range(sessions) {
|
|
|
|
if session_id == session.ID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
send_packets[i] = SendPacket{
|
|
|
|
Packet: packet,
|
|
|
|
Session: session_id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return send_packets
|
|
|
|
}
|
|
|
|
|
|
|
|
type RawMode struct {
|
|
|
|
}
|
|
|
|
|
2024-04-09 17:08:46 -06:00
|
|
|
func(mode *RawMode) Command(session *Session, channel *Channel, command byte, data []byte) ([]SendPacket, error) {
|
|
|
|
return nil, fmt.Errorf("unknown raw mode command 0x%02x", command)
|
|
|
|
}
|
2024-04-08 17:23:55 -06:00
|
|
|
|
2024-04-09 17:08:46 -06:00
|
|
|
func(mode *RawMode) Data(session *Session, channel *Channel, data []byte) []SendPacket {
|
|
|
|
if slices.Contains(channel.sessions, session.ID) {
|
|
|
|
new_packet := NewChannelPeerPacket(session.Peer, channel.id, MODE_RAW, data)
|
|
|
|
return multiplex(session, new_packet, channel.sessions)
|
2024-04-08 11:28:52 -06:00
|
|
|
}
|
|
|
|
return nil
|
2024-04-06 16:38:14 -06:00
|
|
|
}
|