#socket #stream #non-blocking #websocket #ssl

simple-stream

Provides various building blocks, abstractions, and types for streams

33 releases (9 breaking)

Uses old Rust 2015

0.10.1 Jun 23, 2016
0.9.6 May 5, 2016
0.3.5 Mar 31, 2016
0.1.0 Aug 5, 2015

#65 in WebSocket

Download history 103/week @ 2022-03-14 102/week @ 2022-03-21 1/week @ 2022-04-11 1/week @ 2022-04-18 201/week @ 2022-04-25 265/week @ 2022-05-02 34/week @ 2022-05-09 562/week @ 2022-05-16 199/week @ 2022-05-23 231/week @ 2022-05-30 99/week @ 2022-06-06 1/week @ 2022-06-13 2/week @ 2022-06-20 35/week @ 2022-06-27

203 downloads per month

MPL-2.0 license

42KB
852 lines

simple-stream

Documentation


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.


Example Usage

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)
    };
}

Author

Nathan Sizemore, nathanrsizemore@gmail.com

License

simple-stream is available under the MPL-2.0 license. See the LICENSE file for more info.

Dependencies

~1.5–2.7MB
~61K SLoC