karyon_p2p/discovery/kademlia/
messages.rs1use bincode::{Decode, Encode};
9
10use karyon_net::{
11 codec::{Codec, LengthCodec},
12 ByteBuffer,
13};
14
15use crate::{
16 bloom::Bloom,
17 message::PeerAddr,
18 util::{decode, encode},
19 version::VersionInt,
20 PeerID, Result,
21};
22
23#[derive(Decode, Encode, Debug, Clone)]
28pub struct KadNetMsg {
29 pub header: KadNetMsgHeader,
30 pub payload: Vec<u8>,
31}
32
33impl KadNetMsg {
34 pub fn new<T: Encode>(command: KadNetCmd, t: T) -> Result<Self> {
35 Ok(Self {
36 header: KadNetMsgHeader { command },
37 payload: encode(&t)?,
38 })
39 }
40}
41
42#[derive(Decode, Encode, Debug, Clone)]
43pub struct KadNetMsgHeader {
44 pub command: KadNetCmd,
45}
46
47#[derive(Decode, Encode, Debug, Clone)]
49#[repr(u8)]
50pub enum KadNetCmd {
51 Ping,
53 Pong,
55 FindPeer,
57 Peer,
59 Peers,
61 Shutdown,
63}
64
65#[derive(Clone)]
67pub struct KadNetMsgCodec {
68 inner_codec: LengthCodec,
69}
70
71impl KadNetMsgCodec {
72 pub fn new() -> Self {
73 Self {
74 inner_codec: LengthCodec::default(),
75 }
76 }
77}
78
79impl Default for KadNetMsgCodec {
80 fn default() -> Self {
81 Self::new()
82 }
83}
84
85impl Codec<ByteBuffer> for KadNetMsgCodec {
86 type Message = KadNetMsg;
87 type Error = karyon_net::Error;
88
89 fn encode(
90 &self,
91 src: &KadNetMsg,
92 dst: &mut ByteBuffer,
93 ) -> std::result::Result<usize, karyon_net::Error> {
94 let src = encode(src).map_err(|e| karyon_net::Error::IO(std::io::Error::other(e)))?;
95 self.inner_codec.encode(&src, dst)
96 }
97
98 fn decode(
99 &self,
100 src: &mut ByteBuffer,
101 ) -> std::result::Result<Option<(usize, KadNetMsg)>, karyon_net::Error> {
102 match self.inner_codec.decode(src)? {
103 Some((n, s)) => {
104 let (m, _) = decode::<KadNetMsg>(&s)
105 .map_err(|e| karyon_net::Error::IO(std::io::Error::other(e)))?;
106 Ok(Some((n, m)))
107 }
108 None => Ok(None),
109 }
110 }
111}
112
113#[derive(Decode, Encode, Debug, Clone)]
117pub struct PingMsg {
118 pub nonce: [u8; 32],
119 pub version: VersionInt,
120}
121
122#[derive(Decode, Encode, Debug)]
124pub struct PongMsg(pub [u8; 32]);
125
126#[derive(Decode, Encode, Debug)]
128pub struct FindPeerMsg(pub PeerID);
129
130#[derive(Decode, Encode, Debug, Clone, PartialEq, Eq)]
132pub struct PeerMsg {
133 pub peer_id: PeerID,
134 pub addrs: Vec<PeerAddr>,
135 pub discovery_addrs: Vec<PeerAddr>,
136 pub protocols: Bloom,
137}
138
139#[derive(Decode, Encode, Debug)]
141pub struct PeersMsg {
142 pub peers: Vec<PeerMsg>,
143}
144
145#[derive(Decode, Encode, Debug, Clone)]
150pub enum RefreshMsg {
151 Ping([u8; 32]),
152 Pong([u8; 32]),
153}