Struct lunatic::net::TlsListener
source · pub struct TlsListener { /* private fields */ }
Expand description
A TLS server, listening for connections.
After creating a TlsListener
by bind
ing 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
impl TlsListener
sourcepub fn bind<A>(addr: A, certs: Vec<u8>, keys: Vec<u8>) -> Result<Self>where
A: ToSocketAddrs,
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.
sourcepub fn accept(&self) -> Result<(TlsStream, SocketAddr)>
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.
sourcepub fn local_addr(&self) -> Result<SocketAddr>
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.