remote-snake/server/server.lisp
2019-05-12 14:01:05 +02:00

30 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))
;;; TODO: debug this function
(defun send-cmd (host port game-id command arg)
(let ((socket (usocket:socket-connect host port :protocol :datagram))
(request (list :type "admin" :cmd command :game-id game-id :arg arg))
(buffer (make-array 500 :element-type '(unsigned-byte 8) :initial-element 0)))
(usocket:socket-send socket (string-downcase (to-json request)) 300)
(format t (babel:octets-to-string (usocket:socket-receive socket buffer 300)))
(force-output t)
(usocket:socket-close socket)))
(defun handle-client (buffer) ; echo
(declare (type (simple-array (unsigned-byte 8) *) buffer)) ; Seems to be to tell lisp which type is buffer
(let ((request (babel:octets-to-string buffer)))
(format t "Receive client request: ~a" request)
(babel:string-to-octets
(handle-request *server-api* request))))
;;; The server :D
(defun start (host port)
(usocket:socket-server host port #'handle-client nil :in-new-thread nil :protocol :datagram))