Switch to udp

This commit is contained in:
Loic Guegan 2019-05-12 10:14:28 +02:00
parent 9de3c7586d
commit bbd9151d7f
3 changed files with 15 additions and 30 deletions

View file

@ -12,19 +12,13 @@ LARGEUR_BLOCK = 10
NB_BLOCKS = 30 NB_BLOCKS = 30
LARGEUR_ECRAN = LARGEUR_BLOCK * NB_BLOCKS LARGEUR_ECRAN = LARGEUR_BLOCK * NB_BLOCKS
def connect(): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip_adress,port))
return s
def sendData(data): def sendData(data):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.sendto(data,(ip_adress,port))
s.connect((ip_adress,port)) received = s.recvfrom(1024)
s.sendall(data.encode('utf-8')) return received[0]
s.send('#EOF\n'.encode('utf-8'))
received = s.recv(RESP_BUFFER_LENGTH)
s.close()
return received
def newGame(): def newGame():
received = sendData('{"type": "new-game"}') received = sendData('{"type": "new-game"}')
@ -39,6 +33,7 @@ def update(gameId = 1, direction = None):
received = sendData(json.dumps(data)) received = sendData(json.dumps(data))
return json.loads(received) return json.loads(received)
# ---------- END SOCKETS ---------- # ---------- END SOCKETS ----------
LARGEUR_BLOCK = 20 LARGEUR_BLOCK = 20

View file

@ -3,7 +3,7 @@
:description "Remote Snake Server." :description "Remote Snake Server."
:version "0.0.1" :version "0.0.1"
:author "Loic Guegan" :author "Loic Guegan"
:depends-on ( "usocket" "jonathan" "cl-strings" ) :depends-on ( "usocket-server" "jonathan" "cl-strings" )
:components ((:file "packages") :components ((:file "packages")
(:module "game" (:module "game"
:depends-on ("packages") :depends-on ("packages")

View file

@ -7,23 +7,13 @@
:element-type '(unsigned-byte 8) :element-type '(unsigned-byte 8)
:initial-element 0)) :initial-element 0))
(defun handle-client (client-socket) (defun handle-client (buffer) ; echo
(unwind-protect ; To be sure to close the client socket (declare (type (simple-array (unsigned-byte 8) *) buffer)) ; Seems to be to tell lisp which type is buffer
(let* ((request (read-line (usocket:socket-stream client-socket)))) (let ((request (babel:octets-to-string buffer)))
(loop while (eq (search "#EOF" request) nil) do (format t "Receive client request: ~a" request)
(setf request (concatenate 'string request (read-line (usocket:socket-stream client-socket) nil nil)))) (babel:string-to-octets
(setf request (cl-strings:replace-all request "#EOF" "")) (handle-request *server-api* request))))
(format (usocket:socket-stream client-socket) (handle-request *server-api* request))
(force-output (usocket:socket-stream client-socket)))
(usocket:socket-close client-socket)))
;;; The server :D ;;; The server :D
(defun start (interface port) (defun start (host port)
(format t "Server started!~%") (usocket:socket-server host port #'handle-client nil :in-new-thread nil :protocol :datagram))
(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))))