Skip to main content

runecast_protocol/protocol/
mod.rs

1//! Protocol module for `RuneCast` WebSocket communication.
2//!
3//! This module defines all message types exchanged between the frontend client
4//! and backend server over WebSocket connections.
5//!
6//! # Architecture
7//!
8//! ```text
9//! ┌─────────────────────────────────────────────────────────────────────┐
10//! │                           Protocol Layer                             │
11//! ├─────────────────────────────────────────────────────────────────────┤
12//! │  types.rs        - Shared data types (Grid, Position, etc.)         │
13//! │  client_messages - Client → Server message definitions              │
14//! │  server_messages - Server → Client message definitions              │
15//! └─────────────────────────────────────────────────────────────────────┘
16//! ```
17//!
18//! # Message Flow
19//!
20//! ```text
21//! Client                                Server
22//!   │                                      │
23//!   │◀─── Hello ──────────────────────────│
24//!   │                                      │
25//!   │──── JoinChannelLobby ──────────────▶│
26//!   │◀─── LobbyJoined ────────────────────│
27//!   │                                      │
28//!   │──── StartGame ─────────────────────▶│
29//!   │◀─── GameStarted ────────────────────│
30//!   │                                      │
31//!   │──── SubmitWord ────────────────────▶│
32//!   │◀─── WordScored ─────────────────────│
33//!   │◀─── TurnChanged ────────────────────│
34//! ```
35//!
36
37pub mod client_messages;
38pub mod server_messages;
39pub mod types;
40
41// Re-export main types for convenient access
42pub use client_messages::ClientMessage;
43pub use server_messages::{LobbySnapshot, ServerMessage};
44pub use types::*;
45
46// ============================================================================
47// Protocol Constants
48// ============================================================================
49
50/// Recommended heartbeat interval (client should send heartbeat this often).
51pub const HEARTBEAT_INTERVAL_MS: u32 = 30_000;
52
53/// Heartbeat timeout (server closes connection if no heartbeat received).
54pub const HEARTBEAT_TIMEOUT_MS: u32 = 45_000;
55
56/// Grace period for reconnection before session expires.
57pub const RECONNECT_GRACE_MS: u32 = 60_000;
58
59/// Maximum message size in bytes.
60pub const MAX_MESSAGE_SIZE: usize = 64 * 1024; // 64 KB
61
62/// Protocol version for compatibility checks.
63pub const PROTOCOL_VERSION: &str = "1.0.0";
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn test_constants() {
71        assert_eq!(HEARTBEAT_INTERVAL_MS, 30_000);
72        const {
73            assert!(HEARTBEAT_TIMEOUT_MS > HEARTBEAT_INTERVAL_MS);
74        }
75        const {
76            assert!(RECONNECT_GRACE_MS > HEARTBEAT_TIMEOUT_MS);
77        }
78    }
79}