First test passed: send one frame.
This commit is contained in:
parent
2de221e166
commit
2349c9e087
3 changed files with 26 additions and 19 deletions
|
@ -10,4 +10,5 @@ license = "MIT OR Apache-2.0"
|
||||||
repository = "https://github.com/fluffysquirrels/framed"
|
repository = "https://github.com/fluffysquirrels/framed"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
cobs = "^0.1.2"
|
||||||
error-chain = "^0.11.0"
|
error-chain = "^0.11.0"
|
14
src/error.rs
14
src/error.rs
|
@ -8,17 +8,9 @@ error_chain! {
|
||||||
}
|
}
|
||||||
|
|
||||||
errors {
|
errors {
|
||||||
UnknownHeader(b: u8) {
|
CobsDecodeFailed {
|
||||||
description("unknown header byte"),
|
description("COBS decode failed"),
|
||||||
display("unknown header byte: {:x}", b),
|
display("COBS decode failed"),
|
||||||
}
|
|
||||||
EofDuringPacket {
|
|
||||||
description("end of file during packet"),
|
|
||||||
display("end of file during packet"),
|
|
||||||
}
|
|
||||||
EofBeforePacket {
|
|
||||||
description("end of file before packet"),
|
|
||||||
display("end of file before packet"),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
30
src/lib.rs
30
src/lib.rs
|
@ -14,46 +14,52 @@
|
||||||
#![deny(warnings)]
|
#![deny(warnings)]
|
||||||
#![feature(conservative_impl_trait)]
|
#![feature(conservative_impl_trait)]
|
||||||
|
|
||||||
|
extern crate cobs;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate error_chain;
|
extern crate error_chain;
|
||||||
|
|
||||||
pub mod channel;
|
pub mod channel;
|
||||||
|
|
||||||
pub mod error;
|
pub mod error;
|
||||||
use error::{Result};
|
use error::{Error, ErrorKind, Result};
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
|
|
||||||
/// Sends frames over an underlying `io::Write` instance.
|
/// Sends frames over an underlying `io::Write` instance.
|
||||||
pub struct Sender<W: Write> {
|
pub struct Sender<W: Write> {
|
||||||
_w: W,
|
w: W,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: Write> Sender<W> {
|
impl<W: Write> Sender<W> {
|
||||||
pub fn new(w: W) -> Sender<W> {
|
pub fn new(w: W) -> Sender<W> {
|
||||||
Sender::<W> {
|
Sender::<W> {
|
||||||
_w: w,
|
w: w,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send(&mut self, _f: &[u8]) -> Result<()> {
|
pub fn send(&mut self, f: &[u8]) -> Result<()> {
|
||||||
unimplemented!();
|
let code = cobs::encode_vec(f);
|
||||||
|
self.w.write(&code)?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Receives frames from an underlying `io::Read` instance.
|
/// Receives frames from an underlying `io::Read` instance.
|
||||||
pub struct Receiver<R: Read> {
|
pub struct Receiver<R: Read> {
|
||||||
_r: R,
|
r: R,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R: Read> Receiver<R> {
|
impl<R: Read> Receiver<R> {
|
||||||
pub fn new(r: R) -> Receiver<R> {
|
pub fn new(r: R) -> Receiver<R> {
|
||||||
Receiver::<R> {
|
Receiver::<R> {
|
||||||
_r: r,
|
r: r,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn recv(&mut self) -> Result<Vec<u8>> {
|
pub fn recv(&mut self) -> Result<Vec<u8>> {
|
||||||
unimplemented!();
|
let mut code = Vec::new();
|
||||||
|
self.r.read_to_end(&mut code)?;
|
||||||
|
cobs::decode_vec(&code)
|
||||||
|
.map_err(|_| Error::from(ErrorKind::CobsDecodeFailed))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,6 +78,14 @@ mod tests {
|
||||||
assert_eq!(recvd, sent);
|
assert_eq!(recvd, sent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Test sending 2 different frames.
|
||||||
|
|
||||||
|
// TODO: Test buffering:
|
||||||
|
// * Write half a frame
|
||||||
|
// * recv() returns nothing
|
||||||
|
// * Write the rest of the frame
|
||||||
|
// * recv() returns the whole frame
|
||||||
|
|
||||||
fn pair() -> (Sender<impl Write>, Receiver<impl Read>) {
|
fn pair() -> (Sender<impl Write>, Receiver<impl Read>) {
|
||||||
let c = Channel::new();
|
let c = Channel::new();
|
||||||
let tx = Sender::new(c.writer());
|
let tx = Sender::new(c.writer());
|
||||||
|
|
Loading…
Reference in a new issue