original commit: 5764e92362d8de22950308f24e2bdbeee06f3bf5
This commit is contained in:
Matthew Flatt 2005-04-15 16:06:42 +00:00
parent a1a0262c65
commit 25643f3746

View File

@ -74,33 +74,50 @@
(dynamic-wind
void
(lambda ()
;; loop to handle connections
(let loop ()
(with-handlers ([exn:fail:network? handle-exn])
;; Make a custodian for the next session:
(let ([c (make-custodian)])
(parameterize ([current-custodian c])
;; disable breaks during session set-up...
(parameterize-break #f
;; ... but enable breaks while blocked on an accept:
(let-values ([(r w) ((if can-break?
tcp-accept/enable-break
tcp-accept)
l)])
;; Handler thread:
(let ([t (thread (lambda ()
(when can-break?
(break-enabled #t))
(handler r w)))])
;; Clean-up and timeout thread:
(thread (lambda ()
(sync/timeout connection-timeout t)
(when (thread-running? t)
;; Only happens if connection-timeout is not #f
(break-thread t))
(sync/timeout connection-timeout t)
(custodian-shutdown-all c)))))))))
(loop)))
;; Introduce parameter dynamic binding that we can mutate:
(parameterize ([current-custodian (current-custodian)])
;; All connections should use the same parameterization,
;; to facilitate transferring continuations from one
;; connection to another:
(let ([paramz (current-parameterization)])
;; Loop to handle connections:
(let loop ()
(with-handlers ([exn:fail:network? handle-exn])
;; Make a custodian for the next session:
(let ([c (make-custodian)])
(parameterize ([current-custodian c])
;; disable breaks during session set-up...
(parameterize-break #f
;; ... but enable breaks while blocked on an accept:
(let-values ([(r w) ((if can-break?
tcp-accept/enable-break
tcp-accept)
l)])
;; Handler thread:
(let ([t (thread (lambda ()
;; First, install the parameterization
;; used for all connections:
(call-with-parameterization
paramz
(lambda ()
;; Install this connection's custodian
;; for this thread in the shared
;; parameterization:
(current-custodian c)
;; Enable breaking:
(when can-break?
(break-enabled #t))
;; Call the handler
(handler r w)))))])
;; Clean-up and timeout thread:
(thread (lambda ()
(sync/timeout connection-timeout t)
(when (thread-running? t)
;; Only happens if connection-timeout is not #f
(break-thread t))
(sync/timeout connection-timeout t)
(custodian-shutdown-all c)))))))))
(loop)))))
(lambda () (tcp-close l))))))
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;