1use crate::{error::DialError, transport::manager::TransportHandle, types::ConnectionId, PeerId};
24
25use futures::Stream;
26use hickory_resolver::TokioResolver;
27use multiaddr::Multiaddr;
28
29use std::{fmt::Debug, sync::Arc, time::Duration};
30
31pub(crate) mod common;
32#[cfg(feature = "quic")]
33pub mod quic;
34pub mod tcp;
35#[cfg(feature = "webrtc")]
36pub mod webrtc;
37#[cfg(feature = "websocket")]
38pub mod websocket;
39
40#[cfg(test)]
41pub(crate) mod dummy;
42
43pub(crate) mod manager;
44
45pub use manager::limits::{ConnectionLimitsConfig, ConnectionLimitsError};
46
47pub(crate) const CONNECTION_OPEN_TIMEOUT: Duration = Duration::from_secs(10);
49
50pub(crate) const SUBSTREAM_OPEN_TIMEOUT: Duration = Duration::from_secs(5);
52
53pub(crate) const KEEP_ALIVE_TIMEOUT: Duration = Duration::from_secs(5);
55
56pub(crate) const MAX_PARALLEL_DIALS: usize = 8;
58
59#[derive(Debug, Clone, PartialEq, Eq)]
61pub enum Endpoint {
62 Dialer {
64 address: Multiaddr,
66
67 connection_id: ConnectionId,
69 },
70
71 Listener {
73 address: Multiaddr,
75
76 connection_id: ConnectionId,
78 },
79}
80
81impl Endpoint {
82 pub fn address(&self) -> &Multiaddr {
84 match self {
85 Self::Dialer { address, .. } => address,
86 Self::Listener { address, .. } => address,
87 }
88 }
89
90 pub(crate) fn dialer(address: Multiaddr, connection_id: ConnectionId) -> Self {
92 Endpoint::Dialer {
93 address,
94 connection_id,
95 }
96 }
97
98 pub(crate) fn listener(address: Multiaddr, connection_id: ConnectionId) -> Self {
100 Endpoint::Listener {
101 address,
102 connection_id,
103 }
104 }
105
106 pub fn connection_id(&self) -> ConnectionId {
108 match self {
109 Self::Dialer { connection_id, .. } => *connection_id,
110 Self::Listener { connection_id, .. } => *connection_id,
111 }
112 }
113
114 pub fn is_listener(&self) -> bool {
116 std::matches!(self, Self::Listener { .. })
117 }
118}
119
120#[derive(Debug)]
122pub(crate) enum TransportEvent {
123 ConnectionEstablished {
125 peer: PeerId,
127
128 endpoint: Endpoint,
130 },
131
132 PendingInboundConnection {
133 connection_id: ConnectionId,
135 },
136
137 ConnectionOpened {
139 connection_id: ConnectionId,
141
142 address: Multiaddr,
144 },
145
146 #[allow(unused)]
148 ConnectionClosed {
149 peer: PeerId,
151
152 connection_id: ConnectionId,
154 },
155
156 DialFailure {
158 connection_id: ConnectionId,
160
161 address: Multiaddr,
163
164 error: DialError,
166 },
167
168 OpenFailure {
170 connection_id: ConnectionId,
172
173 errors: Vec<(Multiaddr, DialError)>,
175 },
176}
177
178pub(crate) trait TransportBuilder {
179 type Config: Debug;
180 type Transport: Transport;
181
182 fn new(
184 context: TransportHandle,
185 config: Self::Config,
186 resolver: Arc<TokioResolver>,
187 ) -> crate::Result<(Self, Vec<Multiaddr>)>
188 where
189 Self: Sized;
190}
191
192pub(crate) trait Transport: Stream + Unpin + Send {
193 fn dial(&mut self, connection_id: ConnectionId, address: Multiaddr) -> crate::Result<()>;
195
196 fn accept(&mut self, connection_id: ConnectionId) -> crate::Result<()>;
198
199 fn accept_pending(&mut self, connection_id: ConnectionId) -> crate::Result<()>;
201
202 fn reject_pending(&mut self, connection_id: ConnectionId) -> crate::Result<()>;
204
205 fn reject(&mut self, connection_id: ConnectionId) -> crate::Result<()>;
207
208 fn open(&mut self, connection_id: ConnectionId, addresses: Vec<Multiaddr>)
210 -> crate::Result<()>;
211
212 fn negotiate(&mut self, connection_id: ConnectionId) -> crate::Result<()>;
214
215 fn cancel(&mut self, connection_id: ConnectionId);
219}