Generic communication channels for things that can be represented as, or transformed to and from, byte vectors.
The FlatPort
and FlatChan
types implement the generic channel and port interface for arbitrary types and transport strategies. It can particularly be used to send and recieve serializable types over I/O streams.
FlatPort
and FlatChan
implement the same comm traits as pipe-based ports and channels.
This example sends boxed integers across tasks using serialization.
let (port, chan) = serial::pipe_stream();
do task::spawn |move chan| {
for int::range(0, 10) |i| {
chan.send(@i)
}
}
for int::range(0, 10) |i| {
assert @i == port.recv()
}
Flat pipes created from io::Reader
s and io::Writer
s share the same blocking properties as the underlying stream. Since some implementations block the scheduler thread, so will their pipes.
CONTINUE
FlatChan
- A FlatChan, consisting of a Flattener
that converts values to byte vectors, and a ByteChan
that transmits the bytes.FlatPort
ByteChan
- ByteChans are a simple interface for sending bytesBytePort
- BytePorts are a simple interface for receiving a specified numberFlattener
- Flatteners present a value as a byte vectorUnflattener
- Unflatteners convert a byte vector to a valueof GenericPort<T> for FlatPort<T, U, P>
of GenericChan<T> for FlatChan<T, F, C>
for FlatPort<T, U, P>
for FlatChan<T, F, C>
flatpipes::bytepipes
flatpipes::flatteners
flatpipes::pod
- Constructors for flat pipes that send POD types using memcpy.flatpipes::serial
- Constructors for flat pipes that using serialization-based flattening.flatpipes::util
CONTINUE
[u8 * 4]
FlatChan
pub struct FlatChan <T, F: Flattener<T>, C: ByteChan>{
flattener: F,
byte_chan: C,
}
A FlatChan, consisting of a Flattener
that converts values to byte vectors, and a ByteChan
that transmits the bytes.
Create using the constructors in the serial
and pod
modules.
FlatPort
pub struct FlatPort <T, U: Unflattener<T>, P: BytePort>{
unflattener: U,
byte_port: P,
}
A FlatPort, consisting of a BytePort
that recieves byte vectors, and an Unflattener
that converts the bytes to a value.
Create using the constructors in the serial
and pod
modules.
ByteChan
ByteChans are a simple interface for sending bytes
send
fn send(val: ~[u8])
BytePort
BytePorts are a simple interface for receiving a specified number
try_recv
fn try_recv(count: uint) -> Option<~[u8]>
Flattener
Flatteners present a value as a byte vector
flatten
fn flatten(val: T) -> ~[u8]
Unflattener
Unflatteners convert a byte vector to a value
unflatten
fn unflatten(buf: ~[u8]) -> T
GenericPort<T>
for FlatPort<T, U, P>
recv
fn recv() -> T
try_recv
fn try_recv() -> Option<T>
GenericChan<T>
for FlatChan<T, F, C>
send
fn send(val: T)
FlatPort<T, U, P>
new
fn new(u: U, p: P) -> FlatPort<T, U, P>
FlatChan<T, F, C>
new
fn new(f: F, c: C) -> FlatChan<T, F, C>