X-Git-Url: https://jxself.org/git/?p=mudsync.git;a=blobdiff_plain;f=mudsync%2Fnetworking.scm;h=f30a1b99825199c913df3beb9424148a59f74ed2;hp=525997546ad024f05298bcacfc19e9b0af19fef0;hb=b1bf0fc9ec0ffcc06b7b1d4e422f3e28a00a27a6;hpb=095dde9158621c8bb3d690feaa0d525a76342eb9 diff --git a/mudsync/networking.scm b/mudsync/networking.scm index 5259975..f30a1b9 100644 --- a/mudsync/networking.scm +++ b/mudsync/networking.scm @@ -17,7 +17,7 @@ ;;; along with Mudsync. If not, see . (define-module (mudsync networking) - #:use-module (8sync systems actors) + #:use-module (8sync actors) #:use-module (8sync agenda) #:use-module (ice-9 format) #:use-module (ice-9 match) @@ -37,22 +37,24 @@ (define %default-server #f) (define %default-port 8889) -(define-class () +(define-actor () + ((start-listening + (lambda* (actor message + #:key (server %default-server) + (port %default-port)) + (nm-install-socket actor server port))) + (send-to-client + (lambda* (actor message #:key client data) + (nm-send-to-client-id actor client data))) + (new-client nm-new-client)) + (server-socket #:getter nm-server-socket) ;; mapping of client -> client-id (clients #:getter nm-clients #:init-thunk make-hash-table) ;; send input to this actor (send-input-to #:getter nm-send-input-to - #:init-keyword #:send-input-to) - (message-handler - #:init-value - (make-action-dispatch - ((start-listening actor message) - (nm-install-socket actor (message-ref message 'server %default-server) - (message-ref message 'port %default-port))) - ((send-to-client actor message client data) - (nm-send-to-client-id actor client data))))) + #:init-keyword #:send-input-to)) ;;; TODO: We should provide something like this, but this isn't used currently, ;;; and uses old deprecated code (the 8sync-port-remove stuff). @@ -116,10 +118,11 @@ (let loop () ;; (yield) ;; @@: Do we need this? (define client-connection (accept s)) - (8sync (nm-new-client nm s client-connection)) + (<- (actor-id nm) 'new-client + s client-connection) (loop))) -(define (nm-new-client nm s client-connection) +(define (nm-new-client nm message s client-connection) "Handle new client coming in to socket S" (define client-details (cdr client-connection)) (define client (car client-connection)) @@ -130,42 +133,39 @@ (sockaddr:addr client-details))) (fcntl client F_SETFL (logior O_NONBLOCK (fcntl client F_GETFL))) (hash-set! (nm-clients nm) client-id client) - (<- nm (nm-send-input-to nm) 'new-client #:client client-id) + (<- (nm-send-input-to nm) 'new-client #:client client-id) (nm-client-receive-loop nm client client-id)) (define (nm-client-receive-loop nm client client-id) "Make a method to receive client data" (define (loop) - (define line (string-trim-right (read-line client) #\return)) - (nm-handle-line nm client client-id line) - (cond - ;; The port's been closed for some reason, so stop looping - ((port-closed? client) - (nm-handle-port-closed nm client client-id)) - ;; We've reached the EOF object, which means we should close - ;; the port ourselves and stop looping - ((eof-object? (peek-char client)) - (nm-handle-port-eof nm client client-id)) - ;; Otherwise, let's read till the next line! - (else (loop)))) + (define line (read-line client)) + (if (eof-object? line) + (nm-handle-port-eof nm client client-id) + (begin + (nm-handle-line nm client client-id + (string-trim-right line #\return)) + (when (actor-alive? nm) + (loop))))) (loop)) (define (nm-handle-port-closed nm client client-id) "Handle a closed port" (format #t "DEBUG: handled closed port ~x\n" client-id) (hash-remove! (nm-clients nm) client-id) - (<- nm (nm-send-input-to nm) 'client-closed #:client client-id)) + (<-* `(#:actor ,nm) (nm-send-input-to nm) 'client-closed #:client client-id)) (define-method (nm-handle-port-eof nm client client-id) "Handle seeing an EOF on port" (format #t "DEBUG: handled eof-object on port ~x\n" client-id) (close client) (hash-remove! (nm-clients nm) client-id) - (<- nm (nm-send-input-to nm) 'client-closed #:client client-id)) + (<-* `(#:actor ,nm) (nm-send-input-to nm) 'client-closed + #:client client-id)) (define-method (nm-handle-line nm client client-id line) "Handle an incoming line of input from a client" - (<- nm (nm-send-input-to nm) 'client-input + (<-* `(#:actor ,nm) (nm-send-input-to nm) 'client-input #:data line #:client client-id))