tcplane 8.1.4

tcplane is a lightweight and high-performance Rust TCP server library designed to simplify network service development. It supports TCP communication, data stream management, and connection handling, focusing on providing efficient low-level network connections and data transmission capabilities, making it ideal for building modern network services.
Documentation
use crate::*;

impl ServerHook for GreetingHandler {
    async fn new(_: &Context) -> Self {
        Self
    }

    async fn handle(self, ctx: &Context) {
        let _ = ctx.send("Hello from GreetingHandler!").await;
    }
}

impl ServerHook for EchoHandler {
    async fn new(_: &Context) -> Self {
        Self
    }

    async fn handle(self, ctx: &Context) {
        let request: Request = ctx.get_request().await;
        let response: String = format!("Echo: {request:?}");
        let _ = ctx.send(response).await;
    }
}

impl ServerHook for PanicHandler {
    async fn new(_: &Context) -> Self {
        Self
    }

    async fn handle(self, ctx: &Context) {
        let _ = ctx.send("Panic occurred!").await;
    }
}

impl ServerHook for ErrorHandler {
    async fn new(_: &Context) -> Self {
        Self
    }

    async fn handle(self, ctx: &Context) {
        if let Some(error) = ctx.try_get_data::<String, _>("error").await {
            eprintln!("{error}");
            let _ = std::io::Write::flush(&mut std::io::stderr());
        }
    }
}