154 lines
3.5 KiB
Go
154 lines
3.5 KiB
Go
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(),
|
|
}
|
|
|
|
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[uint32]func(CartId, []byte) error
|
|
functions map[uint32]func(CartId, []byte) (uint32, []byte, error)
|
|
}
|
|
|
|
func NewCartTCPServerMux() *TCPCartServerMux {
|
|
m := &TCPCartServerMux{
|
|
mu: sync.RWMutex{},
|
|
listeners: make(map[uint32]func(CartId, []byte) error),
|
|
functions: make(map[uint32]func(CartId, []byte) (uint32, []byte, error)),
|
|
}
|
|
|
|
return m
|
|
}
|
|
|
|
func (m *TCPCartServerMux) handleListener(messageType uint32, 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 uint32, id CartId, data []byte) (bool, error) {
|
|
m.mu.RLock()
|
|
fn, ok := m.functions[messageType]
|
|
m.mu.RUnlock()
|
|
if ok {
|
|
responseType, responseData, err := fn(id, data)
|
|
|
|
if err != nil {
|
|
errData := []byte(err.Error())
|
|
err = binary.Write(connection, binary.LittleEndian, CartPacket{
|
|
Version: CurrentPacketVersion,
|
|
MessageType: responseType,
|
|
DataLength: uint32(len(errData)),
|
|
StatusCode: 500,
|
|
Id: id,
|
|
})
|
|
_, err = connection.Write(errData)
|
|
return true, err
|
|
}
|
|
err = binary.Write(connection, binary.LittleEndian, CartPacket{
|
|
Version: CurrentPacketVersion,
|
|
MessageType: responseType,
|
|
DataLength: uint32(len(responseData)),
|
|
StatusCode: 200,
|
|
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 = ReadCartPacket(connection, &packet)
|
|
if err != nil {
|
|
if err == io.EOF {
|
|
return nil
|
|
}
|
|
log.Printf("Error receiving packet: %v\n", err)
|
|
return err
|
|
}
|
|
if packet.Version != CurrentPacketVersion {
|
|
log.Printf("Incorrect packet version: %d\n", packet.Version)
|
|
continue
|
|
}
|
|
data, err := GetPacketData(connection, packet.DataLength)
|
|
if err != nil {
|
|
log.Printf("Error getting packet data: %v\n", err)
|
|
}
|
|
go m.HandleData(connection, packet.MessageType, packet.Id, data)
|
|
}
|
|
}
|
|
|
|
func (m *TCPCartServerMux) HandleData(connection net.Conn, t uint32, id CartId, data []byte) {
|
|
status, err := m.handleListener(t, id, data)
|
|
if err != nil {
|
|
log.Printf("Error handling listener: %v\n", err)
|
|
}
|
|
if !status {
|
|
status, err = m.handleFunction(connection, t, id, data)
|
|
if err != nil {
|
|
log.Printf("Error handling function: %v\n", err)
|
|
}
|
|
if !status {
|
|
log.Printf("Unknown message type: %d\n", t)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (m *TCPCartServerMux) ListenFor(messageType uint32, handler func(CartId, []byte) error) {
|
|
m.mu.Lock()
|
|
m.listeners[messageType] = handler
|
|
m.mu.Unlock()
|
|
}
|
|
|
|
func (m *TCPCartServerMux) HandleCall(messageType uint32, handler func(CartId, []byte) (uint32, []byte, error)) {
|
|
m.mu.Lock()
|
|
m.functions[messageType] = handler
|
|
m.mu.Unlock()
|
|
}
|