1 ;;; Mudsync --- Live hackable MUD
2 ;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
4 ;;; This file is part of Mudsync.
6 ;;; Mudsync is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or
9 ;;; (at your option) any later version.
11 ;;; Mudsync is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;; General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with Mudsync. If not, see <http://www.gnu.org/licenses/>.
19 (use-modules (8sync systems actors)
26 (define %default-server #f)
27 (define %default-port 8889)
29 (define-class <network-manager> (<actor>)
30 (server-socket #:accessor nm-server-socket)
31 ;; mapping of client -> client-id
32 (clients #:accessor nm-clients
33 #:init-thunk make-hash-table)
34 ;; send input to this actor
35 (send-input-to #:getter nm-send-input-to
36 #:init-keyword #:send-input-to)
40 ((start-listening actor message)
41 (nm-install-socket actor (message-ref message 'server %default-server)
42 (message-ref message 'port %default-port)))
43 ((send-to-client actor message to-client data)
44 (nm-send-to-client-id actor to-client data)))))
46 (define-method (nm-close-everything (nm <network-manager>) remove-from-agenda)
52 (if remove-from-agenda
53 (8sync-port-remove client)))
55 ;; reset the clients list
56 (set! (nm-clients) (make-hash-table))
58 (close (nm-server-socket nm))
59 (if remove-from-agenda
60 (8sync-port-remove (nm-server-socket nm))))
62 ;; Maximum number of backlogged connections when we listen
63 (define %maximum-backlog-conns 128) ; same as SOMAXCONN on Linux 2.X,
66 (define-method (nm-install-socket (nm <network-manager>) server port)
67 "Install socket on SERVER with PORT"
68 (let ((s (socket PF_INET ; ipv4
69 SOCK_STREAM ; two-way connection-based byte stream
72 (inet-pton AF_INET server)
74 ;; Totally mimed from the Guile manual. Not sure if we need this, but:
75 ;; http://www.unixguide.net/network/socketfaq/4.5.shtml
76 (setsockopt s SOL_SOCKET SO_REUSEADDR 1) ; reuse port even if port is busy
77 ;; Connecting to a non-specific address:
78 ;; (bind s AF_INET INADDR_ANY port)
79 ;; Should this be an option? Guess I don't know why we'd need it
80 ;; @@: If we wanted to support listening on a particular hostname,
81 ;; could see 8sync's irc.scm...
82 (bind s AF_INET addr port)
83 ;; Listen to connections
84 (listen s %maximum-backlog-conns)
86 ;; Throw a system-error rather than block on an (accept)
87 ;; that has nothing to do
92 ;; @@: This is used in Guile's http server under the commit:
93 ;; * module/web/server/http.scm (http-open): Ignore SIGPIPE. Keeps the
94 ;; server from dying in some circumstances.
95 ;; (sigaction SIGPIPE SIG_IGN)
96 ;; Will this break other things that use pipes for us though?
98 (set! (nm-server-socket nm) s)
100 (format #t "Listening for clients in pid: ~s\n" (getpid))
101 (8sync-port s #:read (lambda (s) (nm-new-client nm s)))
102 ;; TODO: set up periodic close of idle connections?
105 (define-method (nm-new-client (nm <network-manager>) s)
106 "Handle new client coming in to socket S"
107 (let* ((client-connection (accept s))
108 (client-details (cdr client-connection))
109 (client (car client-connection)))
110 (format #t "New client: ~s\n" client-details)
111 (format #t "Client address: ~s\n"
113 (sockaddr:addr client-details)))
115 (let ((client-id (big-random-number)))
116 (hash-set! (nm-clients nm) client-id client)
117 (8sync-port client #:read (nm-make-client-receive nm client-id)))))
119 (define-method (nm-make-client-receive (nm <network-manager>) client-id)
120 "Make a method to receive client data"
122 (define (reset-buffer)
124 (define (should-read-char client)
125 (and (not (port-closed? client))
127 (not (eof-object? (peek-char client)))))
128 (define (receive-handler client)
129 (while (should-read-char client)
130 (set! buffer (cons (read-char client) buffer))
132 (;; @@: Do we need the "char?"
133 (#\newline #\return (? char? line-chars) ...)
134 (let ((ready-line (list->string (reverse line-chars))))
138 (nm-handle-line nm client client-id ready-line)))
140 ;; Shut things down on closed port or EOF object
142 ((port-closed? client)
143 (nm-handle-port-closed nm client client-id))
144 ((and (char-ready? client)
145 (eof-object? (peek-char client)))
146 (nm-handle-port-eof nm client client-id))))
149 (define-method (nm-handle-port-closed (nm <network-manager>) client client-id)
150 "Handle a closed port"
151 (format #t "DEBUG: handled closed port ~x\n" client-id)
152 (8sync-port-remove client)
153 (hash-remove! (nm-clients nm) client-id))
155 (define-method (nm-handle-port-eof (nm <network-manager>) client client-id)
156 "Handle seeing an EOF on port"
157 (format #t "DEBUG: handled eof-object on port ~x\n" client-id)
159 (8sync-port-remove client)
160 (hash-remove! (nm-clients nm) client-id))
162 (define-method (nm-handle-line (nm <network-manager>) client client-id line)
163 "Handle an incoming line of input from a client"
164 (format #t "~x got line: ~s\n" client-id line)
165 (nm-send-to-client-id nm client-id "Thank, you, processed.\n" ))
167 (define-method (nm-send-to-client-id (nm <network-manager>) client-id data)
168 "Send DATA to TO-CLIENT id"
170 (hash-ref (nm-clients nm) client-id)))
172 ; (ez-run-hive hive (list (bootstrap-message hive (actor-id nm) 'start-listening)))
175 ;; (define-method (nm-close-port (nm <network-manager>)))
178 (define-generic gm-init-world)
179 (define-class <game-master> (<actor>)
182 (make-action-dispatch