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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//! # WebIO Utility & Protocol Engine
//!
//! This module provides a collection of deterministic helpers for filesystem
//! discovery, WebSocket protocol encoding, and cross-thread communication.
//!
//! ### Key Utility Capabilities:
//! * **Recursive Asset Discovery:** Performs depth-first searches to locate
//! hidden assets (like `favicon.ico`) in complex directory trees.
//! * **RFC 6455 Handshake:** Implements the official WebSocket "Magic String"
//! logic using the internal `crypto` module.
//! * **Dynamic Frame Encoding:** Constructs WebSocket binary frames with
//! support for 7-bit, 16-bit, and 64-bit "Big Data" lengths.
//! * **Global Broadcast Hub:** Utilizes `LazyLock` and `Mutex` to manage
//! a thread-safe registry of active WebSocket participants for real-time sync.
use ;
use fs;
use ;
use TcpStream;
use Write;
use crate;
// --- SYSTEM UTILITIES & BROADCAST ENGINE ---
/// Performs a depth-first recursive search for a specific filename within a directory tree.
///
/// This utility enables dynamic asset discovery, such as locating a `favicon.ico`
/// hidden within deep subdirectories. By traversing the filesystem recursively,
/// WebIO provides a flexible "Smart Static" delivery system that reduces
/// manual route configuration for complex frontend directory structures.
/// Generates the cryptographic `Sec-WebSocket-Accept` signature (RFC 6455).
///
/// This internal helper combines the client-provided key with the WebSocket
/// "Magic String" (GUID) and performs a SHA-1 hash followed by Base64 encoding.
/// This deterministic handshake is the security foundation of WebIO's
/// zero-dependency WebSocket implementation.
/// Serializes a raw text message into a compliant WebSocket Binary Frame.
///
/// Following the **RFC 6455 Section 5.2** protocol, this function constructs
/// unmasked server-to-client frames. It intelligently scales the frame header
/// based on payload size, supporting everything from short status updates
/// (7-bit length) to massive Big Data broadcasts (64-bit length).
/// A globally accessible, thread-safe registry of active WebSocket participants.
///
/// Utilizes [`LazyLock`] (Rust 1.80+) for zero-cost initialization and a
/// [`Mutex`] to coordinate cross-thread access. This allows any OS thread
/// within the WebIO engine to interact with the persistent socket pool.
pub static CLIENTS: = new;
/// Disseminates a message to all active WebSocket clients across the entire engine.
///
/// `broadcast` is a high-level orchestration method that facilitates real-time
/// synchronization between isolated OS threads. It automatically performs
/// **Ghost Connection Cleanup**: if a write fails (e.g., client disconnected),
/// the socket is atomically removed from the registry, ensuring RAM stability.