-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rkt
60 lines (54 loc) · 1.98 KB
/
server.rkt
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
55
56
57
58
59
60
#lang racket
(require racket/tcp
racket/rerequire
"config.rkt")
(require (only-in errortrace print-error-trace))
(define commands-ns (begin
(dynamic-rerequire "commands.rkt")
(dynamic-require "commands.rkt" 'commands-ns)))
(define (printf+flush msg . args)
(apply printf (cons msg args))
(flush-output))
(define (print-exception exn)
(printf+flush "You got a bad error boy\n")
(printf+flush "~a\n" exn)
(printf+flush "~a\n"
(let ([out (open-output-string)])
(print-error-trace out exn)
(get-output-string out))))
(module+ main
(read-config)
(define out (current-output-port))
(define server (tcp-listen 1026 4 #t))
(printf+flush "Server running\n")
(define-values (client-in client-out) (tcp-accept server))
(printf+flush "Connection accepted\n")
(let loop ()
(let ([data (call-with-exception-handler
(lambda exc #f)
(lambda () (read client-in)))])
(match data
[(list 'exit)
(printf+flush "Ending trigged by user")
(tcp-close server)]
[(list 'token (? number? nb) (list-rest (? symbol? fun) args))
(dynamic-rerequire "commands.rkt")
(with-handlers ([(lambda _ #t) print-exception])
(let ([data (apply (eval fun commands-ns) args)])
(when data
(fprintf client-out "~s" (list* 'token nb data))
(flush-output client-out))))
(printf+flush "i loop\n")
(loop)]
[(list-rest (? symbol? fun) args)
(dynamic-rerequire "commands.rkt")
(with-handlers ([(lambda _ #t) print-exception])
(let ([data (apply (eval fun commands-ns) args)])
(when data
(fprintf client-out "~s" data)
(flush-output client-out))))
(printf+flush "i loop\n")
(loop)]
[_
(printf+flush "Abnormal ending\n")
(tcp-close server)]))))