Struct compio_net::UnixListener
source · pub struct UnixListener { /* private fields */ }
Expand description
A Unix socket server, listening for connections.
You can accept a new connection by using the UnixListener::accept
method.
Examples
use compio_net::{UnixListener, UnixStream};
use tempfile::tempdir;
let dir = tempdir().unwrap();
let sock_file = dir.path().join("unix-server.sock");
compio_runtime::block_on(async move {
let listener = UnixListener::bind(&sock_file).unwrap();
let tx = UnixStream::connect(&sock_file).unwrap();
let (rx, _) = listener.accept().await.unwrap();
tx.send_all("test").await.0.unwrap();
let (_, buf) = rx.recv_exact(Vec::with_capacity(4)).await.unwrap();
assert_eq!(buf, b"test");
});
Implementations§
source§impl UnixListener
impl UnixListener
sourcepub fn bind(path: impl AsRef<Path>) -> Result<Self>
pub fn bind(path: impl AsRef<Path>) -> Result<Self>
Creates a new UnixListener
, which will be bound to the specified
file path. The file path cannot yet exist, and will be cleaned up
upon dropping UnixListener
sourcepub fn bind_addr(addr: impl ToSockAddrs) -> Result<Self>
pub fn bind_addr(addr: impl ToSockAddrs) -> Result<Self>
Creates a new UnixListener
with SockAddr
, which will be bound to
the specified file path. The file path cannot yet exist, and will be
cleaned up upon dropping UnixListener
sourcepub fn try_clone(&self) -> Result<Self>
pub fn try_clone(&self) -> Result<Self>
Creates a new independently owned handle to the underlying socket.
It does not clear the attach state.
sourcepub async fn accept(&self) -> Result<(UnixStream, SockAddr)>
pub async fn accept(&self) -> Result<(UnixStream, SockAddr)>
Accepts a new incoming connection from this listener.
This function will yield once a new Unix domain socket connection
is established. When established, the corresponding UnixStream
and
will be returned.
sourcepub fn local_addr(&self) -> Result<SockAddr>
pub fn local_addr(&self) -> Result<SockAddr>
Returns the local address that this listener is bound to.