Switch to using the <- syntax for everything
[mudsync.git] / mudsync / networking.scm
1 ;;; Mudsync --- Live hackable MUD
2 ;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
3 ;;;
4 ;;; This file is part of Mudsync.
5 ;;;
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.
10 ;;;
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.
15 ;;;
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/>.
18
19 (define-module (mudsync networking)
20   #:use-module (8sync systems actors)
21   #:use-module (8sync agenda)
22   #:use-module (ice-9 format)
23   #:use-module (ice-9 match)
24   #:use-module (ice-9 rdelim)
25   #:use-module (oop goops)
26
27   #:export (;; Should we be exporting these?
28             %default-server
29             %default-port
30
31             <network-manager>
32             nm-close-everything))
33
34 ;;; Networking
35 ;;; ==========
36
37 (define %default-server #f)
38 (define %default-port 8889)
39
40 (define-class <network-manager> (<actor>)
41   (server-socket #:getter nm-server-socket)
42   ;; mapping of client -> client-id
43   (clients #:getter nm-clients
44            #:init-thunk make-hash-table)
45   ;; send input to this actor
46   (send-input-to #:getter nm-send-input-to
47                  #:init-keyword #:send-input-to)
48   (message-handler
49    #:init-value
50    (make-action-dispatch
51     ((start-listening actor message)
52      (nm-install-socket actor (message-ref message 'server %default-server)
53                         (message-ref message 'port %default-port)))
54     ((send-to-client actor message client data)
55      (nm-send-to-client-id actor client data)))))
56
57 ;;; TODO: We should provide something like this, but this isn't used currently,
58 ;;;    and uses old deprecated code (the 8sync-port-remove stuff).
59 ;; (define-method (nm-close-everything (nm <network-manager>) remove-from-agenda)
60 ;;   "Shut it down!"
61 ;;   ;; close all clients
62 ;;   (hash-for-each
63 ;;    (lambda (_ client)
64 ;;      (close client)
65 ;;      (if remove-from-agenda
66 ;;          (8sync-port-remove client)))
67 ;;    (nm-clients nm))
68 ;;   ;; reset the clients list
69 ;;   (set! (nm-clients) (make-hash-table))
70 ;;   ;; close the server
71 ;;   (close (nm-server-socket nm))
72 ;;   (if remove-from-agenda
73 ;;       (8sync-port-remove (nm-server-socket nm))))
74
75 ;; Maximum number of backlogged connections when we listen
76 (define %maximum-backlog-conns 128)     ; same as SOMAXCONN on Linux 2.X,
77                                         ; says the intarwebs
78
79 (define (nm-install-socket nm server port)
80   "Install socket on SERVER with PORT"
81   (define s
82     (socket PF_INET  ; ipv4
83             SOCK_STREAM  ; two-way connection-based byte stream
84             0))
85   (define addr
86     (if server
87         (inet-pton AF_INET server)
88         INADDR_LOOPBACK))
89
90   ;; Totally mimed from the Guile manual.  Not sure if we need this, but:
91   ;; http://www.unixguide.net/network/socketfaq/4.5.shtml
92   (setsockopt s SOL_SOCKET SO_REUSEADDR 1) ; reuse port even if port is busy
93   ;; Connecting to a non-specific address:
94   ;;   (bind s AF_INET INADDR_ANY port)
95   ;; Should this be an option?  Guess I don't know why we'd need it
96   ;; @@: If we wanted to support listening on a particular hostname,
97   ;;   could see 8sync's irc.scm...
98   (bind s AF_INET addr port)
99   ;; Listen to connections
100   (listen s %maximum-backlog-conns)
101
102   ;; Make port non-blocking
103   (fcntl s F_SETFL (logior O_NONBLOCK (fcntl s F_GETFL)))
104
105   ;; @@: This is used in Guile's http server under the commit:
106   ;;       * module/web/server/http.scm (http-open): Ignore SIGPIPE. Keeps the
107   ;;         server from dying in some circumstances.
108   ;;   (sigaction SIGPIPE SIG_IGN)
109   ;; Will this break other things that use pipes for us though?
110
111   (slot-set! nm 'server-socket s)
112
113   (format #t "Listening for clients in pid: ~s\n" (getpid))
114
115   ;; TODO: set up periodic close of idle connections?
116   (let loop ()
117     ;; (yield)  ;; @@: Do we need this?
118     (define client-connection (accept s))
119     (8sync (nm-new-client nm s client-connection))
120     (loop)))
121
122 (define (nm-new-client nm s client-connection)
123   "Handle new client coming in to socket S"
124   (define client-details (cdr client-connection))
125   (define client (car client-connection))
126   (define client-id (big-random-number))
127   (format #t "New client: ~s\n" client-details)
128   (format #t "Client address: ~s\n"
129           (gethostbyaddr
130            (sockaddr:addr client-details)))
131   (fcntl client F_SETFL (logior O_NONBLOCK (fcntl client F_GETFL)))
132   (hash-set! (nm-clients nm) client-id client)
133   (<- nm (nm-send-input-to nm) 'new-client #:client client-id)
134   (nm-client-receive-loop nm client client-id))
135
136 (define (nm-client-receive-loop nm client client-id)
137   "Make a method to receive client data"
138   (define (loop)
139     (define line (read-line client))
140     (if (eof-object? line)
141         (nm-handle-port-eof nm client client-id)
142         (begin
143           (nm-handle-line nm client client-id
144                           (string-trim-right line #\return))
145           (loop))))
146   (loop))
147
148 (define (nm-handle-port-closed nm client client-id)
149   "Handle a closed port"
150   (format #t "DEBUG: handled closed port ~x\n" client-id)
151   (hash-remove! (nm-clients nm) client-id)
152   (<- nm (nm-send-input-to nm) 'client-closed #:client client-id))
153
154 (define-method (nm-handle-port-eof nm client client-id)
155   "Handle seeing an EOF on port"
156   (format #t "DEBUG: handled eof-object on port ~x\n" client-id)
157       (close client)
158   (hash-remove! (nm-clients nm) client-id)
159   (<- nm (nm-send-input-to nm) 'client-closed #:client client-id))
160
161 (define-method (nm-handle-line nm client client-id line)
162   "Handle an incoming line of input from a client"
163   (<- nm (nm-send-input-to nm) 'client-input
164       #:data line
165       #:client client-id))
166
167 (define-method (nm-send-to-client-id nm client-id data)
168   "Send DATA to TO-CLIENT id"
169   (define client-obj (hash-ref (nm-clients nm) client-id))
170   (if (not client-obj)
171       (throw 'no-such-client
172              "Asked to send data to client but that client doesn't exist"
173              #:client-id client-id
174              #:data data))
175   (display data client-obj))