Switch to udp
This commit is contained in:
parent
9de3c7586d
commit
bbd9151d7f
3 changed files with 15 additions and 30 deletions
|
@ -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
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Add table
Reference in a new issue