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