From 6b9f032afc29798255ce0e58c2fb9caaedeaf068 Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Thu, 24 Jun 2021 20:09:12 +1000 Subject: [PATCH] Keep alive Set to 5 seconds initially using Until. Once we get a substream injected it is set to Yes. Once we are done (in poll) is set to No. --- swap/src/protocol/alice/swap_setup.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/swap/src/protocol/alice/swap_setup.rs b/swap/src/protocol/alice/swap_setup.rs index bf339e16..eb03f017 100644 --- a/swap/src/protocol/alice/swap_setup.rs +++ b/swap/src/protocol/alice/swap_setup.rs @@ -13,6 +13,10 @@ use libp2p::swarm::{ ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr, SubstreamProtocol, }; use libp2p::{Multiaddr, PeerId}; +use serde::de::DeserializeOwned; +use serde::{Deserialize, Serialize}; +use std::future; +use std::time::Instant; use uuid::Uuid; use void::Void; @@ -222,6 +226,7 @@ pub struct Handler { resume_only: bool, timeout: Duration, + keep_alive: KeepAlive, } impl Handler { @@ -241,6 +246,7 @@ impl Handler { latest_rate, resume_only, timeout: Duration::from_secs(60), + keep_alive: KeepAlive::Until(Instant::now() + Duration::from_secs(5)), } } } @@ -271,6 +277,8 @@ where mut substream: NegotiatedSubstream, _: Self::InboundOpenInfo, ) { + self.keep_alive = KeepAlive::Yes; + let (sender, receiver) = bmrng::channel_with_timeout::( 1, Duration::from_secs(5), @@ -435,7 +443,7 @@ where } fn connection_keep_alive(&self) -> KeepAlive { - todo!() + self.keep_alive } fn poll( @@ -450,6 +458,7 @@ where >, > { if let Some(result) = futures::ready!(self.inbound_stream.poll_unpin(cx)) { + self.keep_alive = KeepAlive::No; return Poll::Ready(ProtocolsHandlerEvent::Custom(HandlerOutEvent::Completed( result, )));