Struct lunatic::net::TlsListener

source ·
pub struct TlsListener { /* private fields */ }
Expand description

A TLS server, listening for connections.

After creating a TlsListener by binding it to an address, it listens for incoming encrypted TCP (TLS) connections. These can be accepted by calling accept().

The Transmission Control Protocol is specified in IETF RFC 793.

Examples

use lunatic::{net, Mailbox, Process};
use std::io::{BufRead, BufReader, Write};

fn main() {
    let listener = net::TlsListener::bind("127.0.0.1:0").unwrap();
    while let Ok((tls_stream, _peer)) = listener.accept() {
        // Handle connections in a new process
        Process::spawn(tls_stream, handle);
    }
}

fn handle(mut tls_stream: net::TlsStream, _: Mailbox<()>) {
    let mut buf_reader = BufReader::new(tls_stream.clone());
    loop {
        let mut buffer = String::new();
        let read = buf_reader.read_line(&mut buffer).unwrap();
        if buffer.contains("exit") || read == 0 {
            return;
        }
        tls_stream.write(buffer.as_bytes()).unwrap();
    }
}

Implementations§

source§

impl TlsListener

source

pub fn bind<A>(addr: A, certs: Vec<u8>, keys: Vec<u8>) -> Result<Self>where A: ToSocketAddrs,

Creates a new TlsListener bound to the given address.

Binding with a port number of 0 will request that the operating system assigns an available port to this listener.

If addr yields multiple addresses, binding will be attempted with each of the addresses until one succeeds and returns the listener. If none of the addresses succeed in creating a listener, the error from the last attempt is returned.

source

pub fn accept(&self) -> Result<(TlsStream, SocketAddr)>

Accepts a new incoming connection.

This will block and typically needs its own dedicated child process loop.

Returns a TLS stream and the peer address.

source

pub fn local_addr(&self) -> Result<SocketAddr>

Returns the local address that this listener is bound to.

This can be useful, for example, to identify when binding to port 0 which port was assigned by the OS.

Trait Implementations§

source§

impl Debug for TlsListener

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Drop for TlsListener

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.