Skip to content

Commit

Permalink
Split into client and proxy server
Browse files Browse the repository at this point in the history
  • Loading branch information
borodust committed Apr 17, 2017
1 parent 369ad98 commit 78d757c
Show file tree
Hide file tree
Showing 25 changed files with 111 additions and 1 deletion.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
19 changes: 18 additions & 1 deletion mortar-combat.asd
Expand Up @@ -11,7 +11,7 @@
:license "GPLv3"
:depends-on (log4cl uiop cl-muth bodge-blobs cl-bodge)
:serial t
:pathname "src/"
:pathname "client/src/"
:components ((:file "packages")
(:file "utils")
(:file "camera")
Expand All @@ -32,4 +32,21 @@
:license "GPLv3"
:depends-on (bodge-blobs mortar-combat cl-bodge/distribution)
:serial t
:pathname "client/"
:components ((:file "mortar-combat.dist")))


(defsystem mortar-combat/proxy
:description "Proxy server for Mortar Combat"
:version "0.0.1"
:author "Pavel Korolev"
:mailto "dev@borodust.org"
:license "GPLv3"
:depends-on (log4cl cl-muth cl-conspack usocket flexi-streams
cl-bodge/engine cl-bodge/utils)
:serial t
:pathname "proxy/"
:components ((:file "packages")
(:file "context")
(:file "proxy")
(:file "commands")))
6 changes: 6 additions & 0 deletions proxy/commands.lisp
@@ -0,0 +1,6 @@
(in-package :mortar-combat.proxy)


(defmethod process-command ((command (eql :version)) message)
(list :command :version
:version +server-version+))
5 changes: 5 additions & 0 deletions proxy/context.lisp
@@ -0,0 +1,5 @@
(in-package :mortar-combat.proxy)


(defclass mortar-combat-context ()
((arenas :initform (make-hash-table :test #'equal))))
7 changes: 7 additions & 0 deletions proxy/packages.lisp
@@ -0,0 +1,7 @@
(in-package :mortar-combat.def)


(defpackage :mortar-combat.proxy
(:use :cl :ge.ng :ge.util)
(:export start
stop))
75 changes: 75 additions & 0 deletions proxy/proxy.lisp
@@ -0,0 +1,75 @@
(in-package :mortar-combat.proxy)


(defvar *main-latch* (mt:make-latch))


(define-constant +server-version+ 1)


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


(defclass mortar-combat-proxy (thread-bound-system)
((proxy-socket :initform nil)
(info-socket :initform nil)))


(defun process-request (this connection)
;; fixme: record connection's last communication timestamp
;; to autoclose idle connections
(run (-> (this) ()
(let ((stream (usocket:socket-stream connection)))
(when (listen stream)
;; fixme: make async
(let ((message (conspack:decode-stream stream)))
(when (listp message)
(conspack:encode (process-command (getf message :command) message)
:stream stream)
(force-output stream))))))))


(defmethod initialize-system :after ((this mortar-combat-proxy))
(with-slots (proxy-socket info-socket) this
(setf proxy-socket (usocket:socket-listen #(127 0 0 1) 8222
:element-type '(unsigned-byte 8))
info-socket (usocket:socket-listen #(127 0 0 1) 8778
:element-type '(unsigned-byte 8)))
(in-new-thread "socket-listener"
(let ((sockets (list proxy-socket info-socket)))
(loop while (enabledp this) do
(loop for connection in (cddr (usocket:wait-for-input sockets))
when (usocket:socket-state connection)
do (process-request this connection))
(cond
((usocket:socket-state info-socket)
(push (usocket:socket-accept info-socket) (cddr sockets)))))))))


(defmethod make-system-context ((this mortar-combat-proxy))
(make-instance 'mortar-combat-context))


(defmethod discard-system :before ((this mortar-combat-proxy))
(with-slots (proxy-socket info-socket) this
(usocket:socket-close proxy-socket)
(usocket:socket-close info-socket)))


(defun start ()
(startup '(:engine (:systems (mortar-combat-proxy)))))


(defun stop ()
(in-new-thread "exit-thread"
(shutdown)))


(defun main (args)
(declare (ignore args))
(start)
(mt:wait-for-latch *main-latch*))

0 comments on commit 78d757c

Please sign in to comment.