major refactoring :/
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m56s
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m56s
This commit is contained in:
138
tcp-cart-mux-server.go
Normal file
138
tcp-cart-mux-server.go
Normal file
@@ -0,0 +1,138 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type CartServer struct {
|
||||
*TCPCartServerMux
|
||||
}
|
||||
|
||||
func CartListen(address string) (*CartServer, error) {
|
||||
listener, err := net.Listen("tcp", address)
|
||||
server := &CartServer{
|
||||
NewCartTCPServerMux(100),
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
go func() {
|
||||
for {
|
||||
conn, err := listener.Accept()
|
||||
if err != nil {
|
||||
log.Printf("Error accepting connection: %v\n", err)
|
||||
continue
|
||||
}
|
||||
go server.HandleConnection(conn)
|
||||
}
|
||||
}()
|
||||
return server, nil
|
||||
}
|
||||
|
||||
type TCPCartServerMux struct {
|
||||
mu sync.RWMutex
|
||||
listeners map[uint16]func(CartId, []byte) error
|
||||
functions map[uint16]func(CartId, []byte) (uint16, []byte, error)
|
||||
}
|
||||
|
||||
func NewCartTCPServerMux(maxClients int) *TCPCartServerMux {
|
||||
m := &TCPCartServerMux{
|
||||
mu: sync.RWMutex{},
|
||||
listeners: make(map[uint16]func(CartId, []byte) error),
|
||||
functions: make(map[uint16]func(CartId, []byte) (uint16, []byte, error)),
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *TCPCartServerMux) handleListener(messageType uint16, id CartId, data []byte) (bool, error) {
|
||||
m.mu.RLock()
|
||||
handler, ok := m.listeners[messageType]
|
||||
m.mu.RUnlock()
|
||||
if ok {
|
||||
err := handler(id, data)
|
||||
if err != nil {
|
||||
return true, err
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (m *TCPCartServerMux) handleFunction(connection net.Conn, messageType uint16, id CartId, data []byte) (bool, error) {
|
||||
m.mu.RLock()
|
||||
function, ok := m.functions[messageType]
|
||||
m.mu.RUnlock()
|
||||
if ok {
|
||||
responseType, responseData, err := function(id, data)
|
||||
if err != nil {
|
||||
return true, err
|
||||
}
|
||||
err = binary.Write(connection, binary.LittleEndian, CartPacket{
|
||||
Version: 1,
|
||||
MessageType: responseType,
|
||||
DataLength: uint16(len(responseData)),
|
||||
Id: id,
|
||||
})
|
||||
if err != nil {
|
||||
return true, err
|
||||
}
|
||||
packetsSent.Inc()
|
||||
_, err = connection.Write(responseData)
|
||||
return true, err
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (m *TCPCartServerMux) HandleConnection(connection net.Conn) error {
|
||||
var packet *CartPacket
|
||||
var err error
|
||||
defer connection.Close()
|
||||
for {
|
||||
err = ReadPacket(connection, packet)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
return nil
|
||||
}
|
||||
log.Printf("Error receiving packet: %v\n", err)
|
||||
return err
|
||||
}
|
||||
if packet == nil {
|
||||
log.Println("Packet is nil")
|
||||
continue
|
||||
}
|
||||
data, err := GetPacketData(connection, int(packet.DataLength))
|
||||
if err != nil {
|
||||
log.Printf("Error getting packet data: %v\n", err)
|
||||
}
|
||||
status, err := m.handleListener(packet.MessageType, packet.Id, data)
|
||||
if err != nil {
|
||||
log.Printf("Error handling listener: %v\n", err)
|
||||
}
|
||||
if !status {
|
||||
status, err = m.handleFunction(connection, packet.MessageType, packet.Id, data)
|
||||
if err != nil {
|
||||
log.Printf("Error handling function: %v\n", err)
|
||||
}
|
||||
if !status {
|
||||
log.Printf("Unknown message type: %d\n", packet.MessageType)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *TCPCartServerMux) ListenFor(messageType uint16, handler func(CartId, []byte) error) {
|
||||
m.mu.Lock()
|
||||
m.listeners[messageType] = handler
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
func (m *TCPCartServerMux) HandleCall(messageType uint16, handler func(CartId, []byte) (uint16, []byte, error)) {
|
||||
m.mu.Lock()
|
||||
m.functions[messageType] = handler
|
||||
m.mu.Unlock()
|
||||
}
|
||||
Reference in New Issue
Block a user