tcp-channel-server
A lightweight, async TCP server framework for Rust built on top of Tokio.
Features
- Async TCP server powered by Tokio
- 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)
Requirements
- Rust 1.75 or later (edition 2021)
Installation
Add the following to your Cargo.toml:
[]
= "0.2"
= { = "1", = ["full"] }
To enable TLS support:
[]
= { = "0.2", = ["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 |
Related Crates
- tcpclient — Async TCP client companion crate
License
Licensed under either of
- Apache License, Version 2.0
- MIT License
at your option.