Struct glommio::net::TcpListener [−][src]
pub struct TcpListener { /* fields omitted */ }
Expand description
A TCP socket server, listening for connections.
After creating a TcpListener by binding it to a socket address, it listens
for incoming TCP connections. These can be accepted by calling accept
or
shared_accept
, or by iterating over the Incoming iterator returned by
incoming
.
A good networking architecture within a thread-per-core model needs to take into account parallelism and spawn work into multiple executors. If everything happens inside the same Executor, then at most one thread is used. Sometimes this is what you want: you may want to dedicate a CPU entirely for networking, or even use specialized ports for each CPU of the application, but most likely it isn’t.
There are two approaches to load balancing possible with the TcpListener
:
-
By default, the ReusePort flag is set in the socket automatically. The OS already provides some load balancing capabilities with that so you can simply
bind
to the same address from many executors. -
If that is insufficient or otherwise not desirable, it is possible to use
shared_accept
instead ofaccept
: that returns an object that implementsSend
. You can then use ashared_channel
to send the accepted connection into multiple executors. The object returned byshared_accept
can then be bound to its executor withbind_to_executor
, at which point it becomes a standardTcpStream
.
Relying on the OS is definitely simpler, but which approach is better depends on the specific needs of your application.
The socket will be closed when the value is dropped.
Implementations
Creates a TCP listener bound to the specified address.
Binding with port number 0 will request an available port from the OS.
This method sets the ReusePort option in the bound socket, so it is designed to be called from multiple executors to achieve parallelism.
Examples
use glommio::{net::TcpListener, LocalExecutor};
let ex = LocalExecutor::default();
ex.run(async move {
let listener = TcpListener::bind("127.0.0.1:8000").unwrap();
println!("Listening on {}", listener.local_addr().unwrap());
});
Accepts a new incoming TCP connection and allows the result to be sent to a foreign executor
This is similar to accept
, except it returns an
AcceptedTcpStream
instead of a TcpStream
.
AcceptedTcpStream
implements Send
, so it can be safely sent
for processing over a shared channel to a different executor.
This is useful when the user wants to do her own load balancing across multiple executors instead of relying on the load balancing the OS would do with the ReusePort property of the bound socket.
Examples
use glommio::{net::TcpListener, LocalExecutor};
let ex = LocalExecutor::default();
ex.run(async move {
let listener = TcpListener::bind("127.0.0.1:8000").unwrap();
let stream = listener.shared_accept().await.unwrap();
});
Accepts a new incoming TCP connection in this executor
This is similar to calling shared_accept
and bind_to_executor
in
a single operation.
If this connection once accepted is to be handled by the same executor in which it was accepted, this version is preferred.
Examples
use futures_lite::stream::StreamExt;
use glommio::{net::TcpListener, LocalExecutor};
let ex = LocalExecutor::default();
ex.run(async move {
let listener = TcpListener::bind("127.0.0.1:8000").unwrap();
let stream = listener.accept().await.unwrap();
println!("Accepted client: {:?}", stream.local_addr());
});
Creates a stream of incoming connections
Examples
use futures_lite::stream::StreamExt;
use glommio::{net::TcpListener, LocalExecutor};
let ex = LocalExecutor::default();
ex.run(async move {
let listener = TcpListener::bind("127.0.0.1:8000").unwrap();
let mut incoming = listener.incoming();
while let Some(conn) = incoming.next().await {
println!("Accepted client: {:?}", conn);
}
});
Returns the socket address of the local half of this TCP connection.
Examples
use glommio::{net::TcpListener, LocalExecutor};
let ex = LocalExecutor::default();
ex.run(async move {
let listener = TcpListener::bind("127.0.0.1:8000").unwrap();
println!("Listening on {}", listener.local_addr().unwrap());
});
Gets the value of the IP_TTL
option for this socket.
This option configures the time-to-live field that is used in every packet sent from this socket.
Examples
use glommio::{net::TcpListener, LocalExecutor};
let ex = LocalExecutor::default();
ex.run(async move {
let listener = TcpListener::bind("127.0.0.1:8000").unwrap();
listener.set_ttl(100).expect("could not set TTL");
assert_eq!(listener.ttl().unwrap(), 100);
});
Sets the value of the IP_TTL
option for this socket.
This option configures the time-to-live field that is used in every packet sent from this socket.
Examples
use glommio::{net::TcpListener, LocalExecutor};
let ex = LocalExecutor::default();
ex.run(async move {
let listener = TcpListener::bind("127.0.0.1:8000").unwrap();
listener.set_ttl(100).expect("could not set TTL");
assert_eq!(listener.ttl().unwrap(), 100);
});
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for TcpListener
impl !Send for TcpListener
impl !Sync for TcpListener
impl Unpin for TcpListener
impl !UnwindSafe for TcpListener
Blanket Implementations
Mutably borrows from an owned value. Read more
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more