[][src]Struct ductile::ChannelServer

pub struct ChannelServer<S, R> { /* fields omitted */ }

Listener for connections on some TCP socket.

The connection between the two parts is full-duplex and the types of message shared can be different. S and R are the types of message sent and received respectively. When initialized with an encryption key it is expected that the remote clients use the same key. Clients that use the wrong key are disconnected during an initial handshake.

Implementations

impl<S, R> ChannelServer<S, R>[src]

pub fn bind<A: ToSocketAddrs>(addr: A) -> Result<ChannelServer<S, R>>[src]

Bind a TCP socket and create a new ChannelServer. Only proper sockets are supported (not Unix sockets yet). This method does not enable message encryption.

let server = ChannelServer::<i32, i32>::bind("127.0.0.1:12357").unwrap();
assert!(ChannelServer::<(), ()>::bind("127.0.0.1:12357").is_err()); // port already in use

std::thread::spawn(move || {
    let (sender, receiver) = connect_channel::<_, i32, i32>("127.0.0.1:12357").unwrap();
    assert_eq!(receiver.recv().unwrap(), 42i32);
    sender.send(69i32).unwrap();
});

for (sender, receiver, address) in server {
    sender.send(42i32).unwrap();
    assert_eq!(receiver.recv().unwrap(), 69i32);
}

pub fn bind_with_enc<A: ToSocketAddrs>(
    addr: A,
    enc_key: [u8; 32]
) -> Result<ChannelServer<S, R>>
[src]

Bind a TCP socket and create a new ChannelServer. All the data transferred within this socket is encrypted using ChaCha20 initialized with the provided key. That key should be the same used by the clients that connect to this server.

let key = [42; 32];
let server = ChannelServer::<i32, i32>::bind_with_enc("127.0.0.1:12357", key).unwrap();
assert!(ChannelServer::<(), ()>::bind("127.0.0.1:12357").is_err()); // port already in use

std::thread::spawn(move || {
    let (sender, receiver) = connect_channel_with_enc::<_, i32, i32>("127.0.0.1:12357", &key).unwrap();
    assert_eq!(receiver.recv().unwrap(), 42i32);
    sender.send(69i32).unwrap();
});

for (sender, receiver, address) in server {
    sender.send(42i32).unwrap();
    assert_eq!(receiver.recv().unwrap(), 69i32);
}

Methods from Deref<Target = TcpListener>

pub fn local_addr(&self) -> Result<SocketAddr, Error>1.0.0[src]

Returns the local socket address of this listener.

Examples

use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, TcpListener};

let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
assert_eq!(listener.local_addr().unwrap(),
           SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080)));

pub fn try_clone(&self) -> Result<TcpListener, Error>1.0.0[src]

Creates a new independently owned handle to the underlying socket.

The returned TcpListener is a reference to the same socket that this object references. Both handles can be used to accept incoming connections and options set on one listener will affect the other.

Examples

use std::net::TcpListener;

let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
let listener_clone = listener.try_clone().unwrap();

pub fn accept(&self) -> Result<(TcpStream, SocketAddr), Error>1.0.0[src]

Accept a new incoming connection from this listener.

This function will block the calling thread until a new TCP connection is established. When established, the corresponding TcpStream and the remote peer's address will be returned.

Examples

use std::net::TcpListener;

let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
match listener.accept() {
    Ok((_socket, addr)) => println!("new client: {:?}", addr),
    Err(e) => println!("couldn't get client: {:?}", e),
}

pub fn incoming(&self) -> Incoming1.0.0[src]

Returns an iterator over the connections being received on this listener.

The returned iterator will never return None and will also not yield the peer's SocketAddr structure. Iterating over it is equivalent to calling accept in a loop.

Examples

use std::net::TcpListener;

let listener = TcpListener::bind("127.0.0.1:80").unwrap();

for stream in listener.incoming() {
    match stream {
        Ok(stream) => {
            println!("new client!");
        }
        Err(e) => { /* connection failed */ }
    }
}

pub fn set_ttl(&self, ttl: u32) -> Result<(), Error>1.9.0[src]

Sets the value for the IP_TTL option on this socket.

This value sets the time-to-live field that is used in every packet sent from this socket.

Examples

use std::net::TcpListener;

let listener = TcpListener::bind("127.0.0.1:80").unwrap();
listener.set_ttl(100).expect("could not set TTL");

pub fn ttl(&self) -> Result<u32, Error>1.9.0[src]

Gets the value of the IP_TTL option for this socket.

For more information about this option, see set_ttl.

Examples

use std::net::TcpListener;

let listener = TcpListener::bind("127.0.0.1:80").unwrap();
listener.set_ttl(100).expect("could not set TTL");
assert_eq!(listener.ttl().unwrap_or(0), 100);

pub fn set_only_v6(&self, only_v6: bool) -> Result<(), Error>1.9.0[src]

👎 Deprecated since 1.16.0:

this option can only be set before the socket is bound

pub fn only_v6(&self) -> Result<bool, Error>1.9.0[src]

👎 Deprecated since 1.16.0:

this option can only be set before the socket is bound

pub fn take_error(&self) -> Result<Option<Error>, Error>1.9.0[src]

Gets the value of the SO_ERROR option on this socket.

This will retrieve the stored error in the underlying socket, clearing the field in the process. This can be useful for checking errors between calls.

Examples

use std::net::TcpListener;

let listener = TcpListener::bind("127.0.0.1:80").unwrap();
listener.take_error().expect("No error was expected");

pub fn set_nonblocking(&self, nonblocking: bool) -> Result<(), Error>1.9.0[src]

Moves this TCP stream into or out of nonblocking mode.

This will result in the accept operation becoming nonblocking, i.e., immediately returning from their calls. If the IO operation is successful, Ok is returned and no further action is required. If the IO operation could not be completed and needs to be retried, an error with kind io::ErrorKind::WouldBlock is returned.

On Unix platforms, calling this method corresponds to calling fcntl FIONBIO. On Windows calling this method corresponds to calling ioctlsocket FIONBIO.

Examples

Bind a TCP listener to an address, listen for connections, and read bytes in nonblocking mode:

use std::io;
use std::net::TcpListener;

let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
listener.set_nonblocking(true).expect("Cannot set non-blocking");

for stream in listener.incoming() {
    match stream {
        Ok(s) => {
            // do something with the TcpStream
            handle_connection(s);
        }
        Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
            // wait until network socket is ready, typically implemented
            // via platform-specific APIs such as epoll or IOCP
            wait_for_fd();
            continue;
        }
        Err(e) => panic!("encountered IO error: {}", e),
    }
}

Trait Implementations

impl<S, R> Deref for ChannelServer<S, R>[src]

type Target = TcpListener

The resulting type after dereferencing.

impl<S, R> Iterator for ChannelServer<S, R>[src]

type Item = (ChannelSender<S>, ChannelReceiver<R>, SocketAddr)

The type of the elements being iterated over.

Auto Trait Implementations

impl<S, R> RefUnwindSafe for ChannelServer<S, R> where
    R: RefUnwindSafe,
    S: RefUnwindSafe

impl<S, R> !Send for ChannelServer<S, R>

impl<S, R> !Sync for ChannelServer<S, R>

impl<S, R> Unpin for ChannelServer<S, R>

impl<S, R> UnwindSafe for ChannelServer<S, R> where
    R: RefUnwindSafe,
    S: RefUnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<I> IteratorRandom for I where
    I: Iterator
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,