Skip to content

Commit baa95e5

Browse files
committed
Use ping protocol to prevent connection timeout
1 parent 67305be commit baa95e5

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/server.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use std::net::IpAddr;
22
use std::net::SocketAddr;
33

4+
use futures::Stream;
45
use futures::StreamExt;
56
use litep2p::config::ConfigBuilder;
67
use litep2p::crypto::ed25519::SecretKey;
8+
use litep2p::protocol::libp2p::ping::Config as PingConfig;
9+
use litep2p::protocol::libp2p::ping::PingEvent;
710
use litep2p::protocol::request_response::Config as RequestResponseConfig;
811
use litep2p::protocol::request_response::ConfigBuilder as RequestResponseConfigBuilder;
912
use litep2p::protocol::request_response::RequestResponseEvent;
@@ -16,18 +19,21 @@ use litep2p::Litep2pEvent;
1619
#[derive(Debug)]
1720
pub enum P2pServerEvent {
1821
Litep2p(Litep2pEvent),
22+
PingEvent(PingEvent),
1923
ControlEvent(RequestResponseEvent),
2024
TunnelEvent(RequestResponseEvent),
2125
}
2226

2327
pub struct P2pServer {
2428
pub(crate) litep2p: Litep2p,
29+
pub(crate) ping_stream: Box<dyn Stream<Item = PingEvent> + Send + Unpin>,
2530
pub(crate) control_handle: RequestResponseHandle,
2631
pub(crate) tunnel_handle: RequestResponseHandle,
2732
}
2833

2934
impl P2pServer {
3035
pub fn new(secret_key: SecretKey, server_addr: SocketAddr) -> Self {
36+
let (ping_config, ping_stream) = PingConfig::default();
3137
let (control_config, control_handle) = Self::init_control();
3238
let (tunnel_config, tunnel_handle) = Self::init_tunnel();
3339

@@ -42,6 +48,7 @@ impl P2pServer {
4248
listen_addresses: vec![format!("/{ip_type}/{ip}/tcp/{port}").parse().unwrap()],
4349
..Default::default()
4450
})
51+
.with_libp2p_ping(ping_config)
4552
.with_request_response_protocol(control_config)
4653
.with_request_response_protocol(tunnel_config)
4754
.build();
@@ -52,6 +59,7 @@ impl P2pServer {
5259

5360
Self {
5461
litep2p,
62+
ping_stream,
5563
control_handle,
5664
tunnel_handle,
5765
}
@@ -77,6 +85,9 @@ impl P2pServer {
7785
ev = self.litep2p.next_event() => {
7886
ev.map(P2pServerEvent::Litep2p)
7987
}
88+
ev = self.ping_stream.next() => {
89+
ev.map(P2pServerEvent::PingEvent)
90+
}
8091
ev = self.control_handle.next() => {
8192
ev.map(P2pServerEvent::ControlEvent)
8293
}

0 commit comments

Comments
 (0)