racket-matrix-2012/fake-tcp.rkt

37 lines
1.5 KiB
Racket

#lang racket/base
(require racket/match)
(require "os2.rkt")
(require (prefix-in base: "os2-tcp.rkt"))
(provide (rename-out [base:tcp-driver tcp-driver])
(rename-out [base:tcp-spy tcp-spy])
tcp-listener
topic->tcp-connection
(rename-out [base:tcp-channel tcp-channel])
(rename-out [base:tcp-credit tcp-credit])
(rename-out [base:tcp-mode tcp-mode]))
;; Returns a topic that can be subscribed to in order to be told about
;; conversations coming and going relating to TCP connections arriving
;; at a local server socket. The server socket is created implicitly
;; as the result of subscribing to this topic.
(define (tcp-listener port)
(topic-subscriber (base:tcp-channel (wild) (base:tcp-listener port) (wild))
#:monitor? #t))
;; Given the topic of a newly-arriving connection, creates helper
;; functions that create communication requests relating to that
;; connection, and also creates topics useful for hearing
;; communications relating to that connection.
(define (topic->tcp-connection new-topic)
(match-define (topic _ (base:tcp-channel remote-addr local-addr _) _) new-topic)
(define (cin feedback) (send-feedback (base:tcp-channel remote-addr local-addr feedback)))
(define (cout data) (send-message (base:tcp-channel local-addr remote-addr data)))
(define in-topic (topic-subscriber (base:tcp-channel remote-addr local-addr (wild))))
(define out-topic (topic-publisher (base:tcp-channel local-addr remote-addr (wild))))
(values cin cout in-topic out-topic))