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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Clippy: allow style warnings that don't affect correctness
//! # Lynn_tcp
//! `Lynn_tcp` is a lightweight TCP server framework
//! ## Keywords
//! **Lightweight**: concise code that is easier to learn and use
//!
//! **Concurrent and Performance**: Based on Tokio's excellent asynchronous performance, it is easy to achieve concurrent processing capabilities for multi-user links
//!
//! **Lower latency**: Design with read and write separation to achieve lower latency
//!
//! **Security**: Code written with strong typing and memory safety in Rust
//! ## Features
//! - **server**: Provide customizable TCP services that can easily achieve multi-user long connections and concurrent processing capabilities, with services for different routes
//! - **client**: Provides a custom TCP client that sends and receives messages to and from a TCP server
//! ## Server
//! Represents a server for the Lynn application.
//!
//! The `LynnServer` struct holds information about the server, including its configuration,
//! client list, router map, and thread pool.
//!
//! ### Example
//! Use default config
//! ```rust,no_run
//! use lynn_tcp::{lynn_server::*, lynn_tcp_dependents::*};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let _ = LynnServer::new()
//! .await
//! .add_router(1, my_service)
//! .add_router(2, my_service_with_buf)
//! .add_router(3, my_service_with_clients)
//! .start()
//! .await;
//! Ok(())
//! }
//!
//! pub async fn my_service() -> HandlerResult {
//! HandlerResult::new_without_send()
//! }
//! pub async fn my_service_with_buf(input_buf_vo: InputBufVO) -> HandlerResult {
//! println!(
//! "service read from :{}",
//! input_buf_vo.get_input_addr().unwrap()
//! );
//! HandlerResult::new_without_send()
//! }
//! pub async fn my_service_with_clients(clients_context: ClientsContext) -> HandlerResult {
//! HandlerResult::new_with_send(1, "hello lynn".into(), clients_context.get_all_clients_addrs().await)
//! }
//! ```
//! ### Example
//! Use customized config
//! ```rust,no_run
//! use lynn_tcp::{lynn_server::*, lynn_tcp_dependents::*};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let _ = LynnServer::new_with_config(
//! LynnServerConfigBuilder::new()
//! .with_addr("0.0.0.0:9177").unwrap()
//! .with_server_max_connections(Some(&200))
//! // Suggestion 256-512
//! .with_server_max_taskpool_size(&512)
//! // ...more
//! .build(),
//! )
//! .await
//! .add_router(1, my_service)
//! .add_router(2, my_service_with_buf)
//! .add_router(3, my_service_with_clients)
//! .start()
//! .await;
//! Ok(())
//! }
//!
//! pub async fn my_service() -> HandlerResult {
//! HandlerResult::new_without_send()
//! }
//! pub async fn my_service_with_buf(input_buf_vo: InputBufVO) -> HandlerResult {
//! println!(
//! "service read from :{}",
//! input_buf_vo.get_input_addr().unwrap()
//! );
//! HandlerResult::new_without_send()
//! }
//! pub async fn my_service_with_clients(clients_context: ClientsContext) -> HandlerResult {
//! HandlerResult::new_with_send(1, "hello lynn".into(), clients_context.get_all_clients_addrs().await)
//! }
//! ```
//! ## Clinet
//! A client for communicating with a server over TCP.
//!
//! The `LynnClient` struct represents a client that can connect to a server, send data, and receive data.
//! It uses a configuration object to specify the server's IP address and other settings.
//! The client runs in a separate task and uses channels to communicate with the main task.
//! ### Example
//! Use default config (If you want to use custom configuration, please use `LynnClientConfigBuilder`)
//! ```rust,no_run
//! use lynn_tcp::{
//! lynn_client::LynnClient,
//! lynn_tcp_dependents::*,
//! };
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let mut client = LynnClient::new_with_ipv4("127.0.0.1:9177")
//! .await
//! .start()
//! .await;
//! let _ = client.send_data(HandlerResult::new_with_send_to_server(1, "hello".into())).await;
//! let input_buf_vo = client.get_receive_data().await.unwrap();
//! Ok(())
//! }
//! ```
// ===== 分层架构模块注册 =====
/// 应用层(编排层)
pub
/// 常量配置模块
/// 领域层(纯业务逻辑)
pub
/// 错误类型模块
/// 基础设施层(TCP/事件循环/指标/限流等具体实现)
pub
pub extern crate bytes;
pub extern crate tokio;
pub extern crate tracing;
pub extern crate tracing_subscriber;
/// Re-export common error types
pub use ;
/// Metrics module for monitoring
/// The server module, containing the server configuration API and server implementation.
/// The TCP dependents module, containing common types used by both the server and the client.
/// The global state module: shared values injected into server handler
/// parameters through `AppState<T>` (like axum's `State<T>`).
/// Built-in SeaORM integration: register a database handle with
/// `LynnServer::with_db(...)` (or `with_state`) and extract it in handlers
/// through the [`DbConn`] alias. Requires the optional `seaorm` feature.
/// Optional TLS 1.3 transport security (feature `tls`, disabled by default).
///
/// Servers enable TLS through `LynnServerConfigBuilder::with_tls(...)`,
/// clients through `LynnClientConfigBuilder::with_tls(...)`. Both sides are
/// restricted to TLS 1.3 (rustls + ring).
/// The client module, containing the client configuration and client implementation.