Skip to content

Commit

Permalink
Extract common code between client and server
Browse files Browse the repository at this point in the history
  • Loading branch information
borodust committed Apr 18, 2017
1 parent d756789 commit d64ff70
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 27 deletions.
12 changes: 8 additions & 4 deletions client/src/connector.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
(loop while enabled-p
do (log-errors
(usocket:wait-for-input connection)
(let* ((message (conspack:decode-stream (connection-stream-of this)))
(let* ((message (decode-message (connection-stream-of this)))
(message-id (getf message :reply-for)))
(with-instance-lock-held (this)
(if-let ((handler (gethash message-id message-table)))
Expand All @@ -39,7 +39,7 @@
finally (usocket:socket-close connection)))))


(defun connect-to-server (host &optional (port 8778))
(defun connect-to-server (host port)
(make-instance 'connector
:connection (usocket:socket-connect host port
:element-type '(unsigned-byte 8)
Expand All @@ -62,11 +62,10 @@

(defun send-command (connector &rest properties &key &allow-other-keys)
(let ((stream (connection-stream-of connector)))
(conspack:encode properties :stream stream)
(encode-message properties stream)
(finish-output stream)))


;; (,response (conspack:decode-stream (connection-stream-of ,connector))))
(defmacro with-response (command-name (&rest properties) response &body body)
`(destructuring-bind (&key ,@properties &allow-other-keys) ,response
(check-response ,response ,command-name)
Expand Down Expand Up @@ -111,3 +110,8 @@
(-> (connector :command :get-arena-list) ()
(with-response :arena-list (list) *message*
list)))


(defun register-game-stream (connector peer-id)
(-> (connector :command :register-game-stream :peer-id peer-id) ()
(with-response :ok () *message*)))
4 changes: 3 additions & 1 deletion client/src/packages.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@


(defpackage :mortar-combat
(:use :cl :ge :ge.util))
(:use :cl :ge :ge.util :mortar-combat.common)
(:export start
stop))
8 changes: 8 additions & 0 deletions common/packages.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(in-package :mortar-combat.def)


(defpackage :mortar-combat.common
(:use :cl :ge.ng :ge.util)
(:export process-command
encode-message
decode-message))
26 changes: 26 additions & 0 deletions common/process-command.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(in-package :mortar-combat.common)


(defgeneric process-command (command message)
(:method (command message)
(list :command :error
:type :unknown-command
:text "Unknown command")))


(defmethod process-command :around (command message)
(append (list :reply-for (getf message :message-id))
(handler-case
(call-next-method)
(serious-condition ()
'(:command :error
:type :unhandled-error
:text "Error during command execution")))))


(defun encode-message (message stream)
(conspack:encode message :stream stream))


(defun decode-message (stream)
(conspack:decode-stream stream))
18 changes: 16 additions & 2 deletions mortar-combat.asd
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,27 @@
(cl:in-package :mortar-combat.def)


(defsystem mortar-combat/common
:description "Common code between Mortar Combat client and server"
:version "0.0.1"
:author "Pavel Korolev"
:mailto "dev@borodust.org"
:license "GPLv3"
:depends-on (cl-conspack)
:serial t
:pathname "common/"
:components ((:file "packages")
(:file "process-command")))


(defsystem mortar-combat
:description "Multiplayer first-person shooter with mortars"
:version "0.0.1"
:author "Pavel Korolev"
:mailto "dev@borodust.org"
:license "GPLv3"
:depends-on (log4cl uiop cl-muth bodge-blobs cl-bodge usocket cl-conspack)
:depends-on (log4cl uiop cl-muth bodge-blobs cl-bodge usocket
mortar-combat/common)
:serial t
:pathname "client/src/"
:components ((:file "packages")
Expand Down Expand Up @@ -43,7 +57,7 @@
:author "Pavel Korolev"
:mailto "dev@borodust.org"
:license "GPLv3"
:depends-on (log4cl cl-muth cl-conspack usocket flexi-streams
:depends-on (log4cl cl-muth usocket flexi-streams mortar-combat/common
cl-bodge/engine cl-bodge/utils ironclad uuid)
:serial t
:pathname "proxy/"
Expand Down
2 changes: 1 addition & 1 deletion proxy/packages.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@


(defpackage :mortar-combat.proxy
(:use :cl :ge.ng :ge.util)
(:use :cl :ge.ng :ge.util :mortar-combat.common)
(:export start
stop))
21 changes: 2 additions & 19 deletions proxy/proxy.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,6 @@
(define-constant +routing-buffer-size+ (* 64 1024))


(defgeneric process-command (command message)
(:method (command message)
(list :command :error
:type :unknown-command
:text "Unknown command")))


(defmethod process-command :around (command message)
(append (list :reply-for (getf message :message-id))
(handler-case
(call-next-method)
(serious-condition ()
'(:command :error
:type :unhandled-error
:text "Error during command execution")))))


(defclass mortar-combat-proxy (enableable generic-system)
((proxy-socket :initform nil)
(peer-registry :initform (make-instance 'peer-registry) :reader peer-registry-of)
Expand All @@ -47,9 +30,9 @@
(let ((stream (usocket:socket-stream *connection*)))
(when (listen stream)
;; fixme: make async: read available chunk, don't wait for more
(let ((message (conspack:decode-stream stream)))
(let ((message (decode-message stream)))
(when (listp message)
(conspack:encode (reply-to message) :stream stream)
(encode-message (reply-to message) stream)
(force-output stream))))))


Expand Down

0 comments on commit d64ff70

Please sign in to comment.