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
//! # maestro-rs
//!
//! Supervised TCP and UDP services bound to a specific network interface.
//!
//! Implement [`TcpHandler`] or [`UdpHandler`] for your protocol logic, register
//! the handlers on a [`Supervisor`] for a [`NetworkInterface`], and call
//! [`Supervisor::run`]. Maestro takes care of:
//!
//! - **Binding**: to every address of the interface (including scoped IPv6
//! link-local addresses), to all interfaces, or to one address. See [`BindMode`].
//! - **Concurrency**: one task per TCP connection, and UDP receive work on
//! every CPU core (`SO_REUSEPORT` sharding on Linux, shared sockets elsewhere).
//! - **Portability**: Linux, macOS, Windows and BSD. See
//! [`NetworkInterface::loopback`] for a portable loopback interface.
//! - **Multicast**: joining groups on the chosen interface.
//! See [`UdpHandler::multicast_groups`].
//! - **Supervision**: restarting failed services with exponential backoff.
//! See [`RestartPolicy`].
//! - **Shutdown**: on Ctrl+C, SIGTERM or Windows console close, or on any future you pass to
//! [`Supervisor::run_until`], services stop and in-flight work gets time to finish.
//!
//! # Example
//!
//! ```no_run
//! use maestro_rs::{NetworkInterface, Result, Supervisor, TcpHandler, UdpHandler};
//! use std::{net::SocketAddr, sync::Arc};
//! use tokio::{
//! io::{AsyncReadExt, AsyncWriteExt},
//! net::{TcpStream, UdpSocket},
//! };
//!
//! struct Echo;
//!
//! impl TcpHandler for Echo {
//! fn name(&self) -> &str { "tcp-echo" }
//! fn port(&self) -> u16 { 8080 }
//!
//! async fn on_connection(&self, mut stream: TcpStream, _peer: SocketAddr) {
//! let mut buf = [0u8; 1024];
//! while let Ok(n @ 1..) = stream.read(&mut buf).await {
//! if stream.write_all(&buf[..n]).await.is_err() {
//! break;
//! }
//! }
//! }
//! }
//!
//! impl UdpHandler for Echo {
//! fn name(&self) -> &str { "udp-echo" }
//! fn port(&self) -> u16 { 8080 }
//!
//! async fn on_packet(&self, data: &[u8], socket: &Arc<UdpSocket>, peer: SocketAddr) {
//! let _ = socket.send_to(data, peer).await;
//! }
//! }
//!
//! #[tokio::main]
//! async fn main() -> Result<()> {
//! let iface = NetworkInterface::loopback()?; // or "eth0".parse()?
//! let mut supervisor = Supervisor::new(iface);
//! // `Echo` implements both traits, so name the protocol explicitly.
//! supervisor
//! .add::<maestro_rs::Tcp, _>(Echo)
//! .add::<maestro_rs::Udp, _>(Echo);
//! supervisor.run().await
//! }
//! ```
//!
//! # Features
//!
//! - `tracing` (default): emit logs through [`tracing`](https://docs.rs/tracing).
//! Install a subscriber in your application to see them.
pub use BindMode;
pub use ;
pub use NetworkInterface;
pub use RestartPolicy;
pub use ;
pub use Supervisor;
pub use TcpHandler;
pub use UdpHandler;