1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use std::fmt::{Display, Formatter, Result};

/// 节点Id
/// 8位最多256个节点
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct NodeId(u8);

impl Display for NodeId {
    fn fmt(&self, f: &mut Formatter) -> Result {
        write!(f, "NID({})", self.0)
    }
}

impl NodeId {
    #[inline]
    pub fn from_u8(id: u8) -> Self {
        Self(id)
    }

    #[inline]
    pub fn to_u8(&self) -> u8 {
        self.0
    }
}

/// 全局服务Id
/// 前面1字节是节点Id,后面3个字节是本地服务Id
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct ServiceId(u32);

impl Display for ServiceId {
    fn fmt(&self, f: &mut Formatter) -> Result {
        write!(f, "SID({}:{})", self.node_id().0, self.local_service_id().0)
    }
}

impl ServiceId {
    #[inline]
    pub fn from_u32(id: u32) -> Self {
        Self(id)
    }

    #[inline]
    pub fn to_u32(&self) -> u32 {
        self.0
    }
}

/// 本地24位服务Id
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct LocalServiceId(u32);

impl LocalServiceId {
    #[inline]
    pub fn from_u32(id: u32) -> Self {
        Self(id)
    }

    #[inline]
    pub fn to_u32(&self) -> u32 {
        self.0
    }

    /// 转换为全局服务id
    #[inline]
    pub fn to_global(self, node_id: NodeId) -> ServiceId {
        ServiceId((node_id.0 as u32) << 24 | (self.0) as u32)
    }
}

impl Display for LocalServiceId {
    fn fmt(&self, f: &mut Formatter) -> Result {
        write!(f, "LID({})", self.0)
    }
}

impl ServiceId {
    /// 获取节点Id
    #[inline]
    pub fn node_id(&self) -> NodeId {
        NodeId((self.0 >> 24) as u8)
    }

    /// 获取本地服务id
    #[inline]
    pub fn local_service_id(&self) -> LocalServiceId {
        LocalServiceId(self.0 & 0xffffff)
    }
}