blob: 4b19d854bbd680e196968118e7539286bcbe8f0a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
(module tcp-repl racket/base
(require mzlib/thread
racket/tcp)
(provide (prefix-out tcp-repl- run)
(prefix-out tcp-repl- logger))
(define logger (make-logger 'tcp-repl (current-logger)))
(define (run namespace port)
(thread
(lambda ()
(run-server port ; TCP-Port
(make-connection-handler namespace) ; connection handler
#f; timeout
(lambda (tcp-port max-allow-wait reuse?) ; listen handler
(log-message logger 'info (format "Listen on ~a:~a" "127.0.0.1" tcp-port) #t)
(tcp-listen tcp-port max-allow-wait reuse? "127.0.0.1"))
))))
(define (tcp-tostring port)
(let-values ([(address-from port-from address-to port-to) (tcp-addresses port #t)])
(format "~a:~a -> ~a:~a" address-from port-from address-to port-to)))
(define (make-connection-handler namespace)
(lambda (ip op)
(let/ec exit
(log-message logger 'info (format "New Connection ~a" (tcp-tostring ip)) #t)
(parameterize ([current-input-port ip]
[current-output-port op]
[current-error-port op]
[current-namespace namespace])
(read-eval-print-loop))
(log-message logger 'info (format "End Connection ~a" (tcp-tostring ip)) #t)
(close-output-port op))))
) ; end module tcp-repl
|