Crate simple_stream [] [src]

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() {
    // Starting with an established TcpStream
    let tcp_stream = TcpStream::connect("127.0.0.1").unwrap();

    // Create a socket that takes ownership of the underlying fd
    let socket = Socket::new(tcp_stream.into_raw_fd());

    // Pick a built-in stream type to wrap the socket
    let plain_text = Plain::new(socket);

    // Create a Stream
    let mut stream = Stream::new(Box::new(plain_text));

    // Write a thing
    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();
        }
    }

    // Receive all the things
    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();
        }
    }
}

Modules

blocking

Module containing blocking I/O stream types

nonblocking

Module containing non-blocking I/O stream types

Structs

Socket

Wrapper for file descriptor based sockets

Stream

Generic wrapper for anything implementing the SStream trait

Traits

CloneStream

The CloneStream trait allows for specialized cloning of trait objects.

SRecv

The SRecv trait allows for reading bytes from a source.

SSend

The SSend trait allows for the writing of bytes to a source.

SStream

The SStream trait represents the entirety of methods each specialized inner stream will have.

SocketOptions

The SocketOptions trait allows for various system-level settings for Berkley Sockets.

StreamShutdown

The StreamShutdown is used for sutting down the stream source.

TcpOptions

The TcpOptions trait allows for various TCP setting used in syscalls throughout Unix-like kernels.