parent
ca85eb84bb
commit
ce3ea885d9
5 changed files with 431 additions and 39 deletions
36
framed/Cargo.toml
Normal file
36
framed/Cargo.toml
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
[package]
|
||||
name = "framed"
|
||||
version = "0.1.4"
|
||||
description = "Send and receive data over lossy streams of bytes."
|
||||
authors = ["Alex Helfet <alex.helfet@gmail.com>"]
|
||||
categories = ["no-std", "embedded", "encoding", "network-programming"]
|
||||
keywords = ["no_std", "networking", "datalink", "protocol", "embedded"]
|
||||
readme = "../README.md"
|
||||
license = "MIT OR Apache-2.0"
|
||||
repository = "https://github.com/fluffysquirrels/framed-rs"
|
||||
|
||||
[badges.travis-ci]
|
||||
repository = "fluffysquirrels/framed-rs"
|
||||
branch = "master"
|
||||
|
||||
[dependencies]
|
||||
cobs = "^0.1.3"
|
||||
ref_slice = "^1.1.1"
|
||||
serde = { version = "^1.0", optional = true }
|
||||
ssmarshal = { version = "^1.0", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
serde_derive = "^1.0"
|
||||
|
||||
[features]
|
||||
default = ["use_std", "typed"]
|
||||
|
||||
# Enable to print all data to stdout for testing.
|
||||
trace = []
|
||||
|
||||
# Use standard library. Enabled by default, disable for no_std.
|
||||
use_std = []
|
||||
|
||||
# Enables the `typed` sub-module for sending and receiving
|
||||
# structs serialized with serde.
|
||||
typed = ["serde", "ssmarshal"]
|
||||
105
framed/src/channel.rs
Normal file
105
framed/src/channel.rs
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
//! Vec-backed FIFO buffer of bytes for testing.
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::io::{Read, Result, Write};
|
||||
use std::rc::Rc;
|
||||
|
||||
/// Entry point for the module that can construct multiple `Reader`
|
||||
/// and `Writer` endpoints using the same backing store.
|
||||
pub struct Channel {
|
||||
inner: Rc<RefCell<Inner>>,
|
||||
}
|
||||
|
||||
struct Inner {
|
||||
v: Vec<u8>,
|
||||
read_pos: usize,
|
||||
}
|
||||
|
||||
/// Implements `io::Read`, returning data previously written to a
|
||||
/// `Writer` instance from the same `Channel`.
|
||||
pub struct Reader {
|
||||
inner: Rc<RefCell<Inner>>,
|
||||
}
|
||||
|
||||
/// Implements `io::Write`, writing data to an underlying `Channel`.
|
||||
pub struct Writer {
|
||||
inner: Rc<RefCell<Inner>>,
|
||||
}
|
||||
|
||||
impl Channel {
|
||||
/// Construct a new `Channel`.
|
||||
pub fn new() -> Channel {
|
||||
Channel {
|
||||
inner: Rc::new(RefCell::new(Inner {
|
||||
v: Vec::new(),
|
||||
read_pos: 0
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct a new `Reader` for this `Channel`.
|
||||
pub fn reader(&self) -> Reader {
|
||||
Reader {
|
||||
inner: self.inner.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct a new `Writer` for this `Channel`.
|
||||
pub fn writer(&self) -> Writer {
|
||||
Writer {
|
||||
inner: self.inner.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Read for Reader {
|
||||
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
|
||||
let mut inner = self.inner.borrow_mut();
|
||||
let pos = inner.read_pos;
|
||||
assert!(inner.v.len() >= pos);
|
||||
|
||||
let left = inner.v.len() - pos;
|
||||
let len = buf.len().min(left);
|
||||
|
||||
buf[0..len].copy_from_slice(&inner.v[pos..pos + len]);
|
||||
let new_pos = pos + len;
|
||||
assert!(inner.v.len() >= new_pos);
|
||||
inner.read_pos = new_pos;
|
||||
|
||||
Ok(len)
|
||||
}
|
||||
}
|
||||
|
||||
impl Write for Writer {
|
||||
fn write(&mut self, buf: &[u8]) -> Result<usize> {
|
||||
self.inner.borrow_mut().v.extend_from_slice(buf);
|
||||
Ok(buf.len())
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::io::{Read, Write};
|
||||
use super::Channel;
|
||||
|
||||
#[test]
|
||||
fn ok() {
|
||||
let c = Channel::new();
|
||||
let mut tx = c.writer();
|
||||
let mut rx = c.reader();
|
||||
|
||||
let mut buf = [0u8; 5];
|
||||
|
||||
assert_eq!(rx.read(&mut buf).unwrap(), 0);
|
||||
|
||||
assert_eq!(tx.write(&[1, 2, 3]).unwrap(), 3);
|
||||
assert_eq!(rx.read(&mut buf[0..1]).unwrap(), 1);
|
||||
assert_eq!(buf, [1, 0, 0, 0, 0]);
|
||||
assert_eq!(rx.read(&mut buf[1..5]).unwrap(), 2);
|
||||
assert_eq!(buf, [1, 2, 3, 0, 0]);
|
||||
}
|
||||
}
|
||||
50
framed/src/error.rs
Normal file
50
framed/src/error.rs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
//! Representations of errors returned by this crate.
|
||||
|
||||
#[cfg(feature = "typed")]
|
||||
use ssmarshal;
|
||||
|
||||
#[cfg(feature = "use_std")]
|
||||
use std::io;
|
||||
|
||||
#[cfg(feature = "use_std")]
|
||||
use std::result;
|
||||
#[cfg(not(feature = "use_std"))]
|
||||
use core::result;
|
||||
|
||||
/// Type alias for results from this crate.
|
||||
pub type Result<T> = result::Result<T, Error>;
|
||||
|
||||
/// Errors from this crate.
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
/// COBS decode failed
|
||||
CobsDecodeFailed,
|
||||
|
||||
/// End of data while reading a frame
|
||||
EofDuringFrame,
|
||||
|
||||
/// The supplied value was too short to be an encoded frame
|
||||
EncodedFrameTooShort,
|
||||
|
||||
/// Forwarded io::Error.
|
||||
#[cfg(feature = "use_std")]
|
||||
Io(io::Error),
|
||||
|
||||
/// Forwarded ssmarshal::Error.
|
||||
#[cfg(feature = "typed")]
|
||||
Ssmarshal(ssmarshal::Error),
|
||||
}
|
||||
|
||||
#[cfg(feature = "use_std")]
|
||||
impl From<io::Error> for Error {
|
||||
fn from(e: io::Error) -> Error {
|
||||
Error::Io(e)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "typed")]
|
||||
impl From<ssmarshal::Error> for Error {
|
||||
fn from(e: ssmarshal::Error) -> Error {
|
||||
Error::Ssmarshal(e)
|
||||
}
|
||||
}
|
||||
525
framed/src/lib.rs
Normal file
525
framed/src/lib.rs
Normal file
|
|
@ -0,0 +1,525 @@
|
|||
//! Send and receive data over lossy streams of bytes.
|
||||
//!
|
||||
//! Living in / inspired by the [data link layer][dll] or layer 2
|
||||
//! in the OSI networking model, this module enables sending slices of
|
||||
//! bytes of definite length over an underlying lossy transport that
|
||||
//! only supports sending an unstructured stream of bytes (a [physical
|
||||
//! layer][pl], such as ITM, UART or SPI).
|
||||
//!
|
||||
//! [dll]: https://en.wikipedia.org/wiki/Data_link_layer
|
||||
//! [pl]: https://en.wikipedia.org/wiki/Physical_layer
|
||||
//!
|
||||
//! The transport may corrupt the stream by dropping or modifying some
|
||||
//! bytes en route. When the transport returns corrupt data the
|
||||
//! decoder may return errors or corrupted payloads, but if the
|
||||
//! transport starts operating without losses again the decoder should
|
||||
//! return new uncorrupted frames.
|
||||
//!
|
||||
//! ## Encoding
|
||||
//!
|
||||
//! Currently the encoding is:
|
||||
//!
|
||||
//! * The "body": payload [COBS]-encoded to remove bytes equal to zero
|
||||
//! * A terminating zero byte.
|
||||
//! [COBS]: https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing
|
||||
//!
|
||||
//! The encoding is not stable at the moment, i.e. it can and will
|
||||
//! change between minor versions. Consequently encoded data from this
|
||||
//! crate is unsuitable for long-term storage or transmission between
|
||||
//! different versions of an application. The API should be kept
|
||||
//! stable between versions where possible and the crate version will
|
||||
//! follow Rust semver rules on API changes.
|
||||
//!
|
||||
//! ## Cargo feature flags
|
||||
//!
|
||||
//! `use_std`: Use standard library. Enabled by default, disable for no_std.
|
||||
//!
|
||||
//! `trace`: Enable to print all data to stdout for testing.
|
||||
//!
|
||||
//! `typed`: Enables the [`typed`](typed/index.html) sub-module for sending and
|
||||
//! receiving structs serialized with serde. Enabled by default.
|
||||
//!
|
||||
//! ## API
|
||||
//!
|
||||
//! Payload data and encoded frame data have separate types to avoid
|
||||
//! mixing them up. You are encouraged to use these types in your own
|
||||
//! code when integrating with this crate. Definitions:
|
||||
//!
|
||||
//! ```rust,ignore
|
||||
//! /// Arbitrary user data.
|
||||
//! pub type Payload = [u8];
|
||||
//!
|
||||
//! /// Data that is encoded as a frame. It is ready to send, or may have
|
||||
//! /// just been received.
|
||||
//! pub type Encoded = [u8];
|
||||
//!
|
||||
//! /// Heap-allocated user data used as a return type.
|
||||
//! #[cfg(feature = "use_std")]
|
||||
//! pub struct BoxPayload(_);
|
||||
//!
|
||||
//! /// Heap-allocated frame data used as a return type.
|
||||
//! #[cfg(feature = "use_std")]
|
||||
//! pub struct BoxEncoded(_);
|
||||
//! ```
|
||||
//!
|
||||
//! Consumers have a choice of interfaces to enable usability,
|
||||
//! efficiency, and use from `no_std` crates.
|
||||
//!
|
||||
//! See the `decode_*` and `encode_*` functions for simple uses with
|
||||
//! various input and output types.
|
||||
//!
|
||||
//! Note that data typed as `Payload` or `Encoded` may be
|
||||
//! efficiently passed as a function argument by reference, but is
|
||||
//! returned using an opaque struct (`BoxPayload`, `BoxEncoded`)
|
||||
//! containing a heap-allocated value instead. Consequently `encode_*`
|
||||
//! and `decode_*` variants that require this are only available with
|
||||
//! the `use_std` Cargo feature.
|
||||
//!
|
||||
//! For sending or receiving a stream of frames, consider the `Reader`
|
||||
//! and `Writer` structs that wrap an `io::Read` or `io::Write`
|
||||
//! instance.
|
||||
|
||||
#![deny(warnings)]
|
||||
#![cfg_attr(not(feature = "use_std"), no_std)]
|
||||
|
||||
// ## extern crate statements
|
||||
extern crate cobs;
|
||||
extern crate ref_slice;
|
||||
|
||||
#[cfg(feature = "typed")]
|
||||
extern crate serde;
|
||||
|
||||
#[macro_use]
|
||||
#[cfg(all(test, feature = "typed", feature = "use_std"))]
|
||||
extern crate serde_derive;
|
||||
|
||||
#[cfg(feature = "typed")]
|
||||
extern crate ssmarshal;
|
||||
|
||||
|
||||
// ## Sub-modules
|
||||
#[cfg(feature = "use_std")]
|
||||
pub mod channel;
|
||||
|
||||
pub mod error;
|
||||
pub use error::{Error, Result};
|
||||
|
||||
#[cfg(all(feature = "typed", feature = "use_std"))]
|
||||
pub mod typed;
|
||||
|
||||
// ## use statements
|
||||
#[cfg(feature = "use_std")]
|
||||
use ref_slice::ref_slice_mut;
|
||||
|
||||
#[cfg(feature = "use_std")]
|
||||
use std::io::{self, Read, Write};
|
||||
|
||||
#[cfg(feature = "use_std")]
|
||||
use std::ops::Deref;
|
||||
|
||||
|
||||
/// Arbitrary user data.
|
||||
pub type Payload = [u8];
|
||||
|
||||
/// Data that is encoded as a frame. It is ready to send, or may have
|
||||
/// just been received.
|
||||
pub type Encoded = [u8];
|
||||
|
||||
// Note: BoxPayload and BoxEncoded store data in a Vec<u8> as that's
|
||||
// what `cobs` returns us and converting them into a Box<[u8]> (with
|
||||
// Vec::into_boxed_slice(self)) would require re-allocation.
|
||||
//
|
||||
// See https://doc.rust-lang.org/std/vec/struct.Vec.html#method.into_boxed_slice
|
||||
|
||||
/// Heap-allocated user data used as a return type.
|
||||
#[cfg(feature = "use_std")]
|
||||
#[derive(Debug)]
|
||||
pub struct BoxPayload(Vec<u8>);
|
||||
|
||||
#[cfg(feature = "use_std")]
|
||||
impl From<Vec<u8>> for BoxPayload {
|
||||
fn from(v: Vec<u8>) -> BoxPayload {
|
||||
BoxPayload(v)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "use_std")]
|
||||
impl Deref for BoxPayload {
|
||||
type Target = [u8];
|
||||
|
||||
fn deref(&self) -> &[u8] {
|
||||
&*self.0
|
||||
}
|
||||
}
|
||||
|
||||
/// Heap-allocated frame data used as a return type.
|
||||
#[cfg(feature = "use_std")]
|
||||
#[derive(Debug)]
|
||||
pub struct BoxEncoded(Vec<u8>);
|
||||
|
||||
#[cfg(feature = "use_std")]
|
||||
impl From<Vec<u8>> for BoxEncoded {
|
||||
fn from(v: Vec<u8>) -> BoxEncoded {
|
||||
BoxEncoded(v)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "use_std")]
|
||||
impl Deref for BoxEncoded {
|
||||
type Target = [u8];
|
||||
|
||||
fn deref(&self) -> &[u8] {
|
||||
&*self.0
|
||||
}
|
||||
}
|
||||
|
||||
pub const FRAME_END_SYMBOL: u8 = 0;
|
||||
|
||||
const HEADER_LEN: usize = 0;
|
||||
|
||||
const FOOTER_LEN: usize = 1;
|
||||
|
||||
/// Encode the supplied payload data as a frame at the beginning of
|
||||
/// the supplied buffer `dest`.
|
||||
///
|
||||
/// Returns the number of bytes it has written to the buffer.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function will panic if `dest` is not large enough for the encoded frame.
|
||||
/// Ensure `dest.len() >= max_encoded_len(p.len())`.
|
||||
pub fn encode_to_slice(p: &Payload, dest: &mut [u8]) -> Result<usize> {
|
||||
// Panic if code won't fit in `dest` because this is a programmer error.
|
||||
assert!(max_encoded_len(p.len())? <= dest.len());
|
||||
|
||||
let cobs_len = cobs::encode(&p, &mut dest[HEADER_LEN..]);
|
||||
let footer_idx = HEADER_LEN + cobs_len;
|
||||
dest[footer_idx] = FRAME_END_SYMBOL;
|
||||
|
||||
#[cfg(feature = "trace")] {
|
||||
println!("framed: Frame code = {:?}", &dest[0..(footer_idx + 1)]);
|
||||
}
|
||||
Ok(cobs_len + HEADER_LEN + FOOTER_LEN)
|
||||
}
|
||||
|
||||
/// Encode the supplied payload data as a frame and return it on the heap.
|
||||
#[cfg(feature = "use_std")]
|
||||
pub fn encode_to_box(p: &Payload) -> Result<BoxEncoded> {
|
||||
let mut buf = vec![0; max_encoded_len(p.len())?];
|
||||
let len = encode_to_slice(p, &mut *buf)?;
|
||||
buf.truncate(len);
|
||||
Ok(BoxEncoded::from(buf))
|
||||
}
|
||||
|
||||
/// Encode the supplied payload data as a frame and write it to the
|
||||
/// supplied `Write`.
|
||||
///
|
||||
/// This function will not call `flush` on the writer; the caller do
|
||||
/// so if this is required.
|
||||
///
|
||||
/// Returns the length of the frame it has written.
|
||||
#[cfg(feature = "use_std")]
|
||||
pub fn encode_to_writer<W: Write>(p: &Payload, w: &mut W) -> Result<usize> {
|
||||
let b = encode_to_box(p)?;
|
||||
w.write_all(&*b.0)?;
|
||||
Ok(b.len())
|
||||
}
|
||||
|
||||
/// Decode the supplied encoded frame, placing the payload at the
|
||||
/// beginning of the supplied buffer `dest`.
|
||||
///
|
||||
/// When reading from a stream, the caller can continue reading data
|
||||
/// and buffering it until a `FRAME_END_SYMBOL` is read, then pass the
|
||||
/// whole buffer including `FRAME_END_SYMBOL` to this function for
|
||||
/// decoding.
|
||||
///
|
||||
/// If there is more than 1 FRAME_END_SYMBOL within `e`, the result
|
||||
/// is undefined. Make sure you only pass 1 frame at a time.
|
||||
///
|
||||
/// Returns the length of the payload it has decoded.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns `Err(Error::EofDuringFrame` if `e` is not a complete
|
||||
/// encoded frame, which should have `FRAME_END_SYMBOL` as the last
|
||||
/// byte.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function will panic if `dest` is not large enough for the decoded frame.
|
||||
/// Ensure `dest.len() >= max_decoded_len(e.len())?`.
|
||||
pub fn decode_to_slice(e: &Encoded, mut dest: &mut [u8])
|
||||
-> Result<usize> {
|
||||
assert!(dest.len() >= max_decoded_len(e.len())?);
|
||||
|
||||
#[cfg(feature = "trace")] {
|
||||
println!("framed: Encoded input = {:?}", e);
|
||||
}
|
||||
|
||||
if e[e.len()-1] != FRAME_END_SYMBOL {
|
||||
return Err(Error::EofDuringFrame)
|
||||
}
|
||||
|
||||
assert_eq!(e[e.len() - 1], FRAME_END_SYMBOL);
|
||||
// Just the body (COBS-encoded payload).
|
||||
let body = &e[0..(e.len()-1)];
|
||||
|
||||
let len = cobs::decode(body, &mut dest)
|
||||
.map_err(|_| Error::CobsDecodeFailed)?;
|
||||
|
||||
#[cfg(feature = "trace")] {
|
||||
println!("framed: dest = {:?}\n\
|
||||
framed: body = {:?}\n\
|
||||
framed: decoded = {:?}",
|
||||
&dest, body, &dest[0..len]);
|
||||
}
|
||||
|
||||
Ok(len)
|
||||
}
|
||||
|
||||
/// Decode the supplied encoded frame, returning the payload on the heap.
|
||||
#[cfg(feature = "use_std")]
|
||||
pub fn decode_to_box(e: &Encoded) -> Result<BoxPayload> {
|
||||
let mut buf = vec![0; max_decoded_len(e.len())?];
|
||||
let len = decode_to_slice(e, &mut buf)?;
|
||||
buf.truncate(len);
|
||||
Ok(BoxPayload::from(buf))
|
||||
}
|
||||
|
||||
/// Reads bytes from the supplied `Read` until it has a complete
|
||||
/// encoded frame, then decodes the frame, returning the payload on the heap.
|
||||
#[cfg(feature = "use_std")]
|
||||
pub fn decode_from_reader<R: Read>(r: &mut Read) -> Result<BoxPayload> {
|
||||
// Read until FRAME_END_SYMBOL
|
||||
let mut next_frame = Vec::new();
|
||||
let mut b = 0u8;
|
||||
loop {
|
||||
let res = r.read(ref_slice_mut(&mut b));
|
||||
#[cfg(feature = "trace")] {
|
||||
println!("framed: Read result = {:?}", res);
|
||||
}
|
||||
match res {
|
||||
Err(ref e) if e.kind() == io::ErrorKind::UnexpectedEof =>
|
||||
return Err(Error::EofDuringFrame),
|
||||
Ok(0) =>
|
||||
return Err(Error::EofDuringFrame),
|
||||
Err(e) => return Err(Error::from(e)),
|
||||
Ok(_) => (),
|
||||
};
|
||||
|
||||
#[cfg(feature = "trace")] {
|
||||
println!("framed: Read byte = {}", b);
|
||||
}
|
||||
next_frame.push(b);
|
||||
if b == FRAME_END_SYMBOL {
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert_eq!(next_frame[next_frame.len()-1], FRAME_END_SYMBOL);
|
||||
|
||||
decode_to_box(&*next_frame)
|
||||
}
|
||||
|
||||
/// Returns the maximum possible decoded length given a frame with
|
||||
/// the encoded length supplied.
|
||||
pub fn max_decoded_len(code_len: usize) -> Result<usize> {
|
||||
let framing_len = HEADER_LEN + FOOTER_LEN;
|
||||
if code_len < framing_len {
|
||||
return Err(Error::EncodedFrameTooShort)
|
||||
}
|
||||
let cobs_len = code_len - framing_len;
|
||||
// If every byte is a 0x00, then COBS-encoded data will be the
|
||||
// same length of 0x01.
|
||||
let cobs_decode_limit = cobs_len;
|
||||
Ok(cobs_decode_limit)
|
||||
}
|
||||
|
||||
/// Returns the maximum possible encoded length for a frame with
|
||||
/// the payload length supplied.
|
||||
pub fn max_encoded_len(payload_len: usize) -> Result<usize> {
|
||||
Ok(HEADER_LEN
|
||||
+ cobs::max_encoding_length(payload_len)
|
||||
+ FOOTER_LEN)
|
||||
}
|
||||
|
||||
|
||||
/// Sends encoded frames over an inner `io::Write` instance.
|
||||
#[cfg(feature = "use_std")]
|
||||
pub struct Sender<W: Write> {
|
||||
w: W,
|
||||
}
|
||||
|
||||
#[cfg(feature = "use_std")]
|
||||
impl<W: Write> Sender<W> {
|
||||
/// Construct a `Sender` that sends frames over the supplied
|
||||
/// `io::Write`.
|
||||
pub fn new(w: W) -> Sender<W> {
|
||||
Sender::<W> {
|
||||
w: w,
|
||||
}
|
||||
}
|
||||
|
||||
/// Consume this `Sender` and return the inner `io::Write`.
|
||||
pub fn into_inner(self) -> W {
|
||||
self.w
|
||||
}
|
||||
|
||||
/// Flush all buffered data. Includes calling `flush` on the inner
|
||||
/// writer.
|
||||
pub fn flush(&mut self) -> Result<()> {
|
||||
Ok(self.w.flush()?)
|
||||
}
|
||||
|
||||
/// Queue the supplied payload for transmission.
|
||||
///
|
||||
/// This `Sender` may buffer the data indefinitely, as may the
|
||||
/// inner writer. To ensure all buffered data has been
|
||||
/// transmitted call [`flush`](#method.flush).
|
||||
///
|
||||
/// See also: [`send`](#method.send)
|
||||
pub fn queue(&mut self, p: &Payload) -> Result<usize> {
|
||||
encode_to_writer(p, &mut self.w)
|
||||
}
|
||||
|
||||
/// Encode the supplied payload as a frame, write it to the
|
||||
/// inner writer, then flush.
|
||||
///
|
||||
/// Ensures the data has been transmitted before returning to the
|
||||
/// caller.
|
||||
///
|
||||
/// See also: [`queue`](#method.queue)
|
||||
pub fn send(&mut self, p: &Payload) -> Result<usize> {
|
||||
let len = self.queue(p)?;
|
||||
self.flush()?;
|
||||
Ok(len)
|
||||
}
|
||||
}
|
||||
|
||||
/// Receives encoded frames from an inner `io::Read` instance.
|
||||
#[cfg(feature = "use_std")]
|
||||
pub struct Receiver<R: Read> {
|
||||
r: R,
|
||||
}
|
||||
|
||||
#[cfg(feature = "use_std")]
|
||||
impl<R: Read> Receiver<R> {
|
||||
/// Construct a `Receiver` that receives frames from the supplied
|
||||
/// `io::Read`.
|
||||
pub fn new(r: R) -> Receiver<R> {
|
||||
Receiver::<R> {
|
||||
r: r,
|
||||
}
|
||||
}
|
||||
|
||||
/// Consume this `Receiver` and return the inner `io::Read`.
|
||||
pub fn into_inner(self) -> R {
|
||||
self.r
|
||||
}
|
||||
|
||||
/// Receive an encoded frame from the inner `io::Read`, decode it
|
||||
/// and return the payload.
|
||||
pub fn recv(&mut self) -> Result<BoxPayload> {
|
||||
decode_from_reader::<R>(&mut self.r)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn max_encoded_len_ok() {
|
||||
assert_eq!(max_encoded_len(0) .unwrap(), 1);
|
||||
assert_eq!(max_encoded_len(1) .unwrap(), 3);
|
||||
assert_eq!(max_encoded_len(2) .unwrap(), 4);
|
||||
assert_eq!(max_encoded_len(254).unwrap(), 256);
|
||||
assert_eq!(max_encoded_len(255).unwrap(), 258);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn max_decoded_len_too_short() {
|
||||
match max_decoded_len(0) {
|
||||
Err(Error::EncodedFrameTooShort) => (),
|
||||
e @ _ => panic!("Bad output: {:?}", e)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn max_decoded_len_ok() {
|
||||
assert_eq!(max_decoded_len(1) .unwrap(), 0);
|
||||
assert_eq!(max_decoded_len(2) .unwrap(), 1);
|
||||
assert_eq!(max_decoded_len(3) .unwrap(), 2);
|
||||
assert_eq!(max_decoded_len(255).unwrap(), 254);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Add tests for all encode_*, decode_* functions.
|
||||
|
||||
#[cfg(all(test, feature = "use_std"))]
|
||||
mod rw_tests {
|
||||
use channel::Channel;
|
||||
use error::Error;
|
||||
use std::io::{Read, Write};
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn one_frame() {
|
||||
let (mut tx, mut rx) = pair();
|
||||
let p = [0x00, 0x01, 0x02];
|
||||
assert_eq!(tx.send(&p).unwrap(), 5);
|
||||
let recvd = rx.recv().unwrap();
|
||||
assert_eq!(*recvd, p);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn two_frames_sequentially() {
|
||||
let (mut tx, mut rx) = pair();
|
||||
{
|
||||
let sent = [0x00, 0x01, 0x02];
|
||||
assert_eq!(tx.send(&sent).unwrap(), 5);
|
||||
let recvd = rx.recv().unwrap();
|
||||
assert_eq!(*recvd, sent);
|
||||
}
|
||||
|
||||
{
|
||||
let sent = [0x10, 0x11, 0x12];
|
||||
assert_eq!(tx.send(&sent).unwrap(), 5);
|
||||
let recvd = rx.recv().unwrap();
|
||||
assert_eq!(*recvd, sent);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn two_frames_at_once() {
|
||||
let (mut tx, mut rx) = pair();
|
||||
let s1 = [0x00, 0x01, 0x02];
|
||||
let s2 = [0x10, 0x11, 0x12];
|
||||
|
||||
tx.send(&s1).unwrap();
|
||||
tx.send(&s2).unwrap();
|
||||
|
||||
let r1 = rx.recv().unwrap();
|
||||
let r2 = rx.recv().unwrap();
|
||||
println!("r1: {:?}\n\
|
||||
r2: {:?}", r1, r2);
|
||||
|
||||
assert_eq!(*r1, s1);
|
||||
assert_eq!(*r2, s2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_input() {
|
||||
let (mut _tx, mut rx) = pair();
|
||||
match rx.recv() {
|
||||
Err(Error::EofDuringFrame) => (),
|
||||
e @ _ => panic!("Bad value: {:?}", e)
|
||||
}
|
||||
}
|
||||
|
||||
fn pair() -> (Sender<Box<Write>>, Receiver<Box<Read>>) {
|
||||
let c = Channel::new();
|
||||
let tx = Sender::new(Box::new(c.writer()) as Box<Write>);
|
||||
let rx = Receiver::new(Box::new(c.reader()) as Box<Read>);
|
||||
(tx, rx)
|
||||
}
|
||||
}
|
||||
205
framed/src/typed.rs
Normal file
205
framed/src/typed.rs
Normal file
|
|
@ -0,0 +1,205 @@
|
|||
//! Sending and receiving structs serialized with serde and
|
||||
//! [`ssmarshal`][ssmarshal].
|
||||
//!
|
||||
//! [ssmarshal]: https://crates.io/crates/ssmarshal
|
||||
//!
|
||||
//! `ssmarshal` uses a straightforward, compact serialization format
|
||||
//! that doesn't support compatibility between versions or dynamic
|
||||
//! length types (arrays, maps). Its lack of stability fits with the
|
||||
//! frame encoding in this crate: unsuitable for long-term storage or
|
||||
//! transmission between different versions of an application.
|
||||
//!
|
||||
//! This module currently requires `std`, the standard library.
|
||||
|
||||
use error::{Result};
|
||||
use serde::Serialize;
|
||||
use serde::de::DeserializeOwned;
|
||||
use ssmarshal;
|
||||
use std::io::{Read, Write};
|
||||
use std::marker::PhantomData;
|
||||
use std::mem::size_of;
|
||||
|
||||
/// Sends encoded structs of type `T` over an inner `io::Write` instance.
|
||||
pub struct Sender<W: Write, T: Serialize> {
|
||||
w: W,
|
||||
_t: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<W: Write, T: Serialize> Sender<W, T> {
|
||||
/// Construct a `Sender` that sends encoded structs over the supplied
|
||||
/// `io::Write`.
|
||||
pub fn new(w: W) -> Sender<W, T> {
|
||||
Sender::<W, T> {
|
||||
w: w,
|
||||
_t: PhantomData::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Consume this `Sender` and return the inner `io::Write`.
|
||||
pub fn into_inner(self) -> W {
|
||||
self.w
|
||||
}
|
||||
|
||||
/// Flush all buffered data. Includes calling `flush` on the inner
|
||||
/// writer.
|
||||
pub fn flush(&mut self) -> Result<()> {
|
||||
Ok(self.w.flush()?)
|
||||
}
|
||||
|
||||
/// Queue the supplied payload for transmission.
|
||||
///
|
||||
/// This `Sender` may buffer the data indefinitely, as may the
|
||||
/// inner writer. To ensure all buffered data has been
|
||||
/// transmitted call [`flush`](#method.flush).
|
||||
///
|
||||
/// See also: [`send`](#method.send)
|
||||
pub fn queue(&mut self, v: &T) -> Result<usize> {
|
||||
// This uses a dynamically allocated buffer.
|
||||
//
|
||||
// I couldn't get a no_std version to compile with a stack-allocated
|
||||
// buffer as [0u8; size_of::<T>()] due to compiler errors like:
|
||||
// ```
|
||||
// error[E0401]: can't use type parameters from outer function; try using a local type parameter instead
|
||||
// --> src/typed.rs:46:39
|
||||
// |
|
||||
// 46 | const _sbl: usize = size_of::<T>();
|
||||
// | ^ use of type variable from outer function
|
||||
// ```
|
||||
// I think this may require const generics
|
||||
// (rust-lang tracking issue:
|
||||
// https://github.com/rust-lang/rust/issues/44580).
|
||||
//
|
||||
// When I need to write a no_std version I see a few easy options:
|
||||
// 1. Caller supplies a reference to a buffer, we can assert! that
|
||||
// it's long enough. Annoying to use.
|
||||
// 2. Choose a reasonable length buffer and assert it's long enough.
|
||||
// Won't work for large enough structs, may consume an inappropriate
|
||||
// amount of memory for embedded use.
|
||||
// 3. Provide overloads for 1 and 2. For most cases the fixed size
|
||||
// buffer will be fine, and if not you can provide your own.
|
||||
let mut ser_buf = vec![0u8; size_of::<T>()];
|
||||
|
||||
let ser_len = ssmarshal::serialize(&mut ser_buf, v)?;
|
||||
let ser = &ser_buf[0..ser_len];
|
||||
#[cfg(feature = "trace")] {
|
||||
println!("framed: Serialized = {:?}", ser);
|
||||
}
|
||||
super::encode_to_writer(&ser, &mut self.w)
|
||||
}
|
||||
|
||||
/// Encode the supplied payload as a frame, write it to the
|
||||
/// inner writer, then flush.
|
||||
///
|
||||
/// Ensures the data has been transmitted before returning to the
|
||||
/// caller.
|
||||
///
|
||||
/// See also: [`queue`](#method.queue)
|
||||
pub fn send(&mut self, v: &T) -> Result<usize> {
|
||||
let len = self.queue(v)?;
|
||||
self.flush()?;
|
||||
Ok(len)
|
||||
}
|
||||
}
|
||||
|
||||
/// Receives encoded structs of type `T` from an inner `io::Read` instance.
|
||||
pub struct Receiver<R: Read, T: DeserializeOwned> {
|
||||
r: R,
|
||||
_t: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<R: Read, T: DeserializeOwned> Receiver<R, T> {
|
||||
/// Construct a `Receiver` that receives encoded structs from the supplied
|
||||
/// `io::Read`.
|
||||
pub fn new(r: R) -> Receiver<R, T> {
|
||||
Receiver::<R, T> {
|
||||
r: r,
|
||||
_t: PhantomData::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Consume this `Receiver` and return the inner `io::Read`.
|
||||
pub fn into_inner(self) -> R {
|
||||
self.r
|
||||
}
|
||||
|
||||
/// Receive an encoded frame from the inner `io::Read`, decode it
|
||||
/// and return the payload.
|
||||
pub fn recv(&mut self) -> Result<T> {
|
||||
let payload = super::decode_from_reader::<R>(&mut self.r)?;
|
||||
let (v, _len) = ssmarshal::deserialize(&*payload)?;
|
||||
Ok(v)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use channel::Channel;
|
||||
use error::Error;
|
||||
use std::io::{Read, Write};
|
||||
use super::*;
|
||||
|
||||
#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)]
|
||||
struct Test {
|
||||
i8: i8,
|
||||
i16: i16,
|
||||
i32: i32,
|
||||
i64: i64,
|
||||
|
||||
u8: u8,
|
||||
u16: u16,
|
||||
u32: u32,
|
||||
u64: u64,
|
||||
|
||||
some: Option<u8>,
|
||||
none: Option<u8>,
|
||||
|
||||
a: [u8; 3],
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one() {
|
||||
let (mut tx, mut rx) = pair();
|
||||
let v = val();
|
||||
tx.send(&v).unwrap();
|
||||
let r = rx.recv().unwrap();
|
||||
println!("r: {:#?}", r);
|
||||
assert_eq!(v, r);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_input() {
|
||||
let (mut _tx, mut rx) = pair();
|
||||
match rx.recv() {
|
||||
Err(Error::EofDuringFrame) => (),
|
||||
e @ _ => panic!("Bad value: {:?}", e)
|
||||
}
|
||||
}
|
||||
|
||||
fn val() -> Test {
|
||||
let v = Test {
|
||||
i8: 1,
|
||||
i16: 2,
|
||||
i32: 3,
|
||||
i64: 4,
|
||||
|
||||
u8: 10,
|
||||
u16: 11,
|
||||
u32: 12,
|
||||
u64: 13,
|
||||
|
||||
some: Some(17),
|
||||
none: None,
|
||||
|
||||
a: [1, 2, 3],
|
||||
};
|
||||
println!("Test value: {:#?}", v);
|
||||
v
|
||||
}
|
||||
|
||||
fn pair() -> (Sender<Box<Write>, Test>, Receiver<Box<Read>, Test>) {
|
||||
let c = Channel::new();
|
||||
let tx = Sender::new(Box::new(c.writer()) as Box<Write>);
|
||||
let rx = Receiver::new(Box::new(c.reader()) as Box<Read>);
|
||||
(tx, rx)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue