This commit is contained in:
@@ -36,21 +36,21 @@ func CartListen(address string) (*CartServer, error) {
|
||||
|
||||
type TCPCartServerMux struct {
|
||||
mu sync.RWMutex
|
||||
listeners map[uint16]func(CartId, []byte) error
|
||||
functions map[uint16]func(CartId, []byte) (uint16, []byte, error)
|
||||
listeners map[uint32]func(CartId, []byte) error
|
||||
functions map[uint32]func(CartId, []byte) (uint32, []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)),
|
||||
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 uint16, id CartId, data []byte) (bool, error) {
|
||||
func (m *TCPCartServerMux) handleListener(messageType uint32, id CartId, data []byte) (bool, error) {
|
||||
m.mu.RLock()
|
||||
handler, ok := m.listeners[messageType]
|
||||
m.mu.RUnlock()
|
||||
@@ -63,19 +63,19 @@ func (m *TCPCartServerMux) handleListener(messageType uint16, id CartId, data []
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (m *TCPCartServerMux) handleFunction(connection net.Conn, messageType uint16, id CartId, data []byte) (bool, error) {
|
||||
func (m *TCPCartServerMux) handleFunction(connection net.Conn, messageType uint32, id CartId, data []byte) (bool, error) {
|
||||
m.mu.RLock()
|
||||
function, ok := m.functions[messageType]
|
||||
fn, ok := m.functions[messageType]
|
||||
m.mu.RUnlock()
|
||||
if ok {
|
||||
responseType, responseData, err := function(id, data)
|
||||
responseType, responseData, err := fn(id, data)
|
||||
if err != nil {
|
||||
return true, err
|
||||
}
|
||||
err = binary.Write(connection, binary.LittleEndian, CartPacket{
|
||||
Version: 1,
|
||||
MessageType: responseType,
|
||||
DataLength: uint16(len(responseData)),
|
||||
DataLength: uint64(len(responseData)),
|
||||
Id: id,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -93,7 +93,7 @@ func (m *TCPCartServerMux) HandleConnection(connection net.Conn) error {
|
||||
var err error
|
||||
defer connection.Close()
|
||||
for {
|
||||
err = ReadPacket(connection, &packet)
|
||||
err = ReadCartPacket(connection, &packet)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
return nil
|
||||
@@ -122,13 +122,13 @@ func (m *TCPCartServerMux) HandleConnection(connection net.Conn) error {
|
||||
}
|
||||
}
|
||||
|
||||
func (m *TCPCartServerMux) ListenFor(messageType uint16, handler func(CartId, []byte) error) {
|
||||
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 uint16, handler func(CartId, []byte) (uint16, []byte, error)) {
|
||||
func (m *TCPCartServerMux) HandleCall(messageType uint32, handler func(CartId, []byte) (uint32, []byte, error)) {
|
||||
m.mu.Lock()
|
||||
m.functions[messageType] = handler
|
||||
m.mu.Unlock()
|
||||
|
||||
Reference in New Issue
Block a user