1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//! 与协议无关的连接处理抽象。
//!
//! `ConnectionService` trait 提供统一的接口来处理任意类型的网络连接,
//! 不依赖于具体的应用层协议(HTTP/gRPC/WebSocket 等)。
//!
//! # 核心概念
//!
//! - `BoxedConnection`: 类型擦除的连接流(可以是 TCP、Unix Socket、TLS 等)
//! - `BoxError`: 统一的错误类型
//! - `ConnectionFuture`: 异步连接处理的 Future
//!
//! # Examples
//!
//! 实现一个简单的 echo 服务:
//!
//! ```no_run
//! use silent::ConnectionService;
//! use tokio::io::{AsyncReadExt, AsyncWriteExt};
//!
//! struct EchoService;
//!
//! impl ConnectionService for EchoService {
//! fn call(
//! &self,
//! mut stream: silent::BoxedConnection,
//! _peer: silent::SocketAddr,
//! ) -> silent::ConnectionFuture {
//! Box::pin(async move {
//! let mut buf = vec![0u8; 1024];
//! loop {
//! let n = stream.read(&mut buf).await?;
//! if n == 0 {
//! break;
//! }
//! stream.write_all(&buf[..n]).await?;
//! }
//! Ok(())
//! })
//! }
//! }
//! ```
//!
//! 使用闭包(更简洁):
//!
//! ```no_run
//! use silent::NetServer;
//! use tokio::io::{AsyncReadExt, AsyncWriteExt};
//!
//! # async fn example() {
//! NetServer::new()
//! .bind("127.0.0.1:8080".parse().unwrap()).unwrap()
//! .serve(|mut stream: silent::BoxedConnection, peer: silent::SocketAddr| async move {
//! println!("Connection from: {}", peer);
//! let mut buf = vec![0u8; 1024];
//! let n = stream.read(&mut buf).await?;
//! stream.write_all(&buf[..n]).await?;
//! Ok(())
//! })
//! .await;
//! # }
//! ```
use crateSocketAddr as CoreSocketAddr;
use crateBoxedConnection;
use Error as StdError;
use Future;
use Pin;
/// 统一的错误类型,用于连接处理。
pub type BoxError = ;
/// 连接处理的 Future 类型。
pub type ConnectionFuture = ;
/// 与协议无关的连接处理服务。
///
/// 此 trait 定义了处理单个网络连接的统一接口,不依赖于具体的应用层协议。
///
/// # 实现方式
///
/// - **结构体实现**:适合复杂的状态管理和多个辅助方法
/// - **闭包实现**:自动通过 blanket impl 支持,适合简单场景
///
/// # Examples
///
/// 结构体实现:
///
/// ```no_run
/// use silent::{ConnectionService, ConnectionFuture, BoxedConnection, SocketAddr};
///
/// struct MyService {
/// config: String,
/// }
///
/// impl ConnectionService for MyService {
/// fn call(&self, stream: BoxedConnection, peer: SocketAddr) -> ConnectionFuture {
/// let config = self.config.clone();
/// Box::pin(async move {
/// // 使用 config 和 stream 处理连接
/// Ok(())
/// })
/// }
/// }
/// ```
///
/// 闭包实现(自动支持):
///
/// ```no_run
/// use silent::NetServer;
///
/// # async fn example() {
/// NetServer::new()
/// .bind("127.0.0.1:8080".parse().unwrap()).unwrap()
/// .serve(|stream: silent::BoxedConnection, _peer: silent::SocketAddr| async move {
/// // 直接处理连接
/// Ok(())
/// })
/// .await;
/// # }
/// ```
/// 为闭包自动实现 `ConnectionService`。
///
/// 这允许直接使用闭包作为连接处理器,而无需手动实现 trait。