Skip to content

Commit

Permalink
Add ping command
Browse files Browse the repository at this point in the history
  • Loading branch information
borodust committed Apr 18, 2017
1 parent d64ff70 commit 232f276
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
33 changes: 22 additions & 11 deletions client/src/connector.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@
(loop while enabled-p
do (log-errors
(usocket:wait-for-input connection)
(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)))
(progn
(remhash message-id message-table)
(funcall handler message))
(log:error "Handler not found for message with id ~A" message-id)))))
(let ((message (decode-message (connection-stream-of this))))
(if-let ((reply-id (getf message :reply-for)))
(with-instance-lock-held (this)
(if-let ((handler (gethash reply-id message-table)))
(progn
(remhash reply-id message-table)
(funcall handler message))
(log:error "Handler not found for message with id ~A" reply-id)))
(encode-message (process-command (getf message :command) message)
(connection-stream-of this)))))
finally (usocket:socket-close connection)))))


Expand Down Expand Up @@ -67,9 +69,9 @@


(defmacro with-response (command-name (&rest properties) response &body body)
`(destructuring-bind (&key ,@properties &allow-other-keys) ,response
(check-response ,response ,command-name)
,@body))
`(destructuring-bind (&key ,@properties &allow-other-keys) ,response
(check-response ,response ,command-name)
,@body))


(defmethod dispatch ((this connector) (task function) invariant &rest keys
Expand Down Expand Up @@ -115,3 +117,12 @@
(defun register-game-stream (connector peer-id)
(-> (connector :command :register-game-stream :peer-id peer-id) ()
(with-response :ok () *message*)))


(defun ping-peer (connector)
(-> (connector :command :ping) ()
(with-response :ok () *message*)))


(defmethod process-command ((command (eql :ping)) message)
+ok-reply+)
5 changes: 3 additions & 2 deletions common/packages.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@


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


(define-constant +ok-reply+ (list :command :ok)
:test #'equal)


(defgeneric process-command (command message)
(:method (command message)
(list :command :error
Expand Down
2 changes: 1 addition & 1 deletion mortar-combat.asd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
:author "Pavel Korolev"
:mailto "dev@borodust.org"
:license "GPLv3"
:depends-on (cl-conspack)
:depends-on (cl-bodge/utils cl-conspack)
:serial t
:pathname "common/"
:components ((:file "packages")
Expand Down
4 changes: 0 additions & 4 deletions proxy/commands.lisp
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
(in-package :mortar-combat.proxy)


(define-constant +ok-reply+ (list :command :ok)
:test #'equal)


(defmacro when-peer-identified (&body body)
`(if *peer*
(progn ,@body)
Expand Down

0 comments on commit 232f276

Please sign in to comment.