#:use-module (ice-9 receive)
#:use-module (ice-9 threads)
#:use-module (srfi srfi-9)
- #:use-module (srfi srfi-9 gnu))
+ #:use-module (srfi srfi-9 gnu)
+ #:use-module (system repl server)
+ #:use-module (system repl coop-server))
;;; Engine Inner Properties
(make-engine-record entities mutex running-mutex systems)
engine?
(entities engine-entities set-engine-entities!)
- (mutex engine-mutex set-engine-mutex!)
(running-mutex engine-running-mutex set-engine-running-mutex!)
(systems engine-systems set-engine-systems!))
(make-engine-record
(make-entity-set (new-entity (default-engine-inner-properties)))
(make-mutex)
- (make-mutex)
systems))
(define-syntax define-engine
;;; Engine execution
-(define (start-engine engine)
- (cond ((not (engine-running? engine))
- (with-mutex (engine-running-mutex engine)
- (let loop ()
- (let ((t (current-utime))
- (delay 0)
- (halt #f))
- (with-mutex (engine-mutex engine)
- (for-each
- (lambda (s) (eval-system s engine))
- (engine-systems engine))
- (set! delay (- (inexact->exact (* (engine-property engine 'step) 1000000))
- (- (current-utime) t)))
- (set! halt (engine-stopping? engine #:clean #t)))
- (cond ((not halt)
- (cond ((> delay 0)
- (usleep delay)))
- (loop)))))))))
+(define* (start-engine engine #:optional (socket #f))
+ (catch
+ #t
+ (lambda ()
+ (with-mutex (engine-running-mutex engine)
+ (let loop ((coop-server (if socket (spawn-coop-repl-server socket) #f)))
+ (let ((t (current-utime))
+ (delay 0)
+ (halt #f))
+ (for-each
+ (lambda (s) (eval-system s engine))
+ (engine-systems engine))
+ (set! delay (- (inexact->exact (* (engine-property engine 'step) 1000000))
+ (- (current-utime) t)))
+ (set! halt (engine-stopping? engine #:clean #t))
+ (if coop-server
+ (poll-coop-repl-server coop-server))
+ (cond ((not halt)
+ (cond ((> delay 0)
+ (usleep delay)))
+ (loop coop-server))
+ (else
+ (if coop-server
+ (stop-server-and-clients!))))))))
+ (lambda (key args)
+ #f)))
(define (eval-system system engine)
(call-with-values