@@ -8,12 +8,12 @@ use futures::channel::oneshot;
8
8
use litep2p:: crypto:: ed25519:: SecretKey ;
9
9
use litep2p:: protocol:: request_response:: DialOptions ;
10
10
use litep2p:: protocol:: request_response:: RequestResponseEvent ;
11
+ use litep2p:: types:: RequestId ;
11
12
use litep2p:: PeerId ;
12
13
use multiaddr:: Multiaddr ;
13
14
use multiaddr:: Protocol ;
14
15
use prost:: Message ;
15
16
use tokio:: sync:: mpsc;
16
- use tracing:: warn;
17
17
18
18
use crate :: command:: proto:: AddPeerRequest ;
19
19
use crate :: command:: proto:: AddPeerResponse ;
@@ -77,6 +77,7 @@ pub struct PProxy {
77
77
command_rx : mpsc:: Receiver < ( PProxyCommand , CommandNotifier ) > ,
78
78
p2p_server : P2pServer ,
79
79
proxy_addr : Option < SocketAddr > ,
80
+ outbound_ready_notifiers : HashMap < RequestId , CommandNotifier > ,
80
81
inbound_tunnels : HashMap < ( PeerId , TunnelId ) , Tunnel > ,
81
82
tunnel_txs : HashMap < ( PeerId , TunnelId ) , mpsc:: Sender < Vec < u8 > > > ,
82
83
}
@@ -118,6 +119,7 @@ impl PProxy {
118
119
command_rx,
119
120
p2p_server : P2pServer :: new ( secret_key, server_addr) ,
120
121
proxy_addr,
122
+ outbound_ready_notifiers : HashMap :: new ( ) ,
121
123
inbound_tunnels : HashMap :: new ( ) ,
122
124
tunnel_txs : HashMap :: new ( ) ,
123
125
} ,
@@ -138,14 +140,14 @@ impl PProxy {
138
140
event = self . p2p_server. next_event( ) => match event {
139
141
None => return ,
140
142
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) ;
142
144
}
143
145
} ,
144
146
145
147
command = self . command_rx. recv( ) => match command {
146
148
None => return ,
147
149
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) ;
149
151
}
150
152
}
151
153
}
@@ -166,7 +168,7 @@ impl PProxy {
166
168
..
167
169
} ) => {
168
170
let msg = proto:: Tunnel :: decode ( request. as_slice ( ) ) ?;
169
- tracing:: debug!( "received Tunnel msg: {:?}" , msg) ;
171
+ tracing:: debug!( "received Tunnel request msg: {:?}" , msg) ;
170
172
171
173
match msg. command ( ) {
172
174
proto:: TunnelCommand :: Connect => {
@@ -221,11 +223,49 @@ impl PProxy {
221
223
222
224
_ => {
223
225
return Err ( Error :: ProtocolNotSupport (
224
- "Wrong tunnel command" . to_string ( ) ,
226
+ "Wrong tunnel request command" . to_string ( ) ,
225
227
) ) ;
226
228
}
227
229
}
228
230
}
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
+
229
269
_ => { }
230
270
}
231
271
@@ -286,15 +326,16 @@ impl PProxy {
286
326
}
287
327
. encode_to_vec ( ) ;
288
328
289
- self . p2p_server
329
+ tracing:: info!( "send connect command to peer_id: {:?}" , peer_id) ;
330
+ let request_id = self
331
+ . p2p_server
290
332
. tunnel_handle
291
333
. send_request ( peer_id, request, DialOptions :: Dial )
292
334
. await ?;
293
335
294
- tracing :: info! ( "send connect command to peer_id: {:?}" , peer_id ) ;
336
+ self . outbound_ready_notifiers . insert ( request_id , tx ) ;
295
337
296
- tx. send ( Ok ( PProxyCommandResponse :: SendConnectCommand { } ) )
297
- . map_err ( |_| Error :: EssentialTaskClosed )
338
+ Ok ( ( ) )
298
339
}
299
340
300
341
async fn on_send_outbound_package_command (
0 commit comments