tcp-channel-server
tcp-channel-server — English
A lightweight, async TCP server framework for Rust built on top of Tokio.
- Minimum supported Rust version: 1.75+
- Fluent builder API for easy configuration
- Per-connection stream initialization hook (plain TCP, TLS, or any custom wrapper)
- Optional connection filter callback to accept or reject incoming peers
- Channel-based peer abstraction for safe, concurrent sends
- TLS support via
openssl(opt-in feature flag)
Features
| Feature | Description |
|---|---|
| 🔌 Async TCP Server | Powered by Tokio, fully asynchronous accept and I/O |
| 🏗️ Fluent Builder | Chainable configuration methods with compile-time safety |
| 🔐 TLS Support | Opt-in via tls feature flag, uses openssl + tokio-openssl |
| 📡 Channel-Based Writes | Per-connection writer task eliminates lock contention on the write side |
| 🎛️ Stream Init Hook | Transform raw TcpStream into any stream type (SslStream, etc.) |
| 🔗 Connect Filter | Accept or reject connections by SocketAddr before spawning a handler |
| 🏷️ User Token | Generic T cloned into every connection handler — share state without globals |
Requirements
- Rust 1.75 or later (edition 2021)
Installation
Add the following to your Cargo.toml:
[]
= "0.3"
= { = "1", = ["full"] }
To enable TLS support:
[]
= { = "0.3", = ["tls"] }
Quick Start
Echo Server
use Result;
use Arc;
use ;
use AsyncReadExt;
async
TLS Server
use Result;
use Pin;
use Arc;
use lazy_static;
use ;
use ;
use AsyncReadExt;
use SslStream;
lazy_static!
async
API Overview
Builder
The entry point for constructing a server. All methods consume self and return Self, enabling a fluent chain.
| Method | Required | Description |
|---|---|---|
Builder::new(addr) |
✓ | Bind address (anything that implements ToSocketAddrs) |
.set_stream_init(fn) |
✓ | Transform a raw TcpStream into the final stream type C |
.set_input_event(fn) |
✓ | Async handler called once per connection with (ReadHalf<C>, Arc<TCPPeer<C>>, T) |
.set_connect_event(fn) |
— | Return true to accept, false to reject an incoming address |
.build().await |
✓ | Consumes the builder and returns Arc<dyn ITCPServer<T>> |
ITCPServer<T>
T is an arbitrary user token that is cloned and passed into every input_event invocation —
useful for sharing state (e.g. Arc<AppState>) across connections.
TCPPeer<C>
Represents a connected client. It is cheaply cloneable via Arc and safe to use from multiple tasks.
| Method | Description |
|---|---|
peer.addr() |
Remote SocketAddr |
peer.send(buf) |
Enqueue bytes for writing (buffered, no flush) |
peer.send_all(buf) |
Enqueue bytes and flush |
peer.flush() |
Flush the write buffer |
peer.disconnect() |
Gracefully shut down the connection |
peer.is_disconnect() |
Check whether the peer is already disconnected |
Architecture
See CLAUDE.md for a detailed walkthrough of the internal architecture, including the connection lifecycle, generic parameters, and dependency roles.
Related Crates
- tcpclient — Async TCP client companion crate
License
Licensed under either of
- Apache License, Version 2.0
- MIT License
at your option.
tcp-channel-server — 中文
轻量级异步 TCP 服务端框架,基于 Tokio 构建。
- 最低支持 Rust 版本:1.75+
- 流式 Builder API,链式配置,简洁易用
- 每个连接可自定义流初始化(原生 TCP、TLS 或任意包装类型)
- 可选的连接过滤器,按地址接受或拒绝接入的 peer
- 基于 Channel 的 peer 抽象,安全并发发送
- 通过
openssl支持 TLS(可选 feature 标志)
功能特性
| 功能 | 描述 |
|---|---|
| 🔌 异步 TCP 服务端 | 基于 Tokio,全异步 accept 和 I/O |
| 🏗️ 流式 Builder | 链式配置方法,编译期类型安全 |
| 🔐 TLS 支持 | 通过 tls feature 标志启用,基于 openssl + tokio-openssl |
| 📡 Channel 写入 | 每个连接独立的 writer 任务,消除写端锁竞争 |
| 🎛️ Stream Init 钩子 | 将原始 TcpStream 转换为任意流类型(如 SslStream) |
| 🔗 连接过滤 | 在生成 handler 之前按 SocketAddr 接受或拒绝连接 |
| 🏷️ 用户 Token | 泛型 T 被克隆到每个连接处理函数中 — 无需全局变量即可共享状态 |
环境要求
- Rust 1.75 或更高版本(edition 2021)
安装
在 Cargo.toml 中添加:
[]
= "0.3"
= { = "1", = ["full"] }
启用 TLS 支持:
[]
= { = "0.3", = ["tls"] }
快速开始
Echo 服务端
use Result;
use Arc;
use ;
use AsyncReadExt;
async
TLS 服务端
use Result;
use Pin;
use Arc;
use lazy_static;
use ;
use ;
use AsyncReadExt;
use SslStream;
lazy_static!
async
API 概览
Builder
构建服务端的入口。所有方法消耗 self 并返回 Self,支持链式调用。
| 方法 | 必选 | 描述 |
|---|---|---|
Builder::new(addr) |
✓ | 绑定地址(任何实现了 ToSocketAddrs 的类型) |
.set_stream_init(fn) |
✓ | 将原始 TcpStream 转换为最终流类型 C |
.set_input_event(fn) |
✓ | 每个连接调用一次的异步处理函数,参数为 (ReadHalf<C>, Arc<TCPPeer<C>>, T) |
.set_connect_event(fn) |
— | 返回 true 接受连接,false 拒绝 |
.build().await |
✓ | 消耗 Builder 并返回 Arc<dyn ITCPServer<T>> |
ITCPServer<T>
T 是任意的用户 token,会被克隆并传入每次 input_event 调用 —
用于在连接之间共享状态(例如 Arc<AppState>)。
TCPPeer<C>
表示一个已连接的客户端。通过 Arc 可低成本克隆,多任务并发使用安全。
| 方法 | 描述 |
|---|---|
peer.addr() |
远程 SocketAddr |
peer.send(buf) |
将字节入队等待写入(缓冲,不立即 flush) |
peer.send_all(buf) |
将字节入队并立即 flush |
peer.flush() |
刷新写缓冲区 |
peer.disconnect() |
优雅关闭连接 |
peer.is_disconnect() |
检查 peer 是否已断开 |
架构
详见 CLAUDE.md,其中包含内部架构的详细说明,包括连接生命周期、泛型参数和依赖角色。
相关 Crate
- tcpclient — 配套异步 TCP 客户端 crate
开源协议
本项目采用双许可证:
- Apache License, Version 2.0
- MIT License
任选其一。