Permalink
Browse files

Extract common code between client and server

  • Loading branch information...
borodust committed Apr 18, 2017
1 parent d756789 commit d64ff7050a855f2a265e6f521074299215162f01
Showing with 64 additions and 27 deletions.
  1. +8 −4 client/src/connector.lisp
  2. +3 −1 client/src/packages.lisp
  3. +8 −0 common/packages.lisp
  4. +26 −0 common/process-command.lisp
  5. +16 −2 mortar-combat.asd
  6. +1 −1 proxy/packages.lisp
  7. +2 −19 proxy/proxy.lisp
@@ -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)))
@@ -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)
@@ -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)
@@ -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*)))
@@ -2,4 +2,6 @@
(defpackage :mortar-combat
(:use :cl :ge :ge.util))
(:use :cl :ge :ge.util :mortar-combat.common)
(:export start
stop))
@@ -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))
@@ -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))
@@ -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")
@@ -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/"
@@ -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))
@@ -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)
@@ -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))))))

0 comments on commit d64ff70

Please sign in to comment.