From 6803e1c87f9c9fc2fff2d7bd8cf9da8146b24cf7 Mon Sep 17 00:00:00 2001 From: Ezra Rush Date: Sat, 12 Jul 2014 13:26:31 -0500 Subject: [PATCH] server accepts multiple clients --- README.md | 15 +++++++++++++-- battleship.lisp | 12 ------------ network/common.lisp | 42 +++++++++++++++++++++++------------------- network/server.lisp | 15 ++++++++------- 4 files changed, 44 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 4cb9466..4286d15 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ Run the following from the SBCL shell. You may use slime with this SBCL image by uncommenting the two swank lines and executing the command "slime-connect" in emacs. -###Client +### Player One Run the following in a second SBCL instance shell. @@ -50,4 +50,15 @@ Run the following in a second SBCL instance shell. (sdl2:make-this-thread-main (lambda () (battleship:main nil "127.0.0.1"))) ``` -If the client is on a different host than the server, use the server's ip address instead of the loop back address "127.0.0.1". \ No newline at end of file +If the player one is on a different host than the server, use the server's ip address instead of the loop back address "127.0.0.1". + +### Player Two + +Run the following in a third SBCL instance shell. + +```lisp +(ql:quickload "battleship") +(sdl2:make-this-thread-main (lambda () (battleship:main nil "127.0.0.1"))) +``` + +If the player two is on a different host than the server, use the server's ip address instead of the loop back address "127.0.0.1". \ No newline at end of file diff --git a/battleship.lisp b/battleship.lisp index ffddefb..7bc4494 100644 --- a/battleship.lisp +++ b/battleship.lisp @@ -32,30 +32,22 @@ () (setf *current-time* (sdl2:get-ticks)) (setf *delta-time* (ensure-float (- *current-time* *last-time*))) - (when (>= *delta-time* 10.0) (incf *last-time* 10)) - (network) (when (connected-p))) - (:quit () t)) - (stop-server))) (progn (connect-to-server server-ip port) (sdl2:with-window (win :title (if (server-p) "Battleship Server" "Battleship Client") :w *window-width* :h *window-height* :flags '(:shown :opengl)) (sdl2:with-gl-context (gl-context win) (sdl2:gl-make-current win gl-context) - (setf *graphics-engine* (make-instance 'graphics-engine)) (graphics-init *graphics-engine*) - (setf *last-time* (sdl2:get-ticks)) (unwind-protect - (sdl2:with-event-loop (:method :poll) - (:keydown (:keysym keysym) (let ((scancode (sdl2:scancode-value keysym)) @@ -91,16 +83,12 @@ () (setf *current-time* (sdl2:get-ticks)) (setf *delta-time* (ensure-float (- *current-time* *last-time*))) - (when (>= *delta-time* 10.0) (incf *last-time* 10)) - (network) - (when (connected-p) (render-scene *graphics-engine*) (sdl2:gl-swap-window win))) - (:quit () t)) (disconnect-from-server)))))))) diff --git a/network/common.lisp b/network/common.lisp index 764d6a0..4772f24 100644 --- a/network/common.lisp +++ b/network/common.lisp @@ -7,7 +7,7 @@ (userial:make-vector-serializer :coordinate :int32 2) (defun connected-p () - (or *client* + (or *clients* *server-connection*)) (defun send-message (to buffer) @@ -18,24 +18,24 @@ (write-sequence buffer stream :end (length buffer)) (force-output stream)))) -(defun read-messages () - (let* ((connection (if (server-p) - *client* - *server-connection*)) - (buffer (userial:make-buffer)) - (stream (usocket:socket-stream connection))) +;; (defun read-messages () +;; (let* ((connection (if (server-p) +;; *client* +;; *server-connection*)) +;; (buffer (userial:make-buffer)) +;; (stream (usocket:socket-stream connection))) - ;; Read the size of the message in bytes, then read those bytes - (when (listen stream) - (userial:with-buffer buffer - (let* ((size (read-byte stream))) - (userial:buffer-advance size) - (read-sequence buffer stream :end size)) +;; ;; Read the size of the message in bytes, then read those bytes +;; (when (listen stream) +;; (userial:with-buffer buffer +;; (let* ((size (read-byte stream))) +;; (userial:buffer-advance size) +;; (read-sequence buffer stream :end size)) - (unless (zerop (userial:buffer-length)) - (userial:buffer-rewind) - (deserialize buffer (userial:unserialize :opcodes))))))) +;; (unless (zerop (userial:buffer-length)) +;; (userial:buffer-rewind) +;; (deserialize buffer (userial:unserialize :opcodes))))))) (defgeneric deserialize (buffer thing) (:method (message (thing (eql :delta-update))) @@ -53,6 +53,10 @@ (defun network () "Network loop" + + (when (server-p) + (accept-client)) + (if (connected-p) (progn @@ -63,6 +67,6 @@ ) ) - ;; Nothing is connected, so wait for its connection - (when (server-p) - (accept-client)))) + ) + + ) diff --git a/network/server.lisp b/network/server.lisp index ed3e236..1347b80 100644 --- a/network/server.lisp +++ b/network/server.lisp @@ -6,7 +6,7 @@ (defun server-p () *server*) -(defvar *client* nil) +(defvar *clients* nil) (defun start-server (server-ip port) (assert (not *server-socket*)) @@ -20,17 +20,18 @@ (assert *server-socket*) (usocket:socket-close *server-socket*) (setf *server-socket* nil - *client* nil)) + *clients* nil)) (defun accept-client () (when (usocket:wait-for-input *server-socket* :timeout 0 :ready-only t) - (unless *client* - (setf *client* (usocket:socket-accept *server-socket*))))) + (push (usocket:socket-accept *server-socket*) *clients*) + (format t "a client was accepted") + )) (defun batch-update () - (when *client* + (when *clients* (let ((buffer (userial:make-buffer))) (userial:with-buffer buffer (userial:serialize :opcodes :batch-update) @@ -45,5 +46,5 @@ ;; (userial:serialize :keyword :ball) ;; (serialize buffer *ball*) ) - - (send-message *client* buffer)))) + (loop for client in *clients* do + (send-message client buffer)))))