[−][src]Struct tari_comms::memsocket::MemoryListener
An in-memory socket server, listening for connections.
After creating a MemoryListener
by bind
ing it to a socket address, it listens
for incoming connections. These can be accepted by awaiting elements from the
async stream of incoming connections, incoming
.
The socket will be closed when the value is dropped.
Examples
use std::io::Result; use tari_comms::memsocket::{MemoryListener, MemorySocket}; use futures::prelude::*; async fn write_stormlight(mut stream: MemorySocket) -> Result<()> { let msg = b"The most important step a person can take is always the next one."; stream.write_all(msg).await?; stream.flush().await } async fn listen() -> Result<()> { let mut listener = MemoryListener::bind(16)?; let mut incoming = listener.incoming(); // accept connections and process them serially while let Some(stream) = incoming.next().await { write_stormlight(stream?).await?; } Ok(()) }
Implementations
impl MemoryListener
[src]
pub fn bind(port: u16) -> Result<Self>
[src]
Creates a new MemoryListener
which will be bound to the specified
port.
The returned listener is ready for accepting connections.
Binding with a port number of 0 will request that a port be assigned
to this listener. The port allocated can be queried via the
local_addr
method.
Examples
Create a MemoryListener bound to port 16:
use tari_comms::memsocket::MemoryListener; let listener = MemoryListener::bind(16)?;
pub fn local_addr(&self) -> u16
[src]
Returns the local address that this listener is bound to.
This can be useful, for example, when binding to port 0 to figure out which port was actually bound.
Examples
use tari_comms::memsocket::MemoryListener; let listener = MemoryListener::bind(16)?; assert_eq!(listener.local_addr(), 16);
pub fn incoming(&mut self) -> Incoming<'_>
[src]
Consumes this listener, returning a stream of the sockets this listener accepts.
This method returns an implementation of the Stream
trait which
resolves to the sockets the are accepted on this listener.
Examples
use futures::prelude::*; use tari_comms::memsocket::MemoryListener; let mut listener = MemoryListener::bind(16)?; let mut incoming = listener.incoming(); // accept connections and process them serially while let Some(stream) = incoming.next().await { match stream { Ok(stream) => { println!("new connection!"); }, Err(e) => { /* connection failed */ } } }
Trait Implementations
impl Debug for MemoryListener
[src]
impl Drop for MemoryListener
[src]
Auto Trait Implementations
impl !RefUnwindSafe for MemoryListener
[src]
impl Send for MemoryListener
[src]
impl Sync for MemoryListener
[src]
impl Unpin for MemoryListener
[src]
impl !UnwindSafe for MemoryListener
[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> Cast<U> for T where
U: FromCast<T>,
U: FromCast<T>,
pub fn cast(self) -> U
impl<T> From<T> for T
[src]
impl<T> FromBits<T> for T
pub fn from_bits(t: T) -> T
impl<T> FromCast<T> for T
pub fn from_cast(t: T) -> T
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> IntoBits<U> for T where
U: FromBits<T>,
U: FromBits<T>,
pub fn into_bits(self) -> U
impl<T> SafeBorrow<T> for T where
T: ?Sized,
T: ?Sized,
pub fn borrow_replacement(ptr: &T) -> &T
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]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,