Skip to main content

hoy_protocol/
packet.rs

1//! Protocol packet definitions.
2
3use serde::{Deserialize, Serialize};
4
5/// Client-to-server protocol packets.
6#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
7pub enum ClientPacket {
8    /// Initial handshake packet sent by client after connecting.
9    Hello {
10        /// Client username.
11        username: String,
12    },
13
14    /// Chat message sent by a client.
15    SendMessage {
16        /// Message text
17        text: String,
18    },
19
20    /// Heartbeat ping.
21    Ping,
22
23    /// Request to join or create a room.
24    JoinRoom {
25        /// Name of the room client is requesting to join.
26        room: String,
27    },
28
29    /// Request the list of all known rooms.
30    ListRooms,
31}
32
33/// Server-to-client protocol packets.
34#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
35pub enum ServerPacket {
36    /// Successful handshake response.
37    Welcome {
38        /// Accepted client username.
39        username: String,
40        /// Joined room.
41        room: String,
42    },
43
44    /// Chat message broadcast by the server.
45    ChatMessage {
46        /// Message sender username.
47        from: String,
48
49        /// Target room name.
50        room: String,
51
52        /// Message text.
53        text: String,
54    },
55
56    /// System generated message.
57    SystemMessage {
58        /// System message text.
59        text: String,
60    },
61
62    /// Error reported by the server.
63    Error {
64        /// Error description.
65        message: String,
66    },
67
68    /// Heartbeat ping response.
69    Pong,
70
71    /// Confirms the client had successfully joined a room.
72    RoomJoined {
73        /// Name of the room the client had joined.
74        room: String,
75        /// Message history..
76        messages: Vec<MessageRecord>,
77    },
78
79    /// Response to `ListRooms`,
80    RoomList {
81        /// List of available rooms.
82        rooms: Vec<String>,
83    },
84}
85
86/// Message record for serialized packets.
87#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
88pub struct MessageRecord {
89    /// Sender display name.
90    pub from: String,
91    /// Message text.
92    pub text: String,
93}