aerox/
lib.rs

1//! # AeroX - 高性能游戏服务器后端框架
2//!
3//! AeroX 是一个专注于游戏服务器后端和实时消息转发场景的高性能 Rust 框架。
4//! 它基于 Reactor 模式实现高并发连接处理,并整合了 ECS 架构。
5//!
6//! ## 特性
7//!
8//! - 基于 Tokio 异步运行时的高性能 Reactor 模式
9//! - 支持 TCP 协议(KCP 和 QUIC 规划中)
10//! - Protobuf 消息协议支持
11//! - Bevy ECS 整合
12//! - 灵活的路由和中间件系统
13//! - 插件化架构
14//!
15//! ## 快速开始
16//!
17//! ### 使用 Prelude(推荐)
18//!
19//! ```rust,no_run,ignore
20//! use aerox::prelude::*;  // 导入所有常用类型
21//!
22//! #[tokio::main]
23//! async fn main() -> Result<()> {
24//!     Server::bind("127.0.0.1:8080")
25//!         .route(1001, |ctx| async move {
26//!             println!("Received: {:?}", ctx.data());
27//!             Ok(())
28//!         })
29//!         .run()
30//!         .await
31//! }
32//! ```
33//!
34//! ### 服务器
35//!
36//! ```rust,no_run,ignore
37//! use aerox::Server;
38//!
39//! #[tokio::main]
40//! async fn main() -> aerox::Result<()> {
41//!     Server::bind("127.0.0.1:8080")
42//!         .route(1001, |ctx| async move {
43//!             println!("Received: {:?}", ctx.data());
44//!             Ok(())
45//!         })
46//!         .run()
47//!         .await
48//! }
49//! ```
50//!
51//! ### 客户端
52//!
53//! ```rust,no_run,ignore
54//! use aerox::Client;
55//!
56//! #[tokio::main]
57//! async fn main() -> aerox::Result<()> {
58//!     let mut client = Client::connect("127.0.0.1:8080").await?;
59//!     client.send(1001, &message).await?;
60//!     Ok(())
61//! }
62//! ```
63//!
64//! ## 模块组织
65//!
66//! ### 配置模块 (aerox_config)
67//! - ServerConfig - 服务器基础配置
68//! - ReactorConfig - Reactor 模式配置
69//!
70//! ### 核心模块 (aerox_core)
71//! - App - 应用构建器
72//! - Plugin - 插件 trait
73//! - State - 状态管理
74//! - Connection - 连接抽象
75//! - ConnectionId - 连接唯一标识
76//!
77//! ### ECS 模块 (aerox_ecs)
78//! - EcsWorld - ECS 世界管理
79//! - NetworkBridge - 网络事件桥接
80//! - EventScheduler - 事件调度器
81//! - GameSystems - 游戏系统集合
82//! - 组件: Player, Position, Velocity, Health 等
83//! - 事件: ConnectionEstablishedEvent, MessageReceivedEvent 等
84//!
85//! ### 网络模块 (aerox_network)
86//! - Transport - 传输层抽象
87//! - TcpReactor - TCP 反应器
88//! - Connection - 连接管理
89//! - ConnectionId - 连接唯一标识
90//! - MessageCodec - 消息编解码器
91//!
92//! ### Protobuf 模块 (aerox_protobuf)
93//! - MessageRegistry - 消息注册表
94//! - MessageEncoder - 消息编码器
95//! - encode_message/decode_message - 编解码函数
96//!
97//! ### 路由模块 (aerox_router)
98//! - Router - 消息路由器
99//! - Handler - 消息处理器 trait
100//! - Context - 请求上下文
101//! - Middleware - 中间件 trait
102//! - Extensions - 类型安全的扩展数据
103//!
104//! ### 插件模块 (aerox_plugins)
105//! - HeartbeatPlugin - 心跳检测插件
106//! - RateLimitPlugin - 限流插件
107//!
108//! ## 示例
109//!
110//! - `ecs_basics` - ECS 基础和位置更新系统
111//! - `router_middleware` - 路由和中间件系统
112//! - `complete_game_server` - 完整的游戏服务器示例
113//!
114//! 运行示例:
115//!
116//! ```bash
117//! cargo run --example ecs_basics
118//! cargo run --example router_middleware
119//! cargo run --example complete_game_server
120//! ```
121
122// ============================================================================
123// Conditional Compilation Based on Features
124// ============================================================================
125
126// Client API
127#[cfg(feature = "client")]
128pub mod client;
129
130#[cfg(feature = "client")]
131pub use crate::client::{Client, StreamClient};
132
133// Server API
134#[cfg(feature = "server")]
135pub mod server;
136
137#[cfg(feature = "server")]
138pub use crate::server::{Server, ServerBuilder};
139
140// ============================================================================
141// Crate Re-exports (for advanced users)
142// ============================================================================
143
144#[cfg(feature = "server")]
145pub use aerox_config;
146
147#[cfg(feature = "server")]
148pub use aerox_core;
149
150#[cfg(feature = "server")]
151pub use aerox_network;
152
153#[cfg(feature = "server")]
154pub use aerox_protobuf;
155
156#[cfg(feature = "server")]
157pub use aerox_ecs;
158
159#[cfg(feature = "server")]
160pub use aerox_router;
161
162#[cfg(feature = "server")]
163pub use aerox_plugins;
164
165#[cfg(feature = "client")]
166pub use aerox_client;
167
168// ============================================================================
169// Prelude Module
170// ============================================================================
171
172/// 预导出常用类型
173///
174/// 通过 `use aerox::prelude::*;` 导入所有常用类型
175///
176/// ## 包含的内容
177///
178/// ### 服务器端 API
179/// - 配置管理(aerox_config)
180/// - 核心运行时和插件系统(aerox_core)
181/// - ECS 游戏逻辑(aerox_ecs)
182/// - 网络传输层(aerox_network)
183/// - Protobuf 编解码(aerox_protobuf)
184/// - 路由和中间件(aerox_router)
185/// - 官方插件(aerox_plugins)
186///
187/// ### 客户端 API
188/// - 高级客户端(Client)
189/// - 流式客户端(StreamClient)
190///
191/// ### 高级 API
192/// - 服务器构建器(Server, ServerBuilder)
193///
194/// ### 统一错误处理
195/// - Error(统一错误类型)
196/// - Result(统一结果类型)
197pub mod prelude {
198    // 标准库重导出
199    pub use std::result::Result as StdResult;
200
201    // === 服务器端 API ===
202
203    // 配置模块
204    #[cfg(feature = "server")]
205    pub use aerox_config::{ServerConfig, ReactorConfig, ConfigError};
206
207    // 核心模块 - 应用、插件、连接管理
208    #[cfg(feature = "server")]
209    pub use aerox_core::{
210        App,           // 应用构建器
211        Plugin,        // 插件 trait
212        State,         // 状态管理
213        Connection,    // 连接抽象
214        ConnectionId,  // 连接 ID
215        AeroXError,    // 统一错误类型
216        Result as CoreResult,  // 核心结果类型
217    };
218
219    // ECS 模块 - 游戏逻辑核心
220    #[cfg(feature = "server")]
221    pub use aerox_ecs::prelude::*;
222
223    // 网络模块 - 传输层
224    #[cfg(feature = "server")]
225    pub use aerox_network::prelude::*;
226
227    // Protobuf 支持 - 消息编解码
228    #[cfg(feature = "server")]
229    pub use aerox_protobuf::prelude::*;
230
231    // 路由系统 - 消息分发
232    #[cfg(feature = "server")]
233    pub use aerox_router::prelude::*;
234
235    // 官方插件
236    #[cfg(feature = "server")]
237    pub use aerox_plugins::prelude::*;
238
239    // === 客户端 API ===
240    #[cfg(feature = "client")]
241    pub use crate::client::{Client, StreamClient};
242
243    // === 高级 API ===
244    #[cfg(feature = "server")]
245    pub use crate::server::{Server, ServerBuilder};
246
247    // === 统一错误处理 ===
248    pub use crate::{Error, Result};
249}
250
251// ============================================================================
252// Error Types
253// ============================================================================
254
255/// AeroX 统一错误类型
256pub type Result<T> = std::result::Result<T, Error>;
257
258/// AeroX 统一错误枚举
259#[derive(Debug, thiserror::Error)]
260pub enum Error {
261    /// 核心错误
262    #[cfg(feature = "server")]
263    #[error(transparent)]
264    Core(#[from] aerox_core::AeroXError),
265
266    /// 客户端错误
267    #[cfg(feature = "client")]
268    #[error(transparent)]
269    Client(#[from] aerox_client::ClientError),
270
271    /// 配置错误
272    #[cfg(feature = "server")]
273    #[error(transparent)]
274    Config(#[from] aerox_config::ConfigError),
275
276    /// IO 错误
277    #[error(transparent)]
278    Io(#[from] std::io::Error),
279
280    /// 自定义错误
281    #[error("{0}")]
282    Custom(String),
283}
284
285// ============================================================================
286// Version Information
287// ============================================================================
288
289/// AeroX 版本号
290pub const VERSION: &str = env!("CARGO_PKG_VERSION");
291
292/// AeroX 包名
293pub const NAME: &str = env!("CARGO_PKG_NAME");