The simple-stream crate provides simple abstraction building blocks over any type that implements std::io::Read
+ std::io::Write
, coupled with a FrameBuilder
. It provides built-in types for plain text and secured streams with help from rust-openssl. It includes both blocking and non-blocking modes for any type.
It works by handling all of the I/O on a frame based level. It includes a "simple" built-in framing pattern and WebSocket based framing. It supports custom Frames and FrameBuilders through the frame module traits.
~~~rust extern crate simple_stream as ss;
use ss::frame::Frame; use ss::frame::simple::{SimpleFrame, SimpleFrameBuilder}; use ss::{Socket, Plain, NonBlocking, SocketOptions};
fn main() { // tcpstream is some connection established std::net::TcpStream // // Take ownership of the underlying fd to remove TcpStream's Drop being called now // that we're switching types. let fd = tcpstream.intorawfd();
// Create a socket and set any POSIX based TCP/SOL_SOCKET options
let mut socket = Socket::new(fd);
socket.set_keepalive(true);
socket.set_nonblocking();
// Create a plain text based stream that reads messages with SimpleFrame type
let mut plain_stream = Plain::<Socket, SimpleFrameBuilder>::new(socket);
// Perform non-blocking read
match plain_stream.nb_recv() {
Ok(frames) => {
// msgs is a Vec<Box<Frame>>
for frame in frames.iter() {
// Do stuff with received things
}
}
Err(e) => {
// Error handling here
}
}
// Perform non-blocking write
let frame = SimpleFrame::new(&some_buf[..]);
plain_stream.nb_send(&frame).map_err(|e| {
// Error handling here
});
} ~~~
Nathan Sizemore, nathanrsizemore@gmail.com
simple-stream is available under the MPL-2.0 license. See the LICENSE file for more info.