Crate in_stream

Source
Expand description

Trait system for facilitating non-blocking stream chaining with handshaking

§Example

use url2::prelude::*;
use in_stream::*;

let (send_binding, recv_binding) = crossbeam_channel::unbounded();

let server_thread = std::thread::spawn(move || {
    let config = TcpBindConfig::default();
    let config = TlsBindConfig::new(config).fake_certificate();
    let config = WssBindConfig::new(config);
    let mut listener:
        InStreamListenerWss<InStreamListenerTls<InStreamListenerTcp>> =
        InStreamListenerWss::bind(
            &url2!("ws://127.0.0.1:0"),
            config
        ).unwrap();

    println!("bound to: {}", listener.binding());
    send_binding.send(listener.binding()).unwrap();

    let mut srv = loop {
        match listener.accept() {
            Ok(srv) => break srv,
            Err(e) if e.would_block() => std::thread::yield_now(),
            Err(e) => panic!("{:?}", e),
        }
    };

    srv.write("hello from server".into()).unwrap();
    srv.flush().unwrap();

    std::thread::sleep(std::time::Duration::from_millis(100));

    let mut res = WsFrame::default();
    srv.read(&mut res).unwrap();
    assert_eq!("hello from client", res.as_str());
});

let client_thread = std::thread::spawn(move || {
    let binding = recv_binding.recv().unwrap();
    println!("connect to: {}", binding);

    let mut cli: InStreamWss<InStreamTls<InStreamTcp>> =
        InStreamWss::connect(
            &binding,
            WssConnectConfig::new(
                TlsConnectConfig::new(
                    TcpConnectConfig::default()))).unwrap();

    cli.write("hello from client".into()).unwrap();
    cli.flush().unwrap();

    std::thread::sleep(std::time::Duration::from_millis(100));

    let mut res = WsFrame::default();
    cli.read(&mut res).unwrap();
    assert_eq!("hello from server", res.as_str());
});

server_thread.join().unwrap();
client_thread.join().unwrap();

Re-exports§

pub use json_rpc::*;

Modules§

in_stream_mem
json_rpc

Structs§

CloseData
a close message, with a websocket close code https://www.iana.org/assignments/websocket/websocket.xml#close-code-number
InStreamListenerMem
bind to a virtual in-process ipc “interface”
InStreamListenerTcp
basic tcp socket server/listener
InStreamListenerTls
bind to a network interface to listen for TLS connections
InStreamListenerWss
bind to a network interface to listen for websocket connections
InStreamMem
a singleton memory transport could be used for unit testing or for in-process ipc
InStreamTcp
basic tcp socket stream
InStreamTls
basic tls wrapper stream
InStreamWss
websocket stream
MemBindConfig
memory connection specific bind config
MemConnectConfig
memory stream specific connect config
StdStreamAdapter
provides std::io::{Read, Write} adapter for anything implementing InStreamStd
TcpBindConfig
configuration options for the listener bind call
TcpConnectConfig
configuration options for tcp connect
TlsBindConfig
tls specific bind config
TlsCertificate
represents an encrypted TLS certificate, and the passphrase to decrypt it obviously, when serializing, you should only encode the data, not the passphrase
TlsConnectConfig
tls specific connection config
WssBindConfig
websocket specific bind configuration
WssConnectConfig
websocket specific connect config

Enums§

WsFrame
enumerates the different websocket message types

Traits§

InStream
implement this trait to provide a stream endpoint / socket type connection works like combined std::io::{Read, Write}, but with generic types the underlying stream should be treated as non-blocking. For example, if this is a TLS stream, we may still need to complete a handshaking process before data can actually be written.
InStreamConfig
mixin helper for converting structs into an Any and back again
InStreamListener
implement this trait to provide listening/server socket type functionality
InStreamListenerStd
implement this if your listener accepts std::io::{Read, Write} streams
InStreamStd
implement this if your stream deals with binary u8 data
IoErrorExt
provide some convenience functions for working with non-blocking IO

Type Aliases§

InStreamConfigAny
dynamic type for passing around in_stream configuration this is a stopgap until rust allows better constraints on associated types