2024-04-03 18:52:04 -06:00
|
|
|
package pnyx
|
|
|
|
|
|
|
|
import (
|
2024-04-06 16:38:14 -06:00
|
|
|
"crypto/ed25519"
|
|
|
|
"crypto/rand"
|
2024-04-03 18:52:04 -06:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2024-04-06 16:38:14 -06:00
|
|
|
"os"
|
2024-04-13 14:00:56 -06:00
|
|
|
"slices"
|
2024-04-08 11:28:52 -06:00
|
|
|
"sync"
|
2024-04-03 18:52:04 -06:00
|
|
|
"sync/atomic"
|
2024-04-13 14:00:56 -06:00
|
|
|
"time"
|
2024-04-03 18:52:04 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
SERVER_UDP_BUFFER_SIZE = 2048
|
2024-04-08 11:28:52 -06:00
|
|
|
SERVER_SEND_BUFFER_SIZE = 2048
|
2024-04-16 15:06:53 -06:00
|
|
|
SERVER_COMMAND_BUFFER_SIZE = 2048
|
2024-04-03 18:52:04 -06:00
|
|
|
)
|
|
|
|
|
2024-04-09 17:08:46 -06:00
|
|
|
type RoleID uint32
|
|
|
|
|
2024-04-08 11:28:52 -06:00
|
|
|
type ServerSession struct {
|
|
|
|
Session
|
2024-04-16 15:34:53 -06:00
|
|
|
active atomic.Bool
|
2024-04-08 17:23:55 -06:00
|
|
|
LastSeen time.Time
|
2024-04-08 11:28:52 -06:00
|
|
|
IncomingPackets chan[]byte
|
2024-04-16 15:06:53 -06:00
|
|
|
OutgoingPackets chan Payload
|
2024-04-13 14:00:56 -06:00
|
|
|
Channels []ChannelID
|
2024-04-03 18:52:04 -06:00
|
|
|
}
|
|
|
|
|
2024-04-16 15:34:53 -06:00
|
|
|
func(session *ServerSession) Send(payload Payload) {
|
|
|
|
if session.active.Load() {
|
|
|
|
session.OutgoingPackets <- payload
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-03 18:52:04 -06:00
|
|
|
type Server struct {
|
2024-04-06 16:38:14 -06:00
|
|
|
key ed25519.PrivateKey
|
2024-04-03 18:52:04 -06:00
|
|
|
active atomic.Bool
|
|
|
|
connection *net.UDPConn
|
|
|
|
stopped chan error
|
2024-04-16 15:06:53 -06:00
|
|
|
commands chan Payload
|
2024-04-08 11:28:52 -06:00
|
|
|
|
|
|
|
sessions_lock sync.Mutex
|
|
|
|
sessions map[SessionID]*ServerSession
|
|
|
|
|
2024-04-16 15:06:53 -06:00
|
|
|
channels atomic.Value
|
2024-04-09 17:08:46 -06:00
|
|
|
|
|
|
|
peers map[PeerID][]RoleID
|
2024-04-03 18:52:04 -06:00
|
|
|
}
|
|
|
|
|
2024-04-16 15:06:53 -06:00
|
|
|
func NewServer(key ed25519.PrivateKey, channels map[ChannelID]*Channel) (*Server, error) {
|
2024-04-06 16:38:14 -06:00
|
|
|
if key == nil {
|
|
|
|
var err error
|
|
|
|
_, key, err = ed25519.GenerateKey(rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2024-04-03 18:52:04 -06:00
|
|
|
server := &Server{
|
2024-04-06 16:38:14 -06:00
|
|
|
key: key,
|
2024-04-03 18:52:04 -06:00
|
|
|
connection: nil,
|
|
|
|
active: atomic.Bool{},
|
|
|
|
stopped: make(chan error, 0),
|
2024-04-16 15:06:53 -06:00
|
|
|
commands: make(chan Payload, SERVER_COMMAND_BUFFER_SIZE),
|
2024-04-08 11:28:52 -06:00
|
|
|
|
|
|
|
sessions: map[SessionID]*ServerSession{},
|
2024-04-16 15:06:53 -06:00
|
|
|
channels: atomic.Value{},
|
2024-04-09 17:08:46 -06:00
|
|
|
|
|
|
|
peers: map[PeerID][]RoleID{},
|
2024-04-03 18:52:04 -06:00
|
|
|
}
|
2024-04-16 15:06:53 -06:00
|
|
|
server.channels.Store(channels)
|
2024-04-03 18:52:04 -06:00
|
|
|
server.active.Store(false)
|
2024-04-06 16:38:14 -06:00
|
|
|
return server, nil
|
|
|
|
}
|
|
|
|
|
2024-04-08 11:28:52 -06:00
|
|
|
func(server *Server) Log(format string, fields ...interface{}) {
|
2024-04-03 18:52:04 -06:00
|
|
|
fmt.Fprint(os.Stderr, fmt.Sprintf(format, fields...) + "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func(server *Server) Stop() error {
|
|
|
|
was_active := server.active.CompareAndSwap(true, false)
|
|
|
|
if was_active {
|
|
|
|
err := server.connection.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return <-server.stopped
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Called stop func on stopped server")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-08 11:28:52 -06:00
|
|
|
const SESSION_BUFFER_SIZE = 256
|
|
|
|
|
2024-04-11 21:05:50 -06:00
|
|
|
func handle_session_outgoing(session *ServerSession, server *Server) {
|
|
|
|
server.Log("Starting session outgoing goroutine %s", session.ID)
|
2024-04-16 15:34:53 -06:00
|
|
|
for session.active.Load() {
|
2024-04-11 21:05:50 -06:00
|
|
|
packet := <- session.OutgoingPackets
|
|
|
|
if packet == nil {
|
|
|
|
break
|
|
|
|
}
|
2024-04-03 18:52:04 -06:00
|
|
|
|
2024-04-11 21:05:50 -06:00
|
|
|
if session.remote == nil {
|
|
|
|
server.Log("SESSION_DATA_OUT(%s) error - no remote to send to", session.ID)
|
|
|
|
continue
|
|
|
|
}
|
2024-04-06 16:38:14 -06:00
|
|
|
|
2024-04-11 21:05:50 -06:00
|
|
|
packet_data, err := packet.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
server.Log("SESSION_DATA_OUT(%s) marshal error - %s", session.ID, err)
|
|
|
|
continue
|
|
|
|
}
|
2024-04-09 17:20:37 -06:00
|
|
|
|
2024-04-11 21:05:50 -06:00
|
|
|
encrypted, err := NewSessionData(&session.Session, packet_data)
|
|
|
|
if err != nil {
|
|
|
|
server.Log("SESSION_DATA_OUT(%s) error - %s", session.ID, err)
|
|
|
|
continue
|
|
|
|
}
|
2024-04-09 17:20:37 -06:00
|
|
|
|
2024-04-11 21:05:50 -06:00
|
|
|
_, err = server.connection.WriteToUDP(encrypted, session.remote)
|
|
|
|
if err != nil {
|
|
|
|
server.Log("SESSION_DATA_OUT(%s) write error - %s", session.ID, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
server.Log("Stopping session outgoing goroutine %s", session.ID)
|
|
|
|
}
|
2024-04-09 17:20:37 -06:00
|
|
|
|
2024-04-16 15:06:53 -06:00
|
|
|
const SESSION_PING_TIME = time.Minute
|
|
|
|
const SESSION_TIMEOUT = 2 * time.Minute
|
|
|
|
|
|
|
|
type SessionChannelCommand struct {
|
|
|
|
Session *ServerSession
|
|
|
|
Packet *ChannelCommandPacket
|
|
|
|
}
|
|
|
|
|
2024-04-11 21:05:50 -06:00
|
|
|
func handle_session_incoming(session *ServerSession, server *Server) {
|
|
|
|
server.Log("Starting session incoming goroutine %s", session.ID)
|
2024-04-16 15:06:53 -06:00
|
|
|
ping_timer := time.After(SESSION_PING_TIME)
|
2024-04-16 15:34:53 -06:00
|
|
|
for session.active.Load() {
|
2024-04-16 15:06:53 -06:00
|
|
|
select {
|
|
|
|
case <- ping_timer:
|
2024-04-18 20:10:01 -06:00
|
|
|
if time.Now().Add(-1*SESSION_TIMEOUT).Compare(session.LastSeen) != -1 {
|
|
|
|
server.Log("Closing %s after being inactive since %s", session.ID, session.LastSeen)
|
2024-04-16 15:06:53 -06:00
|
|
|
server.sessions_lock.Lock()
|
|
|
|
server.close_session(session)
|
|
|
|
server.sessions_lock.Unlock()
|
2024-04-23 14:16:25 -06:00
|
|
|
} else if time.Now().Add(-1*SESSION_PING_TIME).Compare(session.LastSeen) != -1 {
|
|
|
|
server.Log("Pinging %s after being inactive since %s", session.ID, session.LastSeen)
|
|
|
|
session.OutgoingPackets <- NewPingPacket()
|
|
|
|
ping_timer = time.After(SESSION_PING_TIME)
|
2024-04-16 15:06:53 -06:00
|
|
|
} else {
|
2024-04-18 20:10:01 -06:00
|
|
|
server.Log("%s passed keep-alive check, last seen %s", session.ID, session.LastSeen)
|
2024-04-16 15:06:53 -06:00
|
|
|
ping_timer = time.After(SESSION_PING_TIME)
|
|
|
|
}
|
|
|
|
case encrypted := <- session.IncomingPackets:
|
|
|
|
if encrypted == nil {
|
|
|
|
continue
|
|
|
|
}
|
2024-04-09 17:20:37 -06:00
|
|
|
|
2024-04-16 15:06:53 -06:00
|
|
|
data, err := ParseSessionData(&session.Session, encrypted)
|
|
|
|
if err != nil {
|
|
|
|
server.Log("SESSION_DATA_IN(%s) error - %s", session.ID, err)
|
|
|
|
continue
|
|
|
|
}
|
2024-04-23 16:38:08 -06:00
|
|
|
|
2024-04-16 15:06:53 -06:00
|
|
|
packet, err := ParsePacket(data)
|
|
|
|
if err != nil {
|
|
|
|
server.Log("SESSION_DATA_IN(%s) parse error - %s", session.ID, err)
|
|
|
|
}
|
2024-04-09 17:20:37 -06:00
|
|
|
|
2024-04-16 15:06:53 -06:00
|
|
|
switch packet := packet.(type) {
|
|
|
|
case CommandPacket:
|
|
|
|
server.commands<-packet
|
|
|
|
case ChannelCommandPacket:
|
|
|
|
channels := server.channels.Load().(map[ChannelID]*Channel)
|
|
|
|
channel, exists := channels[packet.Channel]
|
|
|
|
if exists == true {
|
|
|
|
channel.Commands<-SessionChannelCommand{
|
|
|
|
Session: session,
|
|
|
|
Packet: &packet,
|
2024-04-13 14:00:56 -06:00
|
|
|
}
|
2024-04-16 15:06:53 -06:00
|
|
|
} else {
|
|
|
|
server.Log("Command for unknown channel %d", packet.Channel)
|
2024-04-13 14:00:56 -06:00
|
|
|
}
|
2024-04-16 15:06:53 -06:00
|
|
|
case DataPacket:
|
|
|
|
channels := server.channels.Load().(map[ChannelID]*Channel)
|
|
|
|
channel, exists := channels[packet.Channel]
|
|
|
|
if exists == true {
|
|
|
|
members := channel.Members.Load().([]*ServerSession)
|
|
|
|
if slices.Contains(members, session) {
|
|
|
|
mode, has_mode := channel.Modes[packet.Mode]
|
|
|
|
if has_mode {
|
2024-04-16 15:34:53 -06:00
|
|
|
mode.Load().(Mode).Data(session, packet.Channel, members, packet.Data)
|
2024-04-13 14:00:56 -06:00
|
|
|
}
|
|
|
|
}
|
2024-04-16 15:06:53 -06:00
|
|
|
} else {
|
|
|
|
server.Log("Data for unknown channel %d", packet.Channel)
|
2024-04-13 14:00:56 -06:00
|
|
|
}
|
2024-04-09 17:20:37 -06:00
|
|
|
|
2024-04-16 15:06:53 -06:00
|
|
|
default:
|
|
|
|
server.Log("Unhandled packet type from session %s - 0x%02x", session.ID, err)
|
2024-04-11 21:05:50 -06:00
|
|
|
}
|
|
|
|
}
|
2024-04-09 17:20:37 -06:00
|
|
|
|
2024-04-11 21:05:50 -06:00
|
|
|
}
|
|
|
|
server.Log("Stopping session incoming goroutine %s", session.ID)
|
|
|
|
}
|
2024-04-09 17:20:37 -06:00
|
|
|
|
2024-04-11 21:05:50 -06:00
|
|
|
func(server *Server) handle_session_open(client_session_open []byte, from *net.UDPAddr) error {
|
|
|
|
session, session_opened, err := ParseSessionOpen(server.key, client_session_open)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-06 16:38:14 -06:00
|
|
|
|
2024-04-11 21:05:50 -06:00
|
|
|
server.sessions_lock.Lock()
|
|
|
|
server.sessions[session.ID] = &ServerSession{
|
|
|
|
Session: session,
|
|
|
|
LastSeen: time.Now(),
|
|
|
|
IncomingPackets: make(chan[]byte, SESSION_BUFFER_SIZE),
|
2024-04-16 15:06:53 -06:00
|
|
|
OutgoingPackets: make(chan Payload, SESSION_BUFFER_SIZE),
|
2024-04-11 21:05:50 -06:00
|
|
|
}
|
2024-04-16 15:34:53 -06:00
|
|
|
server.sessions[session.ID].active.Store(true)
|
2024-04-11 21:05:50 -06:00
|
|
|
server.sessions_lock.Unlock()
|
2024-04-09 17:20:37 -06:00
|
|
|
|
2024-04-11 21:05:50 -06:00
|
|
|
go handle_session_outgoing(server.sessions[session.ID], server)
|
|
|
|
go handle_session_incoming(server.sessions[session.ID], server)
|
2024-04-09 17:20:37 -06:00
|
|
|
|
2024-04-11 21:05:50 -06:00
|
|
|
_, err = server.connection.WriteToUDP(session_opened, from)
|
2024-04-09 17:20:37 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-11 21:05:50 -06:00
|
|
|
server.Log("Started session %s with %x", session.ID, session.Peer)
|
2024-04-09 17:20:37 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-11 21:05:50 -06:00
|
|
|
func(server *Server) handle_session_connect(session_connect []byte, from *net.UDPAddr) error {
|
|
|
|
session_id := SessionID(session_connect[:SESSION_ID_LENGTH])
|
2024-04-09 17:20:37 -06:00
|
|
|
session, exists := server.sessions[session_id]
|
|
|
|
if exists == false {
|
|
|
|
return fmt.Errorf("Session %s does not exist, can't connect", session_id)
|
|
|
|
}
|
|
|
|
|
2024-04-11 21:05:50 -06:00
|
|
|
session_connected, err := ParseSessionTimed(SESSION_CONNECTED, server.key, session_connect, &session.Session, time.Now().Add(-1*time.Second).UnixMilli())
|
2024-04-09 17:20:37 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: fix, was client_addr but the client doesnt know it's nat assignment
|
|
|
|
session.remote = from
|
|
|
|
session.LastSeen = time.Now()
|
|
|
|
|
2024-04-11 21:05:50 -06:00
|
|
|
_, err = server.connection.WriteToUDP(session_connected, session.remote)
|
2024-04-09 17:20:37 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
server.Log("Sent server_hello for %s to %s(from %s)", session.ID, session.remote, from)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-11 21:05:50 -06:00
|
|
|
func(server *Server) handle_session_close(session_close []byte, from *net.UDPAddr) error {
|
|
|
|
session_id := SessionID(session_close[:SESSION_ID_LENGTH])
|
2024-04-09 17:20:37 -06:00
|
|
|
session, exists := server.sessions[session_id]
|
|
|
|
if exists == false {
|
|
|
|
return fmt.Errorf("Session %s does not exist, can't close", session_id)
|
|
|
|
}
|
|
|
|
|
2024-04-11 21:05:50 -06:00
|
|
|
session_closed, err := ParseSessionTimed(SESSION_CLOSED, server.key, session_close, &session.Session, time.Now().Add(-1*time.Second).UnixMilli())
|
2024-04-09 17:20:37 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
server.sessions_lock.Lock()
|
2024-04-16 15:06:53 -06:00
|
|
|
server.close_session(session)
|
2024-04-09 17:20:37 -06:00
|
|
|
server.sessions_lock.Unlock()
|
|
|
|
|
2024-04-11 21:05:50 -06:00
|
|
|
_, err = server.connection.WriteToUDP(session_closed, session.remote)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-09 17:20:37 -06:00
|
|
|
server.Log("Session %s closed", session_id)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-12 18:06:57 -06:00
|
|
|
// TODO: handle packets without creating so many objects(and preventing an extra copy)
|
2024-04-09 17:20:37 -06:00
|
|
|
func(server *Server) handle_session_data(data []byte, from *net.UDPAddr) error {
|
2024-04-11 21:05:50 -06:00
|
|
|
session_id := SessionID(data[:SESSION_ID_LENGTH])
|
2024-04-09 17:20:37 -06:00
|
|
|
session, exists := server.sessions[session_id]
|
|
|
|
if exists == false {
|
|
|
|
return fmt.Errorf("Session %s does not exist, can't receive data", session_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
session.LastSeen = time.Now()
|
2024-04-06 16:38:14 -06:00
|
|
|
|
2024-04-11 21:05:50 -06:00
|
|
|
buf_copy := make([]byte, len(data) - SESSION_ID_LENGTH)
|
|
|
|
copy(buf_copy, data[SESSION_ID_LENGTH:])
|
2024-04-09 17:20:37 -06:00
|
|
|
|
|
|
|
select {
|
|
|
|
case session.IncomingPackets<-buf_copy:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Dropped packet to session %s", session_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func(server *Server) listen_udp() {
|
|
|
|
server.Log("Started server on %s", server.connection.LocalAddr())
|
|
|
|
|
|
|
|
var buf [SERVER_UDP_BUFFER_SIZE]byte
|
|
|
|
for true {
|
|
|
|
read, from, err := server.connection.ReadFromUDP(buf[:])
|
|
|
|
if err == nil {
|
|
|
|
var packet_type SessionPacketType = SessionPacketType(buf[0])
|
|
|
|
switch packet_type {
|
|
|
|
case SESSION_OPEN:
|
|
|
|
err := server.handle_session_open(buf[COMMAND_LENGTH:read], from)
|
2024-04-07 13:38:15 -06:00
|
|
|
if err != nil {
|
2024-04-09 17:20:37 -06:00
|
|
|
server.Log("handle_session_open erorr - %s", err)
|
2024-04-07 13:38:15 -06:00
|
|
|
}
|
|
|
|
|
2024-04-09 17:20:37 -06:00
|
|
|
case SESSION_CONNECT:
|
|
|
|
err := server.handle_session_connect(buf[COMMAND_LENGTH:read], from)
|
2024-04-07 13:38:15 -06:00
|
|
|
if err != nil {
|
2024-04-09 17:20:37 -06:00
|
|
|
server.Log("handle_session_connect error - %s", err)
|
2024-04-07 13:38:15 -06:00
|
|
|
}
|
|
|
|
|
2024-04-07 13:27:28 -06:00
|
|
|
case SESSION_CLOSE:
|
2024-04-09 17:20:37 -06:00
|
|
|
err := server.handle_session_close(buf[COMMAND_LENGTH:read], from)
|
2024-04-07 13:27:28 -06:00
|
|
|
if err != nil {
|
2024-04-09 17:20:37 -06:00
|
|
|
server.Log("handle_session_close error - %s", err)
|
2024-04-07 13:27:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
case SESSION_DATA:
|
2024-04-09 17:20:37 -06:00
|
|
|
err := server.handle_session_data(buf[COMMAND_LENGTH:read], from)
|
|
|
|
if err != nil {
|
|
|
|
server.Log("handle_session_data error - %s", err)
|
2024-04-08 11:28:52 -06:00
|
|
|
}
|
|
|
|
|
2024-04-03 18:52:04 -06:00
|
|
|
default:
|
2024-04-06 16:38:14 -06:00
|
|
|
server.Log("Unhandled packet type 0x%04x from %s: %+v", packet_type, from, buf[COMMAND_LENGTH:read])
|
2024-04-03 18:52:04 -06:00
|
|
|
}
|
|
|
|
} else if errors.Is(err, net.ErrClosed) {
|
|
|
|
server.Log("UDP_CLOSE: %s", server.connection.LocalAddr())
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
server.Log("UDP_READ_ERROR: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-16 15:06:53 -06:00
|
|
|
channels := server.channels.Load().(map[ChannelID]*Channel)
|
|
|
|
for _, channel := range(channels) {
|
|
|
|
close(channel.Commands)
|
|
|
|
}
|
|
|
|
|
2024-04-08 11:28:52 -06:00
|
|
|
server.sessions_lock.Lock()
|
2024-04-08 17:23:55 -06:00
|
|
|
sessions := make([]*ServerSession, 0, len(server.sessions))
|
|
|
|
for _, session := range(server.sessions) {
|
|
|
|
sessions = append(sessions, session)
|
|
|
|
}
|
2024-04-16 15:06:53 -06:00
|
|
|
|
2024-04-08 17:23:55 -06:00
|
|
|
for _, session := range(sessions) {
|
|
|
|
server.close_session(session)
|
2024-04-08 11:28:52 -06:00
|
|
|
}
|
|
|
|
server.sessions_lock.Unlock()
|
|
|
|
|
2024-04-03 18:52:04 -06:00
|
|
|
server.Log("Shut down server on %s", server.connection.LocalAddr())
|
|
|
|
server.stopped <- nil
|
|
|
|
}
|
|
|
|
|
2024-04-08 17:23:55 -06:00
|
|
|
func(server *Server) close_session(session *ServerSession) {
|
2024-04-16 15:34:53 -06:00
|
|
|
session.active.Store(false)
|
2024-04-08 17:23:55 -06:00
|
|
|
close(session.IncomingPackets)
|
|
|
|
close(session.OutgoingPackets)
|
|
|
|
delete(server.sessions, session.ID)
|
|
|
|
|
2024-04-16 15:06:53 -06:00
|
|
|
session_closed := NewSessionTimed(SESSION_CLOSED, server.key, &session.Session, time.Now())
|
|
|
|
server.connection.WriteToUDP(session_closed, session.remote)
|
|
|
|
}
|
2024-04-08 17:23:55 -06:00
|
|
|
|
2024-04-16 15:06:53 -06:00
|
|
|
func(server *Server) update_state() {
|
2024-04-08 17:23:55 -06:00
|
|
|
for server.active.Load() {
|
|
|
|
select {
|
2024-04-16 15:06:53 -06:00
|
|
|
case command := <-server.commands:
|
|
|
|
server.Log("Incoming server command %+v", command)
|
2024-04-08 17:23:55 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-03 18:52:04 -06:00
|
|
|
func(server *Server) Start(listen string) error {
|
|
|
|
was_inactive := server.active.CompareAndSwap(false, true)
|
|
|
|
if was_inactive == false {
|
|
|
|
return fmt.Errorf("Server already active")
|
|
|
|
}
|
|
|
|
|
|
|
|
address, err := net.ResolveUDPAddr("udp", listen)
|
|
|
|
if err != nil {
|
|
|
|
server.active.Store(false)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
server.connection, err = net.ListenUDP("udp", address)
|
|
|
|
if err != nil {
|
|
|
|
server.active.Store(false)
|
|
|
|
return fmt.Errorf("Failed to create udp server: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-04-08 11:28:52 -06:00
|
|
|
go server.listen_udp()
|
2024-04-16 15:06:53 -06:00
|
|
|
go server.update_state()
|
2024-04-03 18:52:04 -06:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|