- (let ((s (socket PF_INET ; ipv4
- SOCK_STREAM ; two-way connection-based byte stream
- 0))
- (addr (if server
- (inet-pton AF_INET server)
- INADDR_LOOPBACK)))
- ;; Totally mimed from the Guile manual. Not sure if we need this, but:
- ;; http://www.unixguide.net/network/socketfaq/4.5.shtml
- (setsockopt s SOL_SOCKET SO_REUSEADDR 1) ; reuse port even if port is busy
- ;; Connecting to a non-specific address:
- ;; (bind s AF_INET INADDR_ANY port)
- ;; Should this be an option? Guess I don't know why we'd need it
- ;; @@: If we wanted to support listening on a particular hostname,
- ;; could see 8sync's irc.scm...
- (bind s AF_INET addr port)
- ;; Listen to connections
- (listen s %maximum-backlog-conns)
-
- ;; Throw a system-error rather than block on an (accept)
- ;; that has nothing to do
- (fcntl s F_SETFL
- (logior O_NONBLOCK
- (fcntl s F_GETFL)))
-
- ;; @@: This is used in Guile's http server under the commit:
- ;; * module/web/server/http.scm (http-open): Ignore SIGPIPE. Keeps the
- ;; server from dying in some circumstances.
- ;; (sigaction SIGPIPE SIG_IGN)
- ;; Will this break other things that use pipes for us though?
-
- (slot-set! nm 'server-socket s)
-
- (format #t "Listening for clients in pid: ~s\n" (getpid))
- (8sync-port s #:read (lambda (s) (nm-new-client nm s)))
- ;; TODO: set up periodic close of idle connections?
- ))
-
-(define (nm-new-client nm s)
+ (define s
+ (socket PF_INET ; ipv4
+ SOCK_STREAM ; two-way connection-based byte stream
+ 0))
+ (define addr
+ (if server
+ (inet-pton AF_INET server)
+ INADDR_LOOPBACK))
+
+ ;; Totally mimed from the Guile manual. Not sure if we need this, but:
+ ;; http://www.unixguide.net/network/socketfaq/4.5.shtml
+ (setsockopt s SOL_SOCKET SO_REUSEADDR 1) ; reuse port even if port is busy
+ ;; Connecting to a non-specific address:
+ ;; (bind s AF_INET INADDR_ANY port)
+ ;; Should this be an option? Guess I don't know why we'd need it
+ ;; @@: If we wanted to support listening on a particular hostname,
+ ;; could see 8sync's irc.scm...
+ (bind s AF_INET addr port)
+ ;; Listen to connections
+ (listen s %maximum-backlog-conns)
+
+ ;; Make port non-blocking
+ (fcntl s F_SETFL (logior O_NONBLOCK (fcntl s F_GETFL)))
+
+ ;; @@: This is used in Guile's http server under the commit:
+ ;; * module/web/server/http.scm (http-open): Ignore SIGPIPE. Keeps the
+ ;; server from dying in some circumstances.
+ ;; (sigaction SIGPIPE SIG_IGN)
+ ;; Will this break other things that use pipes for us though?
+
+ (slot-set! nm 'server-socket s)
+
+ (format #t "Listening for clients in pid: ~s\n" (getpid))
+
+ ;; TODO: set up periodic close of idle connections?
+ (let loop ()
+ ;; (yield) ;; @@: Do we need this?
+ (define client-connection (accept s))
+ (8sync (nm-new-client nm s client-connection))
+ (loop)))
+
+(define (nm-new-client nm s client-connection)