use anyhow;
use kio;

use std::string::String;
use std::time;

fn main() -> anyhow::Result<()> {
    kio::run(|kio| async { run(kio).await.unwrap() })
}

async fn run(mut kio: kio::Context) -> anyhow::Result<()> {
    // Set up the connection limit
    let mut kio = kio.timeout(time::Duration::from_secs(5));

    let mut stream = kio.connect("127.0.0.1:8080").await?;
    let message = "hello world";

    kio.write_full(&mut stream, message.as_bytes()).await?;
    kio.write_close(&mut stream)?;

    println!("sent: {}", message);

    let mut buffer = kio.buffer(32);
    let size = kio.read_full(&mut stream, &mut buffer).await?;

    println!("received: {}", String::from_utf8_lossy(&buffer[..size]));

    Ok(())
}
