simple-stream
Documentation
The simple-stream crate provides a simple framing protocol over any type that implements the
SStream
trait. It also provides built-in support for blocking and non-blocking streams over
Unix file descriptors in both plain-text and SSL through rust-openssl.
Usage
extern crate simple_stream as ss;
use std::net::TcpStream;
use std::os::unix::io::IntoRawFd;
use ss::{Socket, Stream, SSend, SRecv, StreamShutdown};
use ss::nonblocking::plain::Plain;
fn main() {
let tcp_stream = TcpStream::connect("127.0.0.1").unwrap();
let socket = Socket::new(tcp_stream.into_raw_fd());
let plain_text = Plain::new(socket);
let mut stream = Stream::new(Box::new(plain_text));
let buffer = "ping".as_bytes();
match stream.send(&buffer[..]) {
Ok(num_written) => println!("Wrote: {} bytes", num_written),
Err(e) => {
println!("Error during write: {}", e);
stream.shutdown().unwrap();
}
}
match stream.recv() {
Ok(()) => {
let mut queue = stream.drain_rx_queue();
for msg in queue.drain(..) {
println!("Received: {}", String::from_utf8(msg).unwrap());
}
}
Err(e) => {
println!("Error during read: {}", e);
stream.shutdown().unwrap();
}
}
}
Author
Nathan Sizemore, nathanrsizemore@gmail.com
License
simple-stream is available under the MPL-2.0 license. See the LICENSE file for more info.