protosocket_rpc/
message.rs

1/// A protosocket message.
2pub trait Message: std::fmt::Debug + Send + Unpin + 'static {
3    /// This is used to relate requests to responses. An RPC response has the same id as the request that generated it.
4    fn message_id(&self) -> u64;
5
6    /// Set the protosocket behavior of this message.
7    fn control_code(&self) -> ProtosocketControlCode;
8
9    /// This is used to relate requests to responses. An RPC response has the same id as the request that generated it.
10    /// When the message is sent, protosocket will set this value.
11    fn set_message_id(&mut self, message_id: u64);
12
13    /// Create a message with a message with a cancel control code - used by the framework to handle cancellation.
14    fn cancelled(message_id: u64) -> Self;
15
16    /// Create a message with a message with an ended control code - used by the framework to handle streaming completion.
17    fn ended(message_id: u64) -> Self;
18}
19
20#[derive(Debug, Clone, Copy)]
21#[repr(u8)]
22pub enum ProtosocketControlCode {
23    /// No special behavior
24    Normal = 0,
25    /// Cancel processing the message with this message's id
26    Cancel = 1,
27    /// End processing the message with this message's id - for response streaming
28    End = 2,
29}
30
31impl ProtosocketControlCode {
32    pub fn from_u8(value: u8) -> Self {
33        match value {
34            0 => Self::Normal,
35            1 => Self::Cancel,
36            2 => Self::End,
37            _ => Self::Cancel,
38        }
39    }
40
41    pub fn as_u8(&self) -> u8 {
42        match self {
43            Self::Normal => 0,
44            Self::Cancel => 1,
45            Self::End => 2,
46        }
47    }
48}