uni-channel
| (require uni-channel) | package: uni-channel |
The uni-channel module provides one small wrapper around three Racket channel-like transports: async-channel?, place-channel? and port-channel?.
The constructor is deliberately simple:
(make-uni-channel channel)
The wrapper detects the concrete channel kind and exposes the same small set of operations for sending, receiving, event synchronization and closing. The wrapper does not change the transport semantics of the underlying channel. A place-channel? still accepts only place messages, and a port-channel? still transports serialized values as defined by port-channel.
1 Model
A uni-channel has a kind and a direction.
The kind is one of:
'async for an async-channel?.
'place for a place-channel?.
'port for a port-channel?.
The direction is one of:
'bidirectional for async-channel and place-channel wrappers.
'input or 'output for port-channel wrappers, copied from port-channel-direction.
A uni-channel that supports receiving is itself a synchronizable event. Synchronizing the wrapper returns the value received from the underlying channel:
(sync ch)
For output-only channels, synchronizing on the uni-channel uses never-evt. Such a channel has no receive side, so it can never become ready as a receive event.
2 Constructor
procedure
(make-uni-channel channel) → uni-channel?
channel : (or/c async-channel? place-channel? port-channel?)
For async-channel? and place-channel?, the result is 'bidirectional. For port-channel?, the result has the same direction as the port-channel endpoint. To represent a full-duplex port-based connection, create one input port-channel? and one output port-channel?, then wrap each endpoint separately with make-uni-channel.
3 Predicates and accessors
procedure
(uni-channel? v) → boolean?
v : any/c
procedure
(uni-channel-kind ch) → (or/c 'async 'place 'port)
ch : uni-channel?
procedure
→ (or/c 'input 'output 'bidirectional) ch : uni-channel?
4 Sending and receiving
procedure
(uni-channel-put ch v) → void?
ch : uni-channel? v : any/c
procedure
(uni-channel-send ch v) → void?
ch : uni-channel? v : any/c
procedure
(uni-channel-get ch) → any/c
ch : uni-channel?
procedure
(uni-channel-recv ch) → any/c
ch : uni-channel?
procedure
(uni-channel-try-get ch) → any/c
ch : uni-channel?
procedure
(uni-channel-get-evt ch) → evt?
ch : uni-channel?
procedure
(uni-channel-put-evt ch v) → evt?
ch : uni-channel? v : any/c
5 Closing
procedure
(uni-channel-close ch) → void?
ch : uni-channel?
procedure
(uni-channel-closed? ch) → boolean?
ch : uni-channel?
6 Transport errors
procedure
(uni-channel-error-get ch) → any/c
ch : uni-channel?
procedure
(uni-channel-error-try-get ch) → any/c
ch : uni-channel?
procedure
(uni-channel-error-evt ch) → evt?
ch : uni-channel?
7 Examples
7.1 Async channel
(require racket/async-channel uni-channel) (define ch (make-uni-channel (make-async-channel))) (uni-channel-send ch 'hello) (uni-channel-recv ch)
7.2 Place channel pair
(require racket/place uni-channel) (define-values (raw-a raw-b) (place-channel)) (define a (make-uni-channel raw-a)) (define b (make-uni-channel raw-b)) (uni-channel-send a '(hello from a)) (uni-channel-recv b)
7.3 Port-channel endpoints over a pipe
(require port-channel uni-channel) (define-values (in out) (make-pipe)) (define reader (make-uni-channel (make-port-channel in))) (define writer (make-uni-channel (make-port-channel out))) (uni-channel-send writer '(hello 1 2 3)) (uni-channel-recv reader)