mirror of https://github.com/smol-rs/smol
48 lines
1.3 KiB
Rust
48 lines
1.3 KiB
Rust
// TODO: document
|
|
#[cfg(windows)]
|
|
fn main() -> std::io::Result<()> {
|
|
use std::path::PathBuf;
|
|
|
|
use futures::io;
|
|
use futures::prelude::*;
|
|
use smol::{Async, Task};
|
|
use tempfile::tempdir;
|
|
use uds_windows::{UnixListener, UnixStream};
|
|
|
|
async fn client(addr: PathBuf) -> io::Result<()> {
|
|
let stream = Async::new(UnixStream::connect(addr)?)?;
|
|
println!("Connected to {:?}", stream.get_ref().peer_addr()?);
|
|
|
|
let mut stdout = smol::writer(std::io::stdout());
|
|
io::copy(&stream, &mut stdout).await?;
|
|
Ok(())
|
|
}
|
|
|
|
let dir = tempdir()?;
|
|
let path = dir.path().join("socket");
|
|
|
|
smol::run(async {
|
|
// Create a listener.
|
|
let listener = Async::new(UnixListener::bind(&path)?)?;
|
|
println!("Listening on {:?}", listener.get_ref().local_addr()?);
|
|
|
|
// Spawn a client task.
|
|
let task = Task::spawn(client(path));
|
|
|
|
// Accept the client.
|
|
let (stream, _) = listener.with(|l| l.accept()).await?;
|
|
println!("Accepted a client");
|
|
|
|
// Send a message, drop the stream, and wait for the client.
|
|
Async::new(stream)?.write_all(b"Hello!\n").await?;
|
|
task.await?;
|
|
|
|
Ok(())
|
|
})
|
|
}
|
|
|
|
#[cfg(not(windows))]
|
|
fn main() {
|
|
println!("This example works only on Windows!");
|
|
}
|