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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use crate::ProtocolId;
use p2p::{
builder::MetaBuilder,
service::{BlockingFlag, ProtocolHandle, ProtocolMeta},
traits::ServiceProtocol,
};
use tokio_util::codec::length_delimited;
#[derive(Clone, Debug)]
pub enum SupportProtocols {
Ping,
Discovery,
Identify,
Feeler,
DisconnectMessage,
Sync,
Relay,
Time,
Alert,
}
impl SupportProtocols {
pub fn protocol_id(&self) -> ProtocolId {
match self {
SupportProtocols::Ping => 0,
SupportProtocols::Discovery => 1,
SupportProtocols::Identify => 2,
SupportProtocols::Feeler => 3,
SupportProtocols::DisconnectMessage => 4,
SupportProtocols::Sync => 100,
SupportProtocols::Relay => 101,
SupportProtocols::Time => 102,
SupportProtocols::Alert => 110,
}
.into()
}
pub fn name(&self) -> String {
match self {
SupportProtocols::Ping => "/ckb/ping",
SupportProtocols::Discovery => "/ckb/discovery",
SupportProtocols::Identify => "/ckb/identify",
SupportProtocols::Feeler => "/ckb/flr",
SupportProtocols::DisconnectMessage => "/ckb/disconnectmsg",
SupportProtocols::Sync => "/ckb/syn",
SupportProtocols::Relay => "/ckb/rel",
SupportProtocols::Time => "/ckb/tim",
SupportProtocols::Alert => "/ckb/alt",
}
.to_owned()
}
pub fn support_versions(&self) -> Vec<String> {
match self {
SupportProtocols::Ping => vec!["0.0.1".to_owned()],
SupportProtocols::Discovery => vec!["0.0.1".to_owned()],
SupportProtocols::Identify => vec!["0.0.1".to_owned()],
SupportProtocols::Feeler => vec!["0.0.1".to_owned()],
SupportProtocols::DisconnectMessage => vec!["0.0.1".to_owned()],
SupportProtocols::Sync => vec!["1".to_owned()],
SupportProtocols::Relay => vec!["1".to_owned()],
SupportProtocols::Time => vec!["1".to_owned()],
SupportProtocols::Alert => vec!["1".to_owned()],
}
}
pub fn max_frame_length(&self) -> usize {
match self {
SupportProtocols::Ping => 1024,
SupportProtocols::Discovery => 512 * 1024,
SupportProtocols::Identify => 2 * 1024,
SupportProtocols::Feeler => 1024,
SupportProtocols::DisconnectMessage => 1024,
SupportProtocols::Sync => 2 * 1024 * 1024,
SupportProtocols::Relay => 4 * 1024 * 1024,
SupportProtocols::Time => 1024,
SupportProtocols::Alert => 128 * 1024,
}
}
pub fn flag(&self) -> BlockingFlag {
match self {
SupportProtocols::Ping
| SupportProtocols::Discovery
| SupportProtocols::Identify
| SupportProtocols::Feeler
| SupportProtocols::DisconnectMessage
| SupportProtocols::Time
| SupportProtocols::Alert => {
let mut no_blocking_flag = BlockingFlag::default();
no_blocking_flag.disable_all();
no_blocking_flag
}
SupportProtocols::Sync | SupportProtocols::Relay => {
let mut blocking_recv_flag = BlockingFlag::default();
blocking_recv_flag.disable_connected();
blocking_recv_flag.disable_disconnected();
blocking_recv_flag.disable_notify();
blocking_recv_flag
}
}
}
pub fn build_meta_with_service_handle<
SH: FnOnce() -> ProtocolHandle<Box<dyn ServiceProtocol + Send + 'static + Unpin>>,
>(
self,
service_handle: SH,
) -> ProtocolMeta {
let meta_builder: MetaBuilder = self.into();
meta_builder.service_handle(service_handle).build()
}
}
impl Into<MetaBuilder> for SupportProtocols {
fn into(self) -> MetaBuilder {
let max_frame_length = self.max_frame_length();
MetaBuilder::default()
.id(self.protocol_id())
.support_versions(self.support_versions())
.flag(self.flag())
.name(move |_| self.name())
.codec(move || {
Box::new(
length_delimited::Builder::new()
.max_frame_length(max_frame_length)
.new_codec(),
)
})
}
}