From bbd9151d7fa3500582c62474da2ce39ea151a63c Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Sun, 12 May 2019 10:14:28 +0200 Subject: [PATCH] Switch to udp --- client/client.py | 17 ++++++----------- server/remote-snake-server.asd | 2 +- server/server.lisp | 26 ++++++++------------------ 3 files changed, 15 insertions(+), 30 deletions(-) diff --git a/client/client.py b/client/client.py index f87eaba..ea72c27 100644 --- a/client/client.py +++ b/client/client.py @@ -12,19 +12,13 @@ LARGEUR_BLOCK = 10 NB_BLOCKS = 30 LARGEUR_ECRAN = LARGEUR_BLOCK * NB_BLOCKS -def connect(): - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.connect((ip_adress,port)) - return s +s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + def sendData(data): - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.connect((ip_adress,port)) - s.sendall(data.encode('utf-8')) - s.send('#EOF\n'.encode('utf-8')) - received = s.recv(RESP_BUFFER_LENGTH) - s.close() - return received + s.sendto(data,(ip_adress,port)) + received = s.recvfrom(1024) + return received[0] def newGame(): received = sendData('{"type": "new-game"}') @@ -39,6 +33,7 @@ def update(gameId = 1, direction = None): received = sendData(json.dumps(data)) return json.loads(received) + # ---------- END SOCKETS ---------- LARGEUR_BLOCK = 20 diff --git a/server/remote-snake-server.asd b/server/remote-snake-server.asd index 598f270..1a168f2 100644 --- a/server/remote-snake-server.asd +++ b/server/remote-snake-server.asd @@ -3,7 +3,7 @@ :description "Remote Snake Server." :version "0.0.1" :author "Loic Guegan" - :depends-on ( "usocket" "jonathan" "cl-strings" ) + :depends-on ( "usocket-server" "jonathan" "cl-strings" ) :components ((:file "packages") (:module "game" :depends-on ("packages") diff --git a/server/server.lisp b/server/server.lisp index f1417c5..3676121 100644 --- a/server/server.lisp +++ b/server/server.lisp @@ -7,23 +7,13 @@ :element-type '(unsigned-byte 8) :initial-element 0)) -(defun handle-client (client-socket) - (unwind-protect ; To be sure to close the client socket - (let* ((request (read-line (usocket:socket-stream client-socket)))) - (loop while (eq (search "#EOF" request) nil) do - (setf request (concatenate 'string request (read-line (usocket:socket-stream client-socket) nil nil)))) - (setf request (cl-strings:replace-all request "#EOF" "")) - (format (usocket:socket-stream client-socket) (handle-request *server-api* request)) - (force-output (usocket:socket-stream client-socket))) - (usocket:socket-close client-socket))) - +(defun handle-client (buffer) ; echo + (declare (type (simple-array (unsigned-byte 8) *) buffer)) ; Seems to be to tell lisp which type is buffer + (let ((request (babel:octets-to-string buffer))) + (format t "Receive client request: ~a" request) + (babel:string-to-octets + (handle-request *server-api* request)))) ;;; The server :D -(defun start (interface port) - (format t "Server started!~%") - (let ((socket (usocket:socket-listen interface port))) - (unwind-protect ; To be sure to close server socket - (loop - (handle-client (usocket:socket-accept socket :element-type 'character))) - (usocket:socket-close socket)))) - +(defun start (host port) + (usocket:socket-server host port #'handle-client nil :in-new-thread nil :protocol :datagram))