From 1464d0895722c8c372de1fca5a8dfc58662fb9ce Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Mon, 15 May 2023 20:08:31 +0200 Subject: [PATCH] #267 Fixed merged requests --- pokete_classes/multiplayer/connector.py | 47 ++++++++++++------------- server/server/server.go | 25 ++++++++----- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/pokete_classes/multiplayer/connector.py b/pokete_classes/multiplayer/connector.py index d2838c6d..fbd3df38 100644 --- a/pokete_classes/multiplayer/connector.py +++ b/pokete_classes/multiplayer/connector.py @@ -77,35 +77,32 @@ def establish_connection(self): ) return False - def send_pos_update(self, _map, x, y): + def send(self, data: dict): self.connection.sendall( - str.encode( - json.dumps( - { - "Type": 0, - "Body": { - "Map": _map, - "X": x, - "Y": y, - }, - } - ) - ) + str.encode(json.dumps(data)) + END_SECTION + ) + + def send_pos_update(self, _map, x, y): + self.send( + { + "Type": 0, + "Body": { + "Map": _map, + "X": x, + "Y": y, + }, + } ) def handshake(self): - self.connection.sendall( - str.encode( - json.dumps( - { - "Type": 1, - "Body": { - "UserName": self.user_name, - "Version": release.VERSION, - }, - } - ) - ) + self.send( + { + "Type": 1, + "Body": { + "UserName": self.user_name, + "Version": release.VERSION, + }, + } ) if (d := self.receive_data())["Type"] == 2: self.ask_user_name(True) diff --git a/server/server/server.go b/server/server/server.go index 3a00a8f2..89252ef6 100644 --- a/server/server/server.go +++ b/server/server/server.go @@ -1,7 +1,8 @@ package server import ( - "github.com/lxgr-linux/pokete/server/provider" + "bytes" + "github.com/lxgr-linux/pokete/server/provider" "log" "net" @@ -13,6 +14,10 @@ import ( "github.com/lxgr-linux/pokete/server/user_repository" ) +var ( + END_SECTION = []byte("") +) + func NewServer(cfg config.Config) (server Server, err error) { mapRepo, err := map_repository.NewMapRepo() if err != nil { @@ -51,13 +56,17 @@ func (s Server) Start() { } } -func (s Server) handleRequests(res []byte, connection *net.Conn) error { - genericResponseObject, err := handler.Handle(res) - log.Printf("%#v\n", genericResponseObject) - err = genericResponseObject.Body.Handle(connection, s.Provider) - if err != nil { - return err - } +func (s Server) handleRequests(origRes []byte, connection *net.Conn) error { + splid := bytes.Split(origRes, END_SECTION) + for _, res := range splid[:len(splid) - 1]{ + genericResponseObject, err := handler.Handle(res) + log.Printf("%s", res) + log.Printf("%#v\n", genericResponseObject) + err = genericResponseObject.Body.Handle(connection, s.Provider) + if err != nil { + return err + } + } return nil }