use std::{
ffi::{OsStr, OsString},
io,
};
use tokio::net::windows::named_pipe::{NamedPipeClient, NamedPipeServer};
use super::pipe::{self, PipeServer};
pub(crate) type LocalAccepted = NamedPipeServer;
pub(crate) type LocalConnected = NamedPipeClient;
pub(crate) struct LocalListener {
pipe: PipeServer,
}
impl LocalListener {
pub(crate) fn bind_private(prefix: &str) -> io::Result<(Self, OsString)> {
let name = OsString::from(format!(
r"\\.\pipe\{prefix}-{}",
uuid::Uuid::new_v4().simple()
));
let pipe = PipeServer::create(&name)?;
Ok((Self { pipe }, name))
}
pub(crate) async fn accept(&mut self) -> io::Result<LocalAccepted> {
self.pipe.accept().await
}
}
pub(crate) async fn connect_local(address: &OsStr) -> io::Result<LocalConnected> {
pipe::open(address).await?.ok_or_else(|| {
io::Error::new(
io::ErrorKind::NotFound,
format!("nothing listens on {}", address.display()),
)
})
}