Skip to main content

orlando_cluster/
network_message.rs

1use orlando_core::Message;
2use serde::{de::DeserializeOwned, Serialize};
3
4/// Payload encoding for grain messages on the wire.
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum Encoding {
7    /// Rust-native binary encoding. Fast, compact, Rust-only.
8    /// Used for silo-to-silo internal traffic.
9    Bincode,
10    /// Protocol Buffers encoding. Cross-language.
11    /// Used by external (non-Rust) clients.
12    Protobuf,
13}
14
15impl Encoding {
16    pub fn from_proto(val: i32) -> Self {
17        match val {
18            1 => Self::Protobuf,
19            _ => Self::Bincode,
20        }
21    }
22
23    pub fn to_proto(self) -> i32 {
24        match self {
25            Self::Bincode => 0,
26            Self::Protobuf => 1,
27        }
28    }
29}
30
31/// A message that can be sent across silo boundaries.
32///
33/// Extends `Message` with serialization support and a stable type name
34/// used for registry-based dispatch on the receiving silo.
35///
36/// By default, messages are serialized with bincode (serde). To support
37/// external (non-Rust) clients, implement the `encode_proto`/`decode_proto`
38/// methods or use `#[message(result = T, network, proto)]`.
39pub trait NetworkMessage: Message + Serialize + DeserializeOwned
40where
41    Self::Result: Serialize + DeserializeOwned,
42{
43    /// Stable name for this message type (used as the key in the message registry).
44    fn message_type_name() -> &'static str;
45
46    /// Version of this message type. Default 0 (unversioned, backward compatible).
47    /// Increment when the message schema changes in a breaking way.
48    /// During rolling deploys, a silo receiving a message with a version newer
49    /// than the one it supports will return `UnsupportedMessageVersion`.
50    fn message_version() -> u32 {
51        0
52    }
53
54    /// Whether this message supports protobuf encoding for external clients.
55    fn supports_proto() -> bool {
56        false
57    }
58
59    /// Encode this message as protobuf. Returns `None` if not supported.
60    fn encode_proto(&self) -> Option<Vec<u8>> {
61        None
62    }
63
64    /// Decode this message from protobuf bytes. Returns `None` if not supported.
65    fn decode_proto(_bytes: &[u8]) -> Option<Self> {
66        None
67    }
68
69    /// Encode the result type as protobuf. Returns `None` if not supported.
70    fn encode_result_proto(_result: &Self::Result) -> Option<Vec<u8>> {
71        None
72    }
73
74    /// Decode the result type from protobuf bytes. Returns `None` if not supported.
75    fn decode_result_proto(_bytes: &[u8]) -> Option<Self::Result> {
76        None
77    }
78}