39 releases (18 stable)

2.0.0 Jan 18, 2024
1.6.1 Apr 20, 2023
1.6.0 Mar 20, 2022
1.5.3 Dec 11, 2021
0.1.5 Oct 30, 2020

#1842 in Network programming

Download history 9/week @ 2024-06-23 142/week @ 2024-06-30 54/week @ 2024-07-14 3/week @ 2024-07-21 323/week @ 2024-07-28 7/week @ 2024-08-11 9/week @ 2024-08-25 3/week @ 2024-09-01 5/week @ 2024-09-08 83/week @ 2024-09-22 17/week @ 2024-09-29 1/week @ 2024-10-06

102 downloads per month
Used in 3 crates (via netxserver)

MIT/Apache

16KB
325 lines

#rust tcp server frame.

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

client crate url

Examples Echo

use anyhow::Result;
use std::sync::Arc;
use tcpserver::{Builder, IPeer, ITCPServer};
use tokio::io::AsyncReadExt;

#[tokio::main]
async fn main() -> Result<()> {
    let tcpserver: Arc<dyn ITCPServer<()>> = Builder::new("0.0.0.0:5555")
        .set_connect_event(|addr| {
            println!("{:?} connect", addr);
            true
        })
        .set_stream_init(|tcp_stream| async move { Ok(tcp_stream) })
        .set_input_event(|mut reader, peer, _| async move  {
            let mut buff = [0; 4096];
            while let Ok(len) = reader.read(&mut buff).await {
                if len == 0 {
                    break;
                }
                peer.send_ref(buff[..len].to_vec()).await?;
            }
            println!("{:?} disconnect", peer.addr());
            Ok(())
        })
        .build()
        .await;
    tcpserver.start_block(()).await?;
    Ok(())
}

Dependencies

~3–12MB
~131K SLoC