Permalink
Browse files

Split into client and proxy server

  • Loading branch information...
borodust committed Apr 17, 2017
1 parent 369ad98 commit 78d757ce5920161381d079af88811a855f0f90bd
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.
@@ -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")
@@ -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")))
@@ -0,0 +1,6 @@
(in-package :mortar-combat.proxy)
(defmethod process-command ((command (eql :version)) message)
(list :command :version
:version +server-version+))
@@ -0,0 +1,5 @@
(in-package :mortar-combat.proxy)
(defclass mortar-combat-context ()
((arenas :initform (make-hash-table :test #'equal))))
@@ -0,0 +1,7 @@
(in-package :mortar-combat.def)
(defpackage :mortar-combat.proxy
(:use :cl :ge.ng :ge.util)
(:export start
stop))
@@ -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.