Skip to main content

mill_net/
lib.rs

1//! High-level networking abstractions based on Mill-IO event loop library.
2//!
3//! This library provides high-level networking abstractions that integrate with
4//! Mill-IO's reactor-based event loop. The design eliminates the need for async/await
5//! while providing efficient non-blocking I/O through handler callbacks.
6//!
7//! # Architecture Overview
8//!
9//! ```text
10//! +-------------------------------------------------------------+
11//! |                      User Application                       |
12//! |  +--------------+          +-----------------+              |
13//! |  | TcpServer/   |----------| Your Handler    |              |
14//! |  | TcpClient    |          | (NetworkHandler)|              |
15//! |  +--------------+          +-----------------+              |
16//! +-------------+----------------------+------------------------+
17//!              |                      | Callbacks
18//!              | Register             | (on_connect, on_data, etc.)
19//!              |                      |
20//! +-------------------------------------------------------------+
21//! |                      Mill-IO EventLoop                      |
22//! |  +----------+       +----------+       +--------------+     |
23//! |  | Reactor  |-------| Handlers |-------| Thread Pool  |     |
24//! |  | (Poll)   |       | Registry |       |              |     |
25//! |  +----------+       +----------+       +--------------+     |
26//! +-------------+-----------------------------------------------+
27//!              | OS Events
28//!              |
29//! +-------------------------------------------------------------+
30//! |              Operating System (epoll/kqueue/IOCP)           |
31//! +-------------------------------------------------------------+
32//! ```
33//!
34//! The reactor continuously polls for I/O events. When an event occurs (data arrives,
35//! connection accepted, etc.), it dispatches the event to a worker thread from the
36//! thread pool, which then invokes the appropriate handler method you implemented.
37//!
38//! # Example
39//!
40//! ```rust,no_run
41//! use mill_net::tcp::{TcpServer, TcpServerConfig};
42//! use mill_net::tcp::{traits::{NetworkHandler, ConnectionId}, ServerContext};
43//! use mill_io::{EventLoop, error::Result};
44//! use std::sync::Arc;
45//!
46//! struct EchoHandler;
47//!
48//! impl NetworkHandler for EchoHandler {
49//!     fn on_data(&self, _ctx: &ServerContext, _conn_id: ConnectionId, data: &[u8]) -> Result<()> {
50//!         // Echo back the data
51//!         println!("Received: {:?}", data);
52//!         Ok(())
53//!     }
54//! }
55//!
56//! # fn main() -> Result<()> {
57//! let config = TcpServerConfig::builder()
58//!     .address("127.0.0.1:8080".parse().unwrap())
59//!     .buffer_size(8192)
60//!     .build();
61//!
62//! let server = Arc::new(TcpServer::new(config, EchoHandler)?);
63//! let event_loop = Arc::new(EventLoop::default());
64//!
65//! server.start(&event_loop, mio::Token(0))?;
66//! event_loop.run()?;
67//! # Ok(())
68//! # }
69//! ```
70
71pub mod errors;
72pub mod tcp;