pub struct MemoryListener { /* private fields */ }
Expand description
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§
Source§impl MemoryListener
impl MemoryListener
Sourcepub fn incoming_stream(&mut self) -> IncomingStream<'_>
pub fn incoming_stream(&mut self) -> IncomingStream<'_>
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 */ }
}
}
Source§impl MemoryListener
impl MemoryListener
Sourcepub fn bind(port: u16) -> Result<Self>
pub fn bind(port: u16) -> Result<Self>
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)?;
Sourcepub fn local_addr(&self) -> u16
pub fn local_addr(&self) -> u16
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);
Sourcepub fn incoming(&self) -> Incoming<'_> ⓘ
pub fn incoming(&self) -> Incoming<'_> ⓘ
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 */ }
}
}
Sourcepub fn accept(&self) -> Result<MemorySocket>
pub fn accept(&self) -> Result<MemorySocket>
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),
}