pub struct ChatMessage {
pub origin_node: u32,
pub timestamp: u64,
pub is_broadcast: bool,
pub requires_ack: bool,
pub reply_to_node: u32,
pub reply_to_timestamp: u64,
/* private fields */
}Expand description
A single chat message in the mesh
Messages are uniquely identified by (origin_node, timestamp).
This allows deduplication across mesh sync while preserving message ordering.
Fields§
§origin_node: u32Node that originated this message
timestamp: u64Timestamp when message was created (ms since epoch)
is_broadcast: boolWhether this is a broadcast message (vs directed)
requires_ack: boolWhether ACK is requested
reply_to_node: u32Reply-to: origin node of the message being replied to (0 = not a reply)
reply_to_timestamp: u64Reply-to: timestamp of the message being replied to (0 = not a reply)
Implementations§
Source§impl ChatMessage
impl ChatMessage
Sourcepub fn new(origin_node: u32, timestamp: u64, sender: &str, text: &str) -> Self
pub fn new(origin_node: u32, timestamp: u64, sender: &str, text: &str) -> Self
Create a new chat message
Sourcepub fn set_sender(&mut self, sender: &str)
pub fn set_sender(&mut self, sender: &str)
Set the sender name (truncated to 12 bytes)
Sourcepub fn set_reply_to(&mut self, node: u32, timestamp: u64)
pub fn set_reply_to(&mut self, node: u32, timestamp: u64)
Set reply-to information
Sourcepub fn message_id(&self) -> u64
pub fn message_id(&self) -> u64
Get the unique message ID (combines origin_node and timestamp)
Format: (origin_node as u64) << 32 | (timestamp & 0xFFFFFFFF)
This provides a sortable key where messages from same node are ordered by time.
Sourcepub fn encode(&self) -> Vec<u8> ⓘ
pub fn encode(&self) -> Vec<u8> ⓘ
Encode to bytes for transmission
Wire format:
origin_node: 4 bytes (LE)
timestamp: 8 bytes (LE)
sender_len: 1 byte
sender: sender_len bytes
text_len: 1 byte
text: text_len bytes
flags: 1 byte (bit 0: is_broadcast, bit 1: requires_ack)
reply_to_node: 4 bytes (LE)
reply_to_timestamp: 8 bytes (LE)Sourcepub fn decode(data: &[u8]) -> Option<(Self, usize)>
pub fn decode(data: &[u8]) -> Option<(Self, usize)>
Decode from bytes with strict validation
Returns None if the data is malformed or fails validation checks.
This is a security-critical function - malformed messages could be
spoofed or part of an attack.
§Validation checks:
- origin_node must be non-zero
- timestamp must be non-zero and within reasonable bounds
- sender must be non-empty and valid UTF-8
- text must be valid UTF-8 (can be empty for ACK messages)
- All length fields must be within bounds
Trait Implementations§
Source§impl Clone for ChatMessage
impl Clone for ChatMessage
Source§fn clone(&self) -> ChatMessage
fn clone(&self) -> ChatMessage
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more