|
20 | 20 |
|
21 | 21 | use anyhow::{Context, Result}; |
22 | 22 | use futures::stream::StreamExt; |
23 | | -use libp2p::swarm::dial_opts::DialOpts; |
24 | | -use libp2p::swarm::ConnectionId; |
25 | 23 | use libp2p::{ |
26 | 24 | core::multiaddr::{Multiaddr, Protocol}, |
27 | 25 | dcutr, identify, noise, ping, relay, |
@@ -85,10 +83,6 @@ async fn main() -> Result<()> { |
85 | 83 | .build(); |
86 | 84 |
|
87 | 85 | client_listen_on_transport(&mut swarm, transport).await?; |
88 | | - let relay_conn_id = client_connect_to_relay(&mut swarm, relay_addr.clone()) |
89 | | - .await |
90 | | - .context("Failed to connect to relay")?; |
91 | | - |
92 | 86 | client_setup(&mut swarm, &mut redis, relay_addr.clone(), mode).await?; |
93 | 87 |
|
94 | 88 | let mut hole_punched_peer_connection = None; |
@@ -118,8 +112,6 @@ async fn main() -> Result<()> { |
118 | 112 | ) => { |
119 | 113 | log::info!("Successfully hole-punched to {remote_peer_id}"); |
120 | 114 |
|
121 | | - // Closing the connection to the relay will implicitly close the relayed connection to the other peer. |
122 | | - swarm.close_connection(relay_conn_id); |
123 | 115 | hole_punched_peer_connection = Some(connection_id); |
124 | 116 | } |
125 | 117 | ( |
@@ -150,19 +142,6 @@ async fn main() -> Result<()> { |
150 | 142 | (SwarmEvent::OutgoingConnectionError { error, .. }, _) => { |
151 | 143 | anyhow::bail!(error) |
152 | 144 | } |
153 | | - ( |
154 | | - SwarmEvent::ConnectionClosed { |
155 | | - connection_id, |
156 | | - cause: Some(error), |
157 | | - .. |
158 | | - }, |
159 | | - _, |
160 | | - ) if connection_id == relay_conn_id => { |
161 | | - log::warn!("Connection to relay failed: {error}"); |
162 | | - |
163 | | - // TODO: Re-connecting is a bit of a hack, we should figure out why the connection sometimes fails. |
164 | | - client_setup(&mut swarm, &mut redis, relay_addr.clone(), mode).await?; |
165 | | - } |
166 | 145 | _ => {} |
167 | 146 | } |
168 | 147 | } |
@@ -194,40 +173,6 @@ where |
194 | 173 | Ok(val) |
195 | 174 | } |
196 | 175 |
|
197 | | -async fn client_connect_to_relay( |
198 | | - swarm: &mut Swarm<Behaviour>, |
199 | | - relay_addr: Multiaddr, |
200 | | -) -> Result<ConnectionId> { |
201 | | - let opts = DialOpts::from(relay_addr); |
202 | | - let relay_connection_id = opts.connection_id(); |
203 | | - |
204 | | - // Connect to the relay server. |
205 | | - swarm.dial(opts)?; |
206 | | - |
207 | | - loop { |
208 | | - match swarm.next().await.unwrap() { |
209 | | - SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Received { |
210 | | - info: identify::Info { observed_addr, .. }, |
211 | | - .. |
212 | | - })) => { |
213 | | - log::info!("Relay told us our public address: {observed_addr}"); |
214 | | - break; |
215 | | - } |
216 | | - SwarmEvent::ConnectionEstablished { connection_id, .. } |
217 | | - if connection_id == relay_connection_id => |
218 | | - { |
219 | | - log::info!("Connected to the relay"); |
220 | | - } |
221 | | - SwarmEvent::OutgoingConnectionError { error, .. } => { |
222 | | - anyhow::bail!(error) |
223 | | - } |
224 | | - _ => {} |
225 | | - } |
226 | | - } |
227 | | - |
228 | | - Ok(relay_connection_id) |
229 | | -} |
230 | | - |
231 | 176 | async fn client_listen_on_transport( |
232 | 177 | swarm: &mut Swarm<Behaviour>, |
233 | 178 | transport: TransportProtocol, |
|
0 commit comments