Crates.io | simple-stream |
lib.rs | simple-stream |
version | 0.11.0 |
source | src |
created_at | 2015-08-05 14:55:10.197996 |
updated_at | 2023-06-23 14:58:25.175812 |
description | Building blocks for streaming chunks of data over io::Read and io::Write impls. |
homepage | |
repository | https://github.com/nathansizemore/simple-stream |
max_upload_size | |
id | 2762 |
size | 57,741 |
simple-stream is a buffered stream wrapper over anything that implements
std::io::Read
and std::io::Write
. It works by buffering all reads and
checking the buffers against a FrameBuilder
, which will inform the stream
that a complete Frame
has been received, and removes it out of the buffer.
The crate comes with a few types of Framing options, and provides both a plain text and encrypted stream via rust-openssl.
extern crate simple_stream as ss;
use std::net::TcpStream;
use ss::frame::{SimpleFrame, SimpleFrameBuilder};
use ss::{Plain, NonBlocking};
fn main() {
// Create some non-blocking type that implements Read + Write
let stream = TcpStream::connect("rust-lang.org:80").unwrap();
stream.set_nonblocking(true).unwrap();
// Create a Plain Text stream that sends and receives messages in the
// `SimpleFrame` format.
let mut plain_stream = Plain::<TcpStream, SimpleFrameBuilder>::new(stream);
// Perform a non-blocking write
let buf = vec!(1, 2, 3, 4);
let frame = SimpleFrame::new(&buf[..]);
match plain_stream.nb_send(&frame) {
Ok(_) => { }
Err(e) => println!("Error during write: {}", e)
};
// Perform a non-blocking read
match plain_stream.nb_recv() {
Ok(frames) => {
for _ in frames {
// Do stuff with received frames
}
}
Err(e) => println!("Error during read: {}", e)
};
}
Nathan Sizemore, nathanrsizemore@gmail.com
simple-stream is available under the MPL-2.0 license. See the LICENSE file for more info.