server.scm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. ;;; Repl server
  2. ;; Copyright (C) 2003, 2010, 2011 Free Software Foundation, Inc.
  3. ;; This library is free software; you can redistribute it and/or
  4. ;; modify it under the terms of the GNU Lesser General Public
  5. ;; License as published by the Free Software Foundation; either
  6. ;; version 3 of the License, or (at your option) any later version.
  7. ;;
  8. ;; This library is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;; Lesser General Public License for more details.
  12. ;;
  13. ;; You should have received a copy of the GNU Lesser General Public
  14. ;; License along with this library; if not, write to the Free Software
  15. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. ;; 02110-1301 USA
  17. ;;; Code:
  18. (define-module (system repl server)
  19. #:use-module (system repl repl)
  20. #:use-module (ice-9 threads)
  21. #:export (make-tcp-server-socket
  22. make-unix-domain-server-socket
  23. run-server
  24. spawn-server
  25. stop-server-and-clients!))
  26. (define *open-sockets* '())
  27. (define sockets-lock (make-mutex))
  28. (define (close-socket! s)
  29. (with-mutex sockets-lock
  30. (set! *open-sockets* (delq! s *open-sockets*)))
  31. ;; Close-port could block or raise an exception flushing buffered
  32. ;; output. Hmm.
  33. (close-port s))
  34. (define (add-open-socket! s)
  35. (with-mutex sockets-lock
  36. (set! *open-sockets* (cons s *open-sockets*))))
  37. (define (stop-server-and-clients!)
  38. (cond
  39. ((with-mutex sockets-lock
  40. (and (pair? *open-sockets*)
  41. (car *open-sockets*)))
  42. => (lambda (s)
  43. (close-socket! s)
  44. (stop-server-and-clients!)))))
  45. (define* (make-tcp-server-socket #:key
  46. (host #f)
  47. (addr (if host (inet-aton host) INADDR_LOOPBACK))
  48. (port 37146))
  49. (let ((sock (socket PF_INET SOCK_STREAM 0)))
  50. (setsockopt sock SOL_SOCKET SO_REUSEADDR 1)
  51. (bind sock AF_INET addr port)
  52. sock))
  53. (define* (make-unix-domain-server-socket #:key (path "/tmp/guile-socket"))
  54. (let ((sock (socket PF_UNIX SOCK_STREAM 0)))
  55. (setsockopt sock SOL_SOCKET SO_REUSEADDR 1)
  56. (bind sock AF_UNIX path)
  57. sock))
  58. (define call-with-sigint
  59. (if (not (provided? 'posix))
  60. (lambda (thunk) (thunk))
  61. (lambda (thunk)
  62. (let ((handler #f))
  63. (dynamic-wind
  64. (lambda ()
  65. (set! handler
  66. (sigaction SIGINT (lambda (sig) (throw 'interrupt)))))
  67. thunk
  68. (lambda ()
  69. (if handler
  70. ;; restore Scheme handler, SIG_IGN or SIG_DFL.
  71. (sigaction SIGINT (car handler) (cdr handler))
  72. ;; restore original C handler.
  73. (sigaction SIGINT #f))))))))
  74. (define* (run-server #:optional (server-socket (make-tcp-server-socket)))
  75. (define (accept-new-client)
  76. (catch #t
  77. (lambda () (call-with-sigint (lambda () (accept server-socket))))
  78. (lambda (k . args)
  79. (cond
  80. ((port-closed? server-socket)
  81. ;; Shutting down.
  82. #f)
  83. ((eq? k 'interrupt)
  84. ;; Interrupt.
  85. (close-socket! server-socket)
  86. #f)
  87. (else
  88. (warn "Error accepting client" k args)
  89. ;; Retry after a timeout.
  90. (sleep 1)
  91. (accept-new-client))))))
  92. (sigaction SIGPIPE SIG_IGN)
  93. (add-open-socket! server-socket)
  94. (listen server-socket 5)
  95. (let lp ((client (accept-new-client)))
  96. ;; If client is false, we are shutting down.
  97. (if client
  98. (let ((client-socket (car client))
  99. (client-addr (cdr client)))
  100. (add-open-socket! client-socket)
  101. (make-thread serve-client client-socket client-addr)
  102. (lp (accept-new-client))))))
  103. (define* (spawn-server #:optional (server-socket (make-tcp-server-socket)))
  104. (make-thread run-server server-socket))
  105. (define (serve-client client addr)
  106. (with-continuation-barrier
  107. (lambda ()
  108. (with-input-from-port client
  109. (lambda ()
  110. (with-output-to-port client
  111. (lambda ()
  112. (with-error-to-port client
  113. (lambda ()
  114. (with-fluids ((*repl-stack* '()))
  115. (start-repl))))))))))
  116. (close-socket! client))