Finish server git add -A!

This commit is contained in:
Loic Guegan 2019-05-10 13:29:26 +02:00
parent 284ad037a5
commit 38d9702266
4 changed files with 43 additions and 3 deletions

View file

@ -51,7 +51,8 @@
(if dir
(refresh game :dir dir)
(refresh game))
(append (list :type "state") (dump gm game-id)))))
(to-json
(append (list :type "state") (dump gm game-id))))))
(defmethod handle-request ((api api) request)

View file

@ -11,4 +11,11 @@
(:use :common-lisp :jonathan :remote-snake-server-game)
(:shadow #:dump) ; Because game-manager also has a dump method
(:export
#:api))
#:api
#:handle-request))
(defpackage :remote-snake-server
(:nicknames :rss)
(:use :common-lisp :usocket :remote-snake-server-api)
(:export
#:start))

View file

@ -14,4 +14,6 @@
:depends-on ("game")
:components ((:file "game-manager")
(:file "api"
:depends-on ("game-manager"))))))
:depends-on ("game-manager"))))
(:file "server"
:depends-on ("api"))))

30
server/server.lisp Normal file
View file

@ -0,0 +1,30 @@
(in-package :remote-snake-server)
(defparameter *server-api* (make-instance 'api))
;;; Not that max buffer size is important (since snake in on a single line, snake coordinate can be cutted !)
(defparameter *server-buffer* (make-array 10000
:element-type '(unsigned-byte 8)
:initial-element 0))
(defun handle-client (client-socket)
(unwind-protect ; To be sure to close the client socket
(let ((data (read-line (usocket:socket-stream client-socket)))
(request ""))
(loop while (not (equalp data "#EOF")) do
(setf request (concatenate 'string request data))
(setf data (read-line (usocket:socket-stream client-socket) nil nil)))
(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
(defun start (port)
(format t "Server start !~%")
(let ((socket (usocket:socket-listen "127.0.0.1" port)))
(unwind-protect ; To be sure to close server socket
(loop
(handle-client (usocket:socket-accept socket :element-type 'character)))
(usocket:socket-close socket))))