cat_dev/net/
mod.rs

1//! A series of "common" networking related utilities.
2//!
3//! These types, and structs, and logic aren't related to anything specific
4//! to cat-dev's at all. But are simply utilities that allow us to actually
5//! implement the rest of things like a "TCP Server", or "TCP Client".
6
7pub mod additions;
8#[cfg_attr(docsrs, doc(cfg(feature = "clients")))]
9#[cfg(feature = "clients")]
10pub mod client;
11pub mod errors;
12mod ext_map;
13pub mod handlers;
14pub mod models;
15#[cfg_attr(docsrs, doc(cfg(feature = "servers")))]
16#[cfg(feature = "servers")]
17pub mod server;
18
19pub use ext_map::Extensions;
20
21#[cfg(test)]
22use std::sync::RwLock;
23use std::{
24	sync::atomic::AtomicU64,
25	time::{Duration, SystemTime},
26};
27
28#[cfg(debug_assertions)]
29use std::{env::var as env_var, sync::LazyLock};
30
31/// The default slowdown to use for cat-dev units that seems to work.
32pub const DEFAULT_CAT_DEV_SLOWDOWN: Duration = Duration::from_millis(25);
33/// The default amount to chunk write calls as to not overwhelm the cat-dev.
34pub const DEFAULT_CAT_DEV_CHUNK_SIZE: usize = 65536;
35/// The default "slow-loris" timeout or how long before we should error
36/// because a connection is taking too long to send us a damn packet.
37const DEFAULT_SLOWLORIS_TIMEOUT: Duration = Duration::from_secs(10 * 60);
38/// An identifier for streams that increments globally.
39static STREAM_ID: AtomicU64 = AtomicU64::new(1);
40#[cfg(feature = "servers")]
41/// A global identifier for a particular TCP server.
42static SERVER_ID: AtomicU64 = AtomicU64::new(1);
43/// Determine how many bytes we should read in one "chunk" of a TCP Stream.
44static TCP_READ_BUFFER_SIZE: usize = 65536_usize;
45
46/// If we should be tracing IO regardless of what the user says.
47#[cfg(debug_assertions)]
48static SPRIG_TRACE_IO: LazyLock<bool> = LazyLock::new(|| {
49	if let Ok(variable) = env_var("SPRIG_FORCE_TRACE_ALL_IO") {
50		variable != "0" && !variable.is_empty()
51	} else {
52		false
53	}
54});
55
56thread_local! {
57	#[cfg(test)]
58	static CURRENT_TIME: LazyLock<RwLock<SystemTime>> = LazyLock::new(|| RwLock::new(SystemTime::now()));
59}
60
61#[cfg(not(test))]
62fn now() -> SystemTime {
63	SystemTime::now()
64}
65
66#[cfg(test)]
67fn now() -> SystemTime {
68	CURRENT_TIME.with(|time_lazy| time_lazy.read().expect("RwLock is poisioned?").clone())
69}
70
71#[cfg(test)]
72pub mod test_helpers {
73	#[cfg(feature = "servers")]
74	pub use crate::net::server::test_helpers::*;
75}