diff --git a/server/api/api.lisp b/server/api/api.lisp index 5a67f06..d41921e 100644 --- a/server/api/api.lisp +++ b/server/api/api.lisp @@ -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) diff --git a/server/packages.lisp b/server/packages.lisp index 3f3788d..2b6a5a2 100644 --- a/server/packages.lisp +++ b/server/packages.lisp @@ -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)) diff --git a/server/remote-snake-server.asd b/server/remote-snake-server.asd index 0dc53fc..b8bcfe5 100644 --- a/server/remote-snake-server.asd +++ b/server/remote-snake-server.asd @@ -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")))) diff --git a/server/server.lisp b/server/server.lisp new file mode 100644 index 0000000..b16bd2d --- /dev/null +++ b/server/server.lisp @@ -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)))) +