[−][src]Struct memory_socket::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 calling accept
or by iterating over
iterating over the Incoming
iterator returned by incoming
.
The socket will be closed when the value is dropped.
Examples
use std::io::{Read, Result, Write}; use memory_socket::{MemoryListener, MemorySocket}; 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)?; stream.flush() } fn main() -> Result<()> { let mut listener = MemoryListener::bind(16)?; // accept connections and process them serially for stream in listener.incoming() { write_stormlight(stream?)?; } Ok(()) }
Implementations
impl MemoryListener
[src]
pub fn incoming_stream(&mut self) -> IncomingStream
[src]
Returns a stream over the connections being received on this listener.
The returned stream will never return None
.
Examples
use futures::prelude::*; use memory_socket::MemoryListener; let mut listener = MemoryListener::bind(80).unwrap(); let mut incoming = listener.incoming_stream(); while let Some(stream) = incoming.next().await { match stream { Ok(stream) => { println!("new client!"); } Err(e) => { /* connection failed */ } } }
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 memory_socket::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 memory_socket::MemoryListener; let listener = MemoryListener::bind(16)?; assert_eq!(listener.local_addr(), 16);
pub fn incoming(&self) -> Incoming
[src]
Returns an iterator over the connections being received on this listener.
The returned iterator will never return None
. Iterating over
it is equivalent to calling accept
in a loop.
Examples
use memory_socket::MemoryListener; use std::io::{Read, Write}; let mut listener = MemoryListener::bind(80).unwrap(); for stream in listener.incoming() { match stream { Ok(stream) => { println!("new client!"); } Err(e) => { /* connection failed */ } } }
pub fn accept(&self) -> Result<MemorySocket>
[src]
Accept a new incoming connection from this listener.
This function will block the calling thread until a new connection
is established. When established, the corresponding MemorySocket
will be returned.
Examples
use std::net::TcpListener; use memory_socket::MemoryListener; let mut listener = MemoryListener::bind(8080).unwrap(); match listener.accept() { Ok(_socket) => println!("new client!"), Err(e) => println!("couldn't get client: {:?}", e), }
Trait Implementations
impl Drop for MemoryListener
[src]
Auto Trait Implementations
impl !RefUnwindSafe for MemoryListener
impl Send for MemoryListener
impl !Sync for MemoryListener
impl Unpin for MemoryListener
impl !UnwindSafe for MemoryListener
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,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
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.
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>,