Skip to content

Commit

Permalink
server accepts multiple clients
Browse files Browse the repository at this point in the history
  • Loading branch information
ezrarush committed Jul 12, 2014
1 parent dea3cd1 commit 6803e1c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 40 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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".
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".
12 changes: 0 additions & 12 deletions battleship.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))))))))
Expand Down
42 changes: 23 additions & 19 deletions network/common.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)))
Expand All @@ -53,6 +53,10 @@

(defun network ()
"Network loop"

(when (server-p)
(accept-client))

(if (connected-p)
(progn

Expand All @@ -63,6 +67,6 @@
)
)

;; Nothing is connected, so wait for its connection
(when (server-p)
(accept-client))))
)

)
15 changes: 8 additions & 7 deletions network/server.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

(defun server-p () *server*)

(defvar *client* nil)
(defvar *clients* nil)

(defun start-server (server-ip port)
(assert (not *server-socket*))
Expand All @@ -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)
Expand All @@ -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)))))

0 comments on commit 6803e1c

Please sign in to comment.