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/// System codes for protosocket rpc framework features.
21#[derive(Debug, Clone, Copy)]
22#[repr(u8)]
23pub enum ProtosocketControlCode {
24 /// No special behavior
25 Normal = 0,
26 /// Cancel processing the message with this message's id
27 Cancel = 1,
28 /// End processing the message with this message's id - for response streaming
29 End = 2,
30}
31
32impl ProtosocketControlCode {
33 /// Read a control code from a u8
34 pub fn from_u8(value: u8) -> Self {
35 match value {
36 0 => Self::Normal,
37 1 => Self::Cancel,
38 2 => Self::End,
39 _ => Self::Cancel,
40 }
41 }
42
43 /// Write a control code as a u8
44 pub fn as_u8(&self) -> u8 {
45 match self {
46 Self::Normal => 0,
47 Self::Cancel => 1,
48 Self::End => 2,
49 }
50 }
51}