remote-snake/server/server.lisp
2019-05-11 15:27:04 +02:00

29 lines
1.3 KiB
Common Lisp

(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* ((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)))
;;; 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))))