tcp-channel-server

Crates.iotcp-channel-server
lib.rstcp-channel-server
version0.1.1
sourcesrc
created_at2024-06-09 12:45:35.319172
updated_at2024-06-10 14:07:41.683265
descriptionrust tcp channel server frame.
homepage
repositoryhttps://github.com/luyikk/tcp_channel_server
max_upload_size
id1266355
size52,547
(luyikk)

documentation

https://docs.rs/tcp_channel_server

README

#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(())
}

Commit count: 5

cargo fmt