Finish server git add -A!
This commit is contained in:
parent
284ad037a5
commit
38d9702266
4 changed files with 43 additions and 3 deletions
|
@ -51,7 +51,8 @@
|
||||||
(if dir
|
(if dir
|
||||||
(refresh game :dir dir)
|
(refresh game :dir dir)
|
||||||
(refresh game))
|
(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)
|
(defmethod handle-request ((api api) request)
|
||||||
|
|
|
@ -11,4 +11,11 @@
|
||||||
(:use :common-lisp :jonathan :remote-snake-server-game)
|
(:use :common-lisp :jonathan :remote-snake-server-game)
|
||||||
(:shadow #:dump) ; Because game-manager also has a dump method
|
(:shadow #:dump) ; Because game-manager also has a dump method
|
||||||
(:export
|
(:export
|
||||||
#:api))
|
#:api
|
||||||
|
#:handle-request))
|
||||||
|
|
||||||
|
(defpackage :remote-snake-server
|
||||||
|
(:nicknames :rss)
|
||||||
|
(:use :common-lisp :usocket :remote-snake-server-api)
|
||||||
|
(:export
|
||||||
|
#:start))
|
||||||
|
|
|
@ -14,4 +14,6 @@
|
||||||
:depends-on ("game")
|
:depends-on ("game")
|
||||||
:components ((:file "game-manager")
|
:components ((:file "game-manager")
|
||||||
(:file "api"
|
(:file "api"
|
||||||
:depends-on ("game-manager"))))))
|
:depends-on ("game-manager"))))
|
||||||
|
(:file "server"
|
||||||
|
:depends-on ("api"))))
|
||||||
|
|
30
server/server.lisp
Normal file
30
server/server.lisp
Normal 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))))
|
||||||
|
|
Loading…
Add table
Reference in a new issue