mesh_node/local_node/
protocol.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Protocol definitions for sending events to remote nodes.
5
6use bitfield_struct::bitfield;
7use zerocopy::FromBytes;
8use zerocopy::Immutable;
9use zerocopy::IntoBytes;
10use zerocopy::KnownLayout;
11
12#[repr(C)]
13#[derive(Copy, Clone, IntoBytes, Immutable, KnownLayout, FromBytes)]
14pub struct Uuid([u8; 16]);
15
16impl Uuid {
17    pub const ZERO: Self = Self([0; 16]);
18
19    pub fn is_zero(&self) -> bool {
20        self.0 == [0; 16]
21    }
22}
23
24impl From<crate::common::Uuid> for Uuid {
25    fn from(value: crate::common::Uuid) -> Self {
26        Self(value.0)
27    }
28}
29
30impl From<Uuid> for crate::common::Uuid {
31    fn from(value: Uuid) -> Self {
32        Self(value.0)
33    }
34}
35
36#[repr(C)]
37#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
38pub struct Event {
39    pub port_id: Uuid,
40    pub event_type: EventType,
41    pub flags: EventFlags,
42    pub reserved: [u8; 6],
43    pub seq: u64,
44    pub resource_count: u32,
45    pub message_size: u32,
46}
47
48open_enum::open_enum! {
49    #[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
50    pub enum EventType: u8 {
51        MESSAGE = 1,
52        CHANGE_PEER = 3,
53        ACKNOWLEDGE_CHANGE_PEER = 4,
54        ACKNOWLEDGE_PORT = 5,
55        FAIL_PORT = 6,
56    }
57}
58
59#[bitfield(u8)]
60#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
61pub struct EventFlags {
62    /// The event contains a port message.
63    pub message: bool,
64    /// The port is closing.
65    pub close: bool,
66    #[bits(6)]
67    _reserved: u8,
68}
69
70#[repr(C)]
71#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
72pub struct ChangePeerData {
73    pub node: Uuid,
74    pub port: Uuid,
75    pub seq_delta: u64,
76    pub reserved: u64,
77}
78
79#[repr(C)]
80#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
81pub struct FailPortData {
82    pub node: Uuid,
83}
84
85#[repr(C)]
86#[derive(Copy, Clone, IntoBytes, Immutable, KnownLayout, FromBytes)]
87pub struct ResourceData {
88    /// if zero, this is a file descriptor/handle
89    pub id: Uuid,
90    pub next_local_seq: u64,
91    pub reserved: u64,
92    pub old_node: Uuid,
93    pub old_port: Uuid,
94    /// if peer_port is zero, this is the node that caused the port to fail
95    pub peer_node: Uuid,
96    /// if zero, the port is failed
97    pub peer_port: Uuid,
98}