rusty_enet/lib.rs
1//! [ENet](https://github.com/lsalzman/enet) transpiled to Rust, and made agnostic to the underlying
2//! socket. Supports [`std::net::UdpSocket`] out of the box. Works in WASM if you bring your own WebRTC
3//! interface or similar.
4//!
5//! Much of the docs are copied from the [ENet Website](http://sauerbraten.org/enet/index.html),
6//! both for convenience, and in case that resource is unavailable for any reason.
7//!
8//! > ENet's purpose is to provide a relatively thin, simple and robust network communication layer
9//! > on top of UDP (User Datagram Protocol). The primary feature it provides is optional reliable,
10//! > in-order delivery of packets.
11//! >
12//! > ENet omits certain higher level networking features such as authentication, lobbying, server
13//! > discovery, encryption, or other similar tasks that are particularly application specific so
14//! > that the library remains flexible, portable, and easily embeddable.
15//!
16//! [See the examples](https://github.com/jabuwu/rusty_enet/tree/main/examples)
17//!
18//! # Features and Architecture
19//!
20//! ENet evolved specifically as a UDP networking layer for the multiplayer first person shooter
21//! Cube.
22//!
23//! Cube necessitated low latency communication with data sent out very frequently, so TCP was an
24//! unsuitable choice due to its high latency and stream orientation. UDP, however, lacks many
25//! sometimes necessary features from TCP such as reliability, sequencing, unrestricted packet
26//! sizes, and connection management. So UDP by itself was not suitable as a network protocol
27//! either. No suitable freely available networking libraries existed at the time of ENet's creation
28//! to fill this niche.
29//!
30//! UDP and TCP could have been used together in Cube to benefit somewhat from both of their
31//! features, however, the resulting combinations of protocols still leaves much to be desired.
32//! TCP lacks multiple streams of communication without resorting to opening many sockets and
33//! complicates delineation of packets due to its buffering behavior. UDP lacks sequencing,
34//! connection management, management of bandwidth resources, and imposes limitations on the size of
35//! packets. A significant investment is required to integrate these two protocols, and the end
36//! result is worse off in features and performance than the uniform protocol presented by ENet.
37//!
38//! ENet thus attempts to address these issues and provide a single, uniform protocol layered over
39//! UDP to the developer with the best features of UDP and TCP as well as some useful features
40//! neither provide, with a much cleaner integration than any resulting from a mixture of UDP and
41//! TCP.
42//!
43//! ## Connection Management
44//!
45//! ENet provides a simple connection interface over which to communicate with a foreign host. The
46//! liveness of the connection is actively monitored by pinging the foreign host at frequent
47//! intervals, and also monitors the network conditions from the local host to the foreign host such
48//! as the mean round trip time and packet loss in this fashion.
49//!
50//! ## Sequencing
51//!
52//! Rather than a single byte stream that complicates the delineation of packets, ENet presents
53//! connections as multiple, properly sequenced packet streams that simplify the transfer of various
54//! types of data.
55//!
56//! ENet provides sequencing for all packets by assigning to each sent packet a sequence number that
57//! is incremented as packets are sent. ENet guarantees that no packet with a higher sequence number
58//! will be delivered before a packet with a lower sequence number, thus ensuring packets are
59//! delivered exactly in the order they are sent.
60//!
61//! For unreliable packets, ENet will simply discard the lower sequence number packet if a packet
62//! with a higher sequence number has already been delivered. This allows the packets to be
63//! dispatched immediately as they arrive, and reduce latency of unreliable packets to an absolute
64//! minimum. For reliable packets, if a higher sequence number packet arrives, but the preceding
65//! packets in the sequence have not yet arrived, ENet will stall delivery of the higher sequence
66//! number packets until its predecessors have arrived.
67//!
68//! ## Channels
69//!
70//! Since ENet will stall delivery of reliable packets to ensure proper sequencing, and consequently
71//! any packets of higher sequence number whether reliable or unreliable, in the event the reliable
72//! packet's predecessors have not yet arrived, this can introduce latency into the delivery of
73//! other packets which may not need to be as strictly ordered with respect to the packet that
74//! stalled their delivery.
75//!
76//! To combat this latency and reduce the ordering restrictions on packets, ENet provides multiple
77//! channels of communication over a given connection. Each channel is independently sequenced, and
78//! so the delivery status of a packet in one channel will not stall the delivery of other packets
79//! in another channel.
80//!
81//! ## Reliability
82//!
83//! ENet provides optional reliability of packet delivery by ensuring the foreign host acknowledges
84//! receipt of all reliable packets. ENet will attempt to resend the packet up to a reasonable
85//! amount of times, if no acknowledgement of the packet's receipt happens within a specified
86//! timeout. Retry timeouts are progressive and become more lenient with every failed attempt to
87//! allow for temporary turbulence in network conditions.
88//!
89//! ## Fragmentation and Reassembly
90//!
91//! ENet will send and deliver packets regardless of size. Large packets are fragmented into many
92//! smaller packets of suitable size, and reassembled on the foreign host to recover the original
93//! packet for delivery. The process is entirely transparent to the developer.
94//!
95//! ## Aggregation
96//!
97//! ENet aggregates all protocol commands, including acknowledgements and packet transfer, into
98//! larger protocol packets to ensure the proper utilization of the connection and to limit the
99//! opportunities for packet loss that might otherwise result in further delivery latency.
100//!
101//! ## Adaptability
102//!
103//! ENet provides an in-flight data window for reliable packets to ensure connections are not
104//! overwhelmed by volumes of packets. It also provides a static bandwidth allocation mechanism to
105//! ensure the total volume of packets sent and received to a host don't exceed the host's
106//! capabilities. Further, ENet also provides a dynamic throttle that responds to deviations from
107//! normal network connections to rectify various types of network congestion by further limiting
108//! the volume of packets sent.
109
110#![no_std]
111#![cfg_attr(docsrs, feature(doc_cfg))]
112#![warn(
113 missing_docs,
114 clippy::missing_panics_doc,
115 clippy::missing_errors_doc,
116 clippy::manual_assert,
117 clippy::ptr_cast_constness,
118 clippy::ptr_as_ptr,
119 clippy::default_trait_access,
120 clippy::explicit_iter_loop,
121 clippy::explicit_into_iter_loop,
122 clippy::needless_pass_by_value,
123 clippy::option_if_let_else,
124 clippy::redundant_feature_names,
125 clippy::semicolon_if_nothing_returned,
126 clippy::must_use_candidate,
127 clippy::borrow_as_ptr,
128 clippy::items_after_statements,
129 clippy::single_match_else,
130 clippy::bool_to_int_with_if,
131 clippy::unnecessary_cast
132)]
133// https://github.com/rust-lang/rust-clippy/issues/11382
134#![allow(clippy::arc_with_non_send_sync)]
135
136#[cfg(feature = "std")]
137#[macro_use]
138extern crate std;
139
140#[cfg(not(feature = "std"))]
141extern crate alloc;
142
143mod address;
144mod c;
145mod compressor;
146mod crc32;
147mod event;
148mod host;
149mod packet;
150mod peer;
151mod read_write;
152mod socket;
153mod time;
154mod version;
155
156pub use address::*;
157pub(crate) use c::*;
158pub use compressor::*;
159pub use crc32::*;
160pub use event::*;
161pub use host::*;
162pub use packet::*;
163pub use peer::*;
164pub use read_write::*;
165pub use socket::*;
166pub use time::*;
167pub use version::*;
168
169#[cfg(any(feature = "connected", doc))]
170#[cfg_attr(docsrs, doc(cfg(feature = "connected")))]
171pub mod connected;
172pub mod error;
173
174/// Constants provided by ENet.
175#[allow(missing_docs)]
176pub mod consts;
177
178#[cfg(feature = "std")]
179#[cfg(test)]
180mod test;
181
182#[cfg(feature = "std")]
183pub(crate) use std::{boxed::Box, collections::VecDeque, vec::Vec};
184
185#[cfg(not(feature = "std"))]
186pub(crate) use alloc::{boxed::Box, collections::VecDeque, vec::Vec};