use crate::ProtocolId;
use p2p::{
builder::MetaBuilder,
service::{ProtocolHandle, ProtocolMeta},
traits::ServiceProtocol,
};
use tokio_util::codec::length_delimited;
const LASTEST_VERSION: &str = "2";
#[derive(Clone, Debug)]
pub enum SupportProtocols {
Ping,
Discovery,
Identify,
Feeler,
DisconnectMessage,
Sync,
RelayV2,
Time,
Alert,
LightClient,
Filter,
}
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::RelayV2 => 101,
SupportProtocols::Time => 102,
SupportProtocols::Alert => 110,
SupportProtocols::LightClient => 120,
SupportProtocols::Filter => 121,
}
.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::RelayV2 => "/ckb/relay",
SupportProtocols::Time => "/ckb/tim",
SupportProtocols::Alert => "/ckb/alt",
SupportProtocols::LightClient => "/ckb/lightclient",
SupportProtocols::Filter => "/ckb/filter",
}
.to_owned()
}
pub fn support_versions(&self) -> Vec<String> {
match self {
SupportProtocols::Ping => vec![LASTEST_VERSION.to_owned()],
SupportProtocols::Discovery => vec![LASTEST_VERSION.to_owned(), "2.1".to_owned()],
SupportProtocols::Identify => vec![LASTEST_VERSION.to_owned()],
SupportProtocols::Feeler => vec![LASTEST_VERSION.to_owned()],
SupportProtocols::DisconnectMessage => {
vec![LASTEST_VERSION.to_owned()]
}
SupportProtocols::Sync => vec![LASTEST_VERSION.to_owned()],
SupportProtocols::Time => vec![LASTEST_VERSION.to_owned()],
SupportProtocols::Alert => vec![LASTEST_VERSION.to_owned()],
SupportProtocols::RelayV2 => vec![LASTEST_VERSION.to_owned()],
SupportProtocols::LightClient => vec![LASTEST_VERSION.to_owned()],
SupportProtocols::Filter => vec![LASTEST_VERSION.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::RelayV2 => 4 * 1024 * 1024, SupportProtocols::Time => 1024, SupportProtocols::Alert => 128 * 1024, SupportProtocols::LightClient => 2 * 1024 * 1024, SupportProtocols::Filter => 2 * 1024 * 1024, }
}
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 From<SupportProtocols> for MetaBuilder {
fn from(p: SupportProtocols) -> Self {
let max_frame_length = p.max_frame_length();
MetaBuilder::default()
.id(p.protocol_id())
.support_versions(p.support_versions())
.name(move |_| p.name())
.codec(move || {
Box::new(
length_delimited::Builder::new()
.max_frame_length(max_frame_length)
.new_codec(),
)
})
}
}