Skip to content

Commit 04c2d7a

Browse files
committed
feat: replace sleep with notifier for tunnel open
1 parent 9a7e185 commit 04c2d7a

File tree

4 files changed

+54
-19
lines changed

4 files changed

+54
-19
lines changed

src/command.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use tonic::Request;
22
use tonic::Response;
33
use tonic::Status;
4-
use tracing::trace;
54

65
use crate::PProxyHandle;
76

@@ -29,7 +28,7 @@ impl proto::command_service_server::CommandService for PProxyCommander {
2928
&self,
3029
request: Request<proto::AddPeerRequest>,
3130
) -> Result<Response<proto::AddPeerResponse>, Status> {
32-
trace!("handle request: {:?}", request);
31+
tracing::trace!("handle request: {:?}", request);
3332

3433
self.handle
3534
.add_peer(request.into_inner())
@@ -43,7 +42,7 @@ impl proto::command_service_server::CommandService for PProxyCommander {
4342
request: tonic::Request<proto::CreateTunnelServerRequest>,
4443
) -> std::result::Result<tonic::Response<proto::CreateTunnelServerResponse>, tonic::Status>
4544
{
46-
trace!("handle request: {:?}", request);
45+
tracing::trace!("handle request: {:?}", request);
4746

4847
self.handle
4948
.create_tunnel_server(request.into_inner())

src/error.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,12 @@ pub enum Error {
1919
Litep2p(#[from] litep2p::Error),
2020
#[error("Litep2p request response error: {0:?}")]
2121
Litep2pRequestResponseError(litep2p::protocol::request_response::RequestResponseError),
22-
#[error("Httparse error: {0}")]
23-
Httparse(#[from] httparse::Error),
24-
#[error("Incomplete http request")]
25-
IncompleteHttpRequest,
2622
#[error("Protocol not support: {0}")]
2723
ProtocolNotSupport(String),
28-
#[error("Io error: {0}")]
29-
Io(#[from] std::io::Error),
3024
#[error("Unexpected response type")]
3125
UnexpectedResponseType,
26+
#[error("Tunnel not waiting")]
27+
TunnelNotWaiting(String),
3228
#[error("Tunnel error: {0:?}")]
3329
Tunnel(TunnelError),
3430
#[error("Protobuf decode error: {0}")]

src/lib.rs

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ use futures::channel::oneshot;
88
use litep2p::crypto::ed25519::SecretKey;
99
use litep2p::protocol::request_response::DialOptions;
1010
use litep2p::protocol::request_response::RequestResponseEvent;
11+
use litep2p::types::RequestId;
1112
use litep2p::PeerId;
1213
use multiaddr::Multiaddr;
1314
use multiaddr::Protocol;
1415
use prost::Message;
1516
use tokio::sync::mpsc;
16-
use tracing::warn;
1717

1818
use crate::command::proto::AddPeerRequest;
1919
use crate::command::proto::AddPeerResponse;
@@ -77,6 +77,7 @@ pub struct PProxy {
7777
command_rx: mpsc::Receiver<(PProxyCommand, CommandNotifier)>,
7878
p2p_server: P2pServer,
7979
proxy_addr: Option<SocketAddr>,
80+
outbound_ready_notifiers: HashMap<RequestId, CommandNotifier>,
8081
inbound_tunnels: HashMap<(PeerId, TunnelId), Tunnel>,
8182
tunnel_txs: HashMap<(PeerId, TunnelId), mpsc::Sender<Vec<u8>>>,
8283
}
@@ -118,6 +119,7 @@ impl PProxy {
118119
command_rx,
119120
p2p_server: P2pServer::new(secret_key, server_addr),
120121
proxy_addr,
122+
outbound_ready_notifiers: HashMap::new(),
121123
inbound_tunnels: HashMap::new(),
122124
tunnel_txs: HashMap::new(),
123125
},
@@ -138,14 +140,14 @@ impl PProxy {
138140
event = self.p2p_server.next_event() => match event {
139141
None => return,
140142
Some(event) => if let Err(error) = self.handle_p2p_server_event(event).await {
141-
warn!("failed to handle event: {:?}", error);
143+
tracing::warn!("failed to handle event: {:?}", error);
142144
}
143145
},
144146

145147
command = self.command_rx.recv() => match command {
146148
None => return,
147149
Some((command, tx)) => if let Err(error) = self.handle_command(command, tx).await {
148-
warn!("failed to handle command: {:?}", error);
150+
tracing::warn!("failed to handle command: {:?}", error);
149151
}
150152
}
151153
}
@@ -166,7 +168,7 @@ impl PProxy {
166168
..
167169
}) => {
168170
let msg = proto::Tunnel::decode(request.as_slice())?;
169-
tracing::debug!("received Tunnel msg: {:?}", msg);
171+
tracing::debug!("received Tunnel request msg: {:?}", msg);
170172

171173
match msg.command() {
172174
proto::TunnelCommand::Connect => {
@@ -221,11 +223,49 @@ impl PProxy {
221223

222224
_ => {
223225
return Err(Error::ProtocolNotSupport(
224-
"Wrong tunnel command".to_string(),
226+
"Wrong tunnel request command".to_string(),
225227
));
226228
}
227229
}
228230
}
231+
P2pServerEvent::TunnelEvent(RequestResponseEvent::ResponseReceived {
232+
peer,
233+
request_id,
234+
response,
235+
..
236+
}) => {
237+
// This is response of TunnelCommand::Package
238+
if response.is_empty() {
239+
return Ok(());
240+
}
241+
242+
let msg = proto::Tunnel::decode(response.as_slice())?;
243+
tracing::debug!("received Tunnel response msg: {:?}", msg);
244+
245+
match msg.command() {
246+
proto::TunnelCommand::ConnectResp => {
247+
let tx = self
248+
.outbound_ready_notifiers
249+
.remove(&request_id)
250+
.ok_or_else(|| {
251+
Error::TunnelNotWaiting(format!(
252+
"peer {}, tunnel {}",
253+
peer, msg.tunnel_id
254+
))
255+
})?;
256+
257+
tx.send(Ok(PProxyCommandResponse::SendConnectCommand {}))
258+
.map_err(|_| Error::EssentialTaskClosed)?;
259+
}
260+
261+
_ => {
262+
return Err(Error::ProtocolNotSupport(
263+
"Wrong tunnel response command".to_string(),
264+
));
265+
}
266+
}
267+
}
268+
229269
_ => {}
230270
}
231271

@@ -286,15 +326,16 @@ impl PProxy {
286326
}
287327
.encode_to_vec();
288328

289-
self.p2p_server
329+
tracing::info!("send connect command to peer_id: {:?}", peer_id);
330+
let request_id = self
331+
.p2p_server
290332
.tunnel_handle
291333
.send_request(peer_id, request, DialOptions::Dial)
292334
.await?;
293335

294-
tracing::info!("send connect command to peer_id: {:?}", peer_id);
336+
self.outbound_ready_notifiers.insert(request_id, tx);
295337

296-
tx.send(Ok(PProxyCommandResponse::SendConnectCommand {}))
297-
.map_err(|_| Error::EssentialTaskClosed)
338+
Ok(())
298339
}
299340

300341
async fn on_send_outbound_package_command(

src/tunnel.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ impl TunnelServerListener {
156156
Ok(Ok(_resp)) => {}
157157
}
158158

159-
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
160159
if let Err(e) = tunnel.listen(stream, tunnel_rx).await {
161160
tracing::error!("Tunnel listen failed: {e:?}");
162161
continue;

0 commit comments

Comments
 (0)