-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho4.ss
More file actions
54 lines (51 loc) · 1.49 KB
/
echo4.ss
File metadata and controls
54 lines (51 loc) · 1.49 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env swish
(define (echo:start&link ip op)
(define (reader who)
(let ([x (get-bytevector-some ip)])
(unless (eof-object? x)
(send who `#(echo ,x))
(reader who))))
(define (init)
(let ([me self])
(spawn&link
(lambda ()
(reader me))))
`#(ok #f))
(define (terminate reason state)
(close-port op)
'ok)
(define (handle-call msg from state) (match msg))
(define (handle-cast msg state) (match msg))
(define (handle-info msg state)
(match msg
[#(echo ,bv)
(put-bytevector op bv)
(flush-output-port op)
`#(no-reply ,state)]))
(gen-server:start&link #f))
(define (echo-server:start&link)
(define-state-tuple <echo-server> listener)
(define (init)
(let ([listener (listen-tcp "::" 5300 self)])
(printf "Waiting for connection on port ~a\n"
(listener-port-number listener))
`#(ok
,(<echo-server> make
[listener listener]))))
(define (terminate reason state)
(close-tcp-listener ($state listener))
'ok)
(define (handle-call msg from state) (match msg))
(define (handle-cast msg state) (match msg))
(define (handle-info msg state)
(match msg
[#(accept-tcp ,_ ,ip ,op)
(printf "Handling new connection\n")
(echo:start&link ip op)
`#(no-reply ,state)]
[#(accept-tcp-failed ,_ ,_ ,_)
(printf "Good bye!\n")
`#(stop ,msg ,state)]))
(gen-server:start&link 'echo-server))
(echo-server:start&link)
(receive)