52 lines
1.4 KiB
Racket
52 lines
1.4 KiB
Racket
#lang racket/base
|
|
|
|
(define (listener port)
|
|
(transition 'no-state
|
|
(role/act (tcp-listener port)
|
|
#:topic t
|
|
#:on-presence (spawn (connection-handler t))))))
|
|
|
|
(define (connection-handler topic)
|
|
(define-values (cin cout in-topic out-topic) (tcp-accept topic))
|
|
(transition 'no-state
|
|
(cin (tcp-credit 1) #:mode 'feedback)
|
|
(role/act in-topic
|
|
[(tcp-channel _ _ (or (== #"\4") (? eof-object?)))
|
|
(kill)]
|
|
[(tcp-channel _ _ (? bytes? bytev))
|
|
(list (cin (tcp-credit 1) #:mode 'feedback)
|
|
(cout bytev))])))
|
|
|
|
(define (main port)
|
|
(ground-vm
|
|
(transition 'no-state
|
|
(spawn tcp-driver)
|
|
(spawn (listener port)))))
|
|
|
|
(main 5999)
|
|
|
|
(define (tcp-accept topic)
|
|
(match-define (topic (tcp-channel remote-addr local-addr _)) topic)
|
|
(values (match-lambda
|
|
[(val #:mode 'feedback)
|
|
(send-feedback (tcp-channel remote-addr local-addr val))])
|
|
(match-lambda
|
|
[(val)
|
|
(send-message (tcp-channel local-addr remote-addr val))])
|
|
(topic-subscriber (tcp-channel remote-addr local-addr (wild)))
|
|
(topic-publisher (tcp-channel local-addr remote-addr (wild)))))
|
|
|
|
|
|
(define (tcp-listener port)
|
|
(topic-subscriber (tcp-channel (wild)
|
|
(tcp-local-endpoint port)
|
|
(wild))
|
|
#:monitor? #t))
|
|
|
|
(require racket/string)
|
|
(require racket/set)
|
|
(require racket/match)
|
|
(require "os2.rkt")
|
|
(require "os2-tcp.rkt")
|
|
(require "os2-tcp-helper.rkt")
|