pub struct UnixListener { /* private fields */ }Expand description
A Unix socket server, listening for connections.
You can accept a new connection by using the accept
method.
§Examples
use tokio_uring::net::UnixListener;
use tokio_uring::net::UnixStream;
let sock_file = "/tmp/tokio-uring-unix-test.sock";
let listener = UnixListener::bind(&sock_file).unwrap();
tokio_uring::start(async move {
let (tx_ch, rx_ch) = tokio::sync::oneshot::channel();
tokio_uring::spawn(async move {
let rx = listener.accept().await.unwrap();
if let Err(_) = tx_ch.send(rx) {
panic!("The receiver dropped");
}
});
tokio::task::yield_now().await; // Ensure the listener.accept().await has been kicked off.
let tx = UnixStream::connect(&sock_file).await.unwrap();
let rx = rx_ch.await.expect("The spawned task expected to send a UnixStream");
tx.write(b"test" as &'static [u8]).submit().await.0.unwrap();
let (_, buf) = rx.read(vec![0; 4]).await;
assert_eq!(buf, b"test");
});
std::fs::remove_file(&sock_file).unwrap();Implementations§
Source§impl UnixListener
impl UnixListener
Sourcepub fn bind<P: AsRef<Path>>(path: P) -> Result<UnixListener>
pub fn bind<P: AsRef<Path>>(path: P) -> Result<UnixListener>
Creates a new UnixListener, which will be bound to the specified file path.
The file path cannnot yet exist, and will be cleaned up upon dropping UnixListener
Sourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
Returns the local address that this listener is bound to.
§Examples
use tokio_uring::net::UnixListener;
use std::path::Path;
let sock_file = "/tmp/tokio-uring-unix-test.sock";
let listener = UnixListener::bind(&sock_file).unwrap();
let addr = listener.local_addr().expect("Couldn't get local address");
assert_eq!(addr.as_pathname(), Some(Path::new(sock_file)));
std::fs::remove_file(&sock_file).unwrap();Sourcepub async fn accept(&self) -> Result<UnixStream>
pub async fn accept(&self) -> Result<UnixStream>
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.
Auto Trait Implementations§
impl Freeze for UnixListener
impl !RefUnwindSafe for UnixListener
impl !Send for UnixListener
impl !Sync for UnixListener
impl Unpin for UnixListener
impl !UnwindSafe for UnixListener
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more