diff --git a/TODO b/TODO index 19a9874..447bed4 100644 --- a/TODO +++ b/TODO @@ -17,7 +17,7 @@ -> SEVERE -* run-away thread creation +* ... -> NON-SEVERE diff --git a/TODO b/TODO index 19a9874..447bed4 100644 --- a/TODO +++ b/TODO @@ -17,7 +17,7 @@ -> SEVERE -* run-away thread creation +* ... -> NON-SEVERE diff --git a/client.lisp b/client.lisp new file mode 100644 index 0000000..3031931 --- /dev/null +++ b/client.lisp @@ -0,0 +1,40 @@ +;;;; +;;;; Naledi ya Africa ("Star of Africa") is an ncurses-based survival game +;;;; set in Africa. +;;;; +;;;; This file is responsible for connecting to the server and provides the +;;;; API functions for doing so. +;;;; +;;;; (c) 2018 Daniel Vedder, MIT license +;;;; + +(let ((naledi-server NIL)) + (defun connect-server (&optional (ip "127.0.0.1") (port *port*)) + "Connect to the specified server" + ;;FIXME I need to catch some exceptions here... + (setf naledi-server (socket-connect ip port)) + (if naledi-server + (notify "Connected to server ~A:~A" ip port) + (notify "Connection to server ~A:~A failed." ip port))) + + (defun current-server () naledi-server) ;TODO remove after development + + (defun query-server (request) + "Send a request string to the server and return the answer" + (unless naledi-server ;XXX do this with exceptions + (return-from query-server "You are not connected to a server!")) + (let ((servstr (socket-stream naledi-server))) + (logging "CLIENT: sending request ~S" request) + (format servstr "~A~%" request) + (finish-output servstr) + ;;FIXME server still doesn't receive string until disconnect... + (logging "CLIENT: waiting for server response") + (wait-for-input naledi-server) + (read-from-string (read-line servstr)))) + + (defun disconnect () + "Disconnect from the server" + (when naledi-server + (socket-close naledi-server) + (setf naledi-server NIL) + (notify "Disconnected from server.")))) diff --git a/TODO b/TODO index 19a9874..447bed4 100644 --- a/TODO +++ b/TODO @@ -17,7 +17,7 @@ -> SEVERE -* run-away thread creation +* ... -> NON-SEVERE diff --git a/client.lisp b/client.lisp new file mode 100644 index 0000000..3031931 --- /dev/null +++ b/client.lisp @@ -0,0 +1,40 @@ +;;;; +;;;; Naledi ya Africa ("Star of Africa") is an ncurses-based survival game +;;;; set in Africa. +;;;; +;;;; This file is responsible for connecting to the server and provides the +;;;; API functions for doing so. +;;;; +;;;; (c) 2018 Daniel Vedder, MIT license +;;;; + +(let ((naledi-server NIL)) + (defun connect-server (&optional (ip "127.0.0.1") (port *port*)) + "Connect to the specified server" + ;;FIXME I need to catch some exceptions here... + (setf naledi-server (socket-connect ip port)) + (if naledi-server + (notify "Connected to server ~A:~A" ip port) + (notify "Connection to server ~A:~A failed." ip port))) + + (defun current-server () naledi-server) ;TODO remove after development + + (defun query-server (request) + "Send a request string to the server and return the answer" + (unless naledi-server ;XXX do this with exceptions + (return-from query-server "You are not connected to a server!")) + (let ((servstr (socket-stream naledi-server))) + (logging "CLIENT: sending request ~S" request) + (format servstr "~A~%" request) + (finish-output servstr) + ;;FIXME server still doesn't receive string until disconnect... + (logging "CLIENT: waiting for server response") + (wait-for-input naledi-server) + (read-from-string (read-line servstr)))) + + (defun disconnect () + "Disconnect from the server" + (when naledi-server + (socket-close naledi-server) + (setf naledi-server NIL) + (notify "Disconnected from server.")))) diff --git a/naledi.lisp b/naledi.lisp index dd4a87f..df5bd7c 100644 --- a/naledi.lisp +++ b/naledi.lisp @@ -28,6 +28,7 @@ (load "biomes.lisp") (load "animals.lisp") (load "server.lisp") +(load "client.lisp") (defun start-game () "Start the game logic and UI" diff --git a/TODO b/TODO index 19a9874..447bed4 100644 --- a/TODO +++ b/TODO @@ -17,7 +17,7 @@ -> SEVERE -* run-away thread creation +* ... -> NON-SEVERE diff --git a/client.lisp b/client.lisp new file mode 100644 index 0000000..3031931 --- /dev/null +++ b/client.lisp @@ -0,0 +1,40 @@ +;;;; +;;;; Naledi ya Africa ("Star of Africa") is an ncurses-based survival game +;;;; set in Africa. +;;;; +;;;; This file is responsible for connecting to the server and provides the +;;;; API functions for doing so. +;;;; +;;;; (c) 2018 Daniel Vedder, MIT license +;;;; + +(let ((naledi-server NIL)) + (defun connect-server (&optional (ip "127.0.0.1") (port *port*)) + "Connect to the specified server" + ;;FIXME I need to catch some exceptions here... + (setf naledi-server (socket-connect ip port)) + (if naledi-server + (notify "Connected to server ~A:~A" ip port) + (notify "Connection to server ~A:~A failed." ip port))) + + (defun current-server () naledi-server) ;TODO remove after development + + (defun query-server (request) + "Send a request string to the server and return the answer" + (unless naledi-server ;XXX do this with exceptions + (return-from query-server "You are not connected to a server!")) + (let ((servstr (socket-stream naledi-server))) + (logging "CLIENT: sending request ~S" request) + (format servstr "~A~%" request) + (finish-output servstr) + ;;FIXME server still doesn't receive string until disconnect... + (logging "CLIENT: waiting for server response") + (wait-for-input naledi-server) + (read-from-string (read-line servstr)))) + + (defun disconnect () + "Disconnect from the server" + (when naledi-server + (socket-close naledi-server) + (setf naledi-server NIL) + (notify "Disconnected from server.")))) diff --git a/naledi.lisp b/naledi.lisp index dd4a87f..df5bd7c 100644 --- a/naledi.lisp +++ b/naledi.lisp @@ -28,6 +28,7 @@ (load "biomes.lisp") (load "animals.lisp") (load "server.lisp") +(load "client.lisp") (defun start-game () "Start the game logic and UI" diff --git a/server.lisp b/server.lisp index 901d069..656ac10 100644 --- a/server.lisp +++ b/server.lisp @@ -108,12 +108,12 @@ (with-socket-listener (socket "127.0.0.1" *port*) (while running (wait-for-input socket) - ;;FIXME infinite threads created over time (let ((thread (bt:make-thread #'(lambda () (handle-connection socket)) :name (string-from-list (list "player-thread" (length player-threads)) "-")))) - (setf player-threads (cons thread player-threads)))))) + (setf player-threads (cons thread player-threads)) + (sleep 1))))) ;;give the socket a chance to connect (defun handle-connection (socket) "Answer requests until the player disconnects" @@ -121,7 +121,8 @@ (logging "~&SERVER: received a connection.") ; cf. `get-peer-name' (do* ((sockstr (socket-stream connection)) (request (read-line sockstr NIL) (read-line sockstr NIL))) - ((or (not running) (null request))) + ;;TODO remove player-thread from list when terminated + ((or (not running) (null request))) (format sockstr "~S~%" (to-string (answer request))) (finish-output sockstr))))) @@ -157,37 +158,3 @@ (format NIL "The following items are here:~A *~A" #\newline (string-from-list (mapcar #'.name (patch-items p))) (format NIL "~% *")))))) - - -;;; CLIENT FUNCTIONS - -(let ((naledi-server NIL)) - (defun connect-server (&optional (ip "127.0.0.1") (port *port*)) - "Connect to the specified server" - ;;FIXME I need to catch some exceptions here... - (setf naledi-server (socket-connect ip port)) - (if naledi-server - (notify "Connected to server ~A:~A" ip port) - (notify "Connection to server ~A:~A failed." ip port))) - - (defun current-server () naledi-server) ;TODO remove after development - - (defun query-server (request) - "Send a request string to the server and return the answer" - (unless naledi-server ;XXX do this with exceptions - (return-from query-server "You are not connected to a server!")) - (let ((servstr (socket-stream naledi-server))) - (logging "CLIENT: sending request ~S" request) - (format servstr "~A~%" request) - (finish-output servstr) - ;;FIXME server still doesn't receive string until disconnect... - (logging "CLIENT: waiting for server response") - (wait-for-input naledi-server) - (read-from-string (read-line servstr)))) - - (defun disconnect () - "Disconnect from the server" - (when naledi-server - (socket-close naledi-server) - (setf naledi-server NIL) - (notify "Disconnected from server."))))