From e15d3e6fd20b3f87abaad7ddec1c88b0e66419f9 Mon Sep 17 00:00:00 2001 From: hozan23 Date: Mon, 15 Jul 2024 13:16:01 +0200 Subject: p2p: Major refactoring of the handshake protocol Introduce a new protocol InitProtocol which can be used as the core protocol for initializing a connection with a peer. Move the handshake logic from the PeerPool module to the protocols directory and build a handshake protocol that implements InitProtocol trait. --- p2p/src/protocol.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'p2p/src/protocol.rs') diff --git a/p2p/src/protocol.rs b/p2p/src/protocol.rs index 021844f..249692b 100644 --- a/p2p/src/protocol.rs +++ b/p2p/src/protocol.rs @@ -56,11 +56,8 @@ impl EventValue for ProtocolEvent { /// #[async_trait] /// impl Protocol for NewProtocol { /// async fn start(self: Arc) -> Result<(), Error> { -/// let listener = self.peer.register_listener::().await; /// loop { -/// let event = listener.recv().await.unwrap(); -/// -/// match event { +/// match self.peer.recv::().await.expect("Receive msg") { /// ProtocolEvent::Message(msg) => { /// println!("{:?}", msg); /// } @@ -69,8 +66,6 @@ impl EventValue for ProtocolEvent { /// } /// } /// } -/// -/// listener.cancel().await; /// Ok(()) /// } /// @@ -114,3 +109,10 @@ pub trait Protocol: Send + Sync { where Self: Sized; } + +#[async_trait] +pub(crate) trait InitProtocol: Send + Sync { + type T; + /// Initialize the protocol + async fn init(self: Arc) -> Self::T; +} -- cgit v1.2.3