#actor #async-io #tokio #aqueue

tcpclient

Asynchronous tcpclient based on aqueue actor

24 releases (stable)

2.0.0 Jan 18, 2024
1.4.1 Apr 20, 2023
1.4.0 Mar 20, 2022
1.3.2 Dec 11, 2021
0.1.0 Nov 27, 2020

#249 in Asynchronous

Download history 18/week @ 2024-06-10 1/week @ 2024-06-17 8/week @ 2024-06-24 19/week @ 2024-07-01 64/week @ 2024-07-15 75/week @ 2024-07-29 9/week @ 2024-08-12 22/week @ 2024-08-26 3/week @ 2024-09-09 5/week @ 2024-09-16 91/week @ 2024-09-23

105 downloads per month
Used in 5 crates (3 directly)

MIT/Apache

10KB
177 lines

Asynchronous tcpclient based on aqueue actor.

  • Version 2.+ Minimum supported Rust version: 1.75 or later

DEMO URL:

demo url

examples echo:

#![feature(async_closure)]
use tcpclient::{TcpClient,SocketClientTrait};
use tokio::io::AsyncReadExt;
use std::error::Error;
use log::LevelFilter;

#[tokio::main]
async fn main()->Result<(),Box<dyn Error>> {
    // set logger out
    env_logger::Builder::new().filter_level(LevelFilter::Debug).init();

    // connect echo server
    let client =
        TcpClient::connect("127.0.0.1:5555", async move |_, client, mut reader| {
            // read buff from target server
            let mut buff=[0;7];
            while let Ok(len) = reader.read_exact(&mut buff).await {
                // send buff to target server
                println!("{}",std::str::from_utf8(&buff[..len])?);
                client.send(&buff[..len]).await?;
            }
            // return true need disconnect,false not disconnect
            // if true and the current state is disconnected, it will be ignored.
            Ok(true)
        }, ()).await?;

    // connect ok send buff to target server
    client.send(b"1234567").await?;

    // test disconnect readline 
    let mut str = "".into();
    std::io::stdin().read_line(&mut str)?;

    // disconnect target server
    client.disconnect().await?;
    // wait env logger out show
    std::io::stdin().read_line(&mut str)?;
    Ok(())
}

Dependencies

~3–11MB
~105K SLoC