Skip to content

Commit 70819df

Browse files
authored
transport: Introduce common listener for tcp and websocket (#147)
This PR aims to remove duplicated code for socket listener between TCP and WebSocket transports. Changes done: - Extended code logic to facilitate both TCP and WebSockets - Moved the listener code to a dedicated `common` module - Provided fairness for polling the listener stream by introducing a rotating index - Traits to convert between multiaddr <-> socket addr - Removed unnecessary cloning of listen addresses Part of #70. --------- Signed-off-by: Alexandru Vasile <[email protected]>
1 parent ec48b04 commit 70819df

File tree

9 files changed

+376
-596
lines changed

9 files changed

+376
-596
lines changed

src/transport/websocket/listener.rs renamed to src/transport/common/listener.rs

+335-144
Large diffs are not rendered by default.

src/transport/common/mod.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2024 litep2p developers
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the "Software"),
5+
// to deal in the Software without restriction, including without limitation
6+
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7+
// and/or sell copies of the Software, and to permit persons to whom the
8+
// Software is furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19+
// DEALINGS IN THE SOFTWARE.
20+
21+
//! Shared transport protocol implementation
22+
23+
pub mod listener;

src/transport/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use multiaddr::Multiaddr;
2727

2828
use std::{fmt::Debug, time::Duration};
2929

30+
pub(crate) mod common;
3031
pub mod quic;
3132
pub mod tcp;
3233
pub mod webrtc;

src/transport/tcp/config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ pub struct Config {
6565
/// By default the value is set to `2` which means that the `NoiseSocket` will allocate
6666
/// `130 KB` for each outgoing connection.
6767
///
68-
/// The write buffer size is separate from the read-ahead frame count so by default
68+
/// The write buffer size is separate from the read-ahead frame count so by default
6969
/// the Noise code will allocate `2 * 65 KB + 5 * 65 KB = 455 KB` per connection.
7070
pub noise_write_buffer_size: usize,
7171

7272
/// Connection open timeout.
7373
///
74-
/// How long should litep2p wait for a connection to be opend before the host
74+
/// How long should litep2p wait for a connection to be opened before the host
7575
/// is deemed unreachable.
7676
pub connection_open_timeout: std::time::Duration,
7777

src/transport/tcp/connection.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ use crate::{
2828
multistream_select::{dialer_select_proto, listener_select_proto, Negotiated, Version},
2929
protocol::{Direction, Permit, ProtocolCommand, ProtocolSet},
3030
substream,
31-
transport::{
32-
tcp::{listener::AddressType, substream::Substream},
33-
Endpoint,
34-
},
31+
transport::{common::listener::AddressType, tcp::substream::Substream, Endpoint},
3532
types::{protocol::ProtocolName, ConnectionId, SubstreamId},
3633
BandwidthSink, PeerId,
3734
};

0 commit comments

Comments
 (0)