pub struct Listener {
pub addr: Option<IpAddr>,
pub port: u16,
pub proto: Protocol,
handle: JoinHandle<()>,
}
Expand description
A links redirector listener.
Listens for incoming network connections on a specified address using a specified protocol in an async task in the background. On drop, the async task is aborted in order to stop listening.
Fields§
§addr: Option<IpAddr>
The address this listener will listen on. No address indicates that this
listener will accept all traffic on any address (IPv4 and IPv6),
0.0.0.0
means any IPv4 address (but not IPv6), [::]
means any IPv6
address (but not IPv4).
port: u16
The port this listener will listen on. Currently, this is a TCP port, but may in the future also additionally indicate a UDP port.
proto: Protocol
The protocol of the acceptor/handler this listener uses to process requests
handle: JoinHandle<()>
Implementations§
Source§impl Listener
impl Listener
Sourcepub async fn new(
addr: Option<IpAddr>,
port: Option<u16>,
acceptor: &'static impl Acceptor<TcpStream>,
) -> Result<Self, IoError>
pub async fn new( addr: Option<IpAddr>, port: Option<u16>, acceptor: &'static impl Acceptor<TcpStream>, ) -> Result<Self, IoError>
Create a new Listener
on the specified address, which will use the
specified acceptor to accept incoming connections. If no address is
specified, the listener will listen on all IPv4 and IPv6 addresses.
Address 0.0.0.0
can be used to listen on all IPv4 (but not IPv6)
addresses, and address [::]
can be used to listen on all IPv6 (but not
IPv4) addresses. If the port is not specified, the protocol’s default
port will be used (see Protocol
for details).
Note:
Support for dual stack sockets (IPv4 and IPv6 in one socket, available
in links via an empty address) is not universal on all platforms (such
as some BSDs). On those platforms, an empty address and [::]
will
behave the same, i.e. an empty address will only listen on IPv6, not
IPv4. To get the desired result (IPv4 and IPv6), you must use two
listeners, one listening on 0.0.0.0
and the other on [::]
.
§Drop
When dropped, a listener will wait until its internal task is fully
cancelled, which can take some time to complete. Dropping a listener
should therefore be considered blocking, and only done in synchronous
contexts or via the spawn_blocking
function.
Additionally, because the drop
function blocks its thread until the
async runtime completes the cancellation of the task in the background,
a listener requires more than one thread to drop, and can not
successfully be dropped inside of a single-threaded tokio runtime (the
entire program will block indefinitely).
§Errors
This function returns an error if it can not set up the listening socket.
Sourcepub const fn listen_address(&self) -> ListenAddress
pub const fn listen_address(&self) -> ListenAddress
Get the ListenAddress
of this listener
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Listener
impl RefUnwindSafe for Listener
impl Send for Listener
impl Sync for Listener
impl Unpin for Listener
impl UnwindSafe for Listener
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request