18
18
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19
19
// DEALINGS IN THE SOFTWARE.
20
20
21
- use crate :: handler:: { self , Proto , Push } ;
21
+ use crate :: handler:: { self , InEvent , Proto , Reply } ;
22
22
use crate :: protocol:: { Info , ReplySubstream , UpgradeError } ;
23
- use futures:: prelude:: * ;
24
23
use libp2p_core:: {
25
24
connection:: ConnectionId , multiaddr:: Protocol , ConnectedPoint , Multiaddr , PeerId , PublicKey ,
26
25
} ;
@@ -35,7 +34,6 @@ use std::num::NonZeroUsize;
35
34
use std:: {
36
35
collections:: { HashMap , HashSet , VecDeque } ,
37
36
iter:: FromIterator ,
38
- pin:: Pin ,
39
37
task:: Context ,
40
38
task:: Poll ,
41
39
time:: Duration ,
@@ -51,8 +49,8 @@ pub struct Behaviour {
51
49
config : Config ,
52
50
/// For each peer we're connected to, the observed address to send back to it.
53
51
connected : HashMap < PeerId , HashMap < ConnectionId , Multiaddr > > ,
54
- /// Pending replies to send .
55
- pending_replies : VecDeque < Reply > ,
52
+ /// Pending requests to respond .
53
+ requests : VecDeque < Request > ,
56
54
/// Pending events to be emitted when polled.
57
55
events : VecDeque < NetworkBehaviourAction < Event , Proto > > ,
58
56
/// Peers to which an active push with current information about
@@ -63,18 +61,10 @@ pub struct Behaviour {
63
61
}
64
62
65
63
/// A pending reply to an inbound identification request.
66
- enum Reply {
67
- /// The reply is queued for sending.
68
- Queued {
69
- peer : PeerId ,
70
- io : ReplySubstream < NegotiatedSubstream > ,
71
- observed : Multiaddr ,
72
- } ,
73
- /// The reply is being sent.
74
- Sending {
75
- peer : PeerId ,
76
- io : Pin < Box < dyn Future < Output = Result < ( ) , UpgradeError > > + Send > > ,
77
- } ,
64
+ struct Request {
65
+ peer : PeerId ,
66
+ io : ReplySubstream < NegotiatedSubstream > ,
67
+ observed : Multiaddr ,
78
68
}
79
69
80
70
/// Configuration for the [`identify::Behaviour`](Behaviour).
@@ -184,7 +174,7 @@ impl Behaviour {
184
174
Self {
185
175
config,
186
176
connected : HashMap :: new ( ) ,
187
- pending_replies : VecDeque :: new ( ) ,
177
+ requests : VecDeque :: new ( ) ,
188
178
events : VecDeque :: new ( ) ,
189
179
pending_push : HashSet :: new ( ) ,
190
180
discovered_peers,
@@ -287,7 +277,7 @@ impl NetworkBehaviour for Behaviour {
287
277
with an established connection and calling `NetworkBehaviour::on_event` \
288
278
with `FromSwarm::ConnectionEstablished ensures there is an entry; qed",
289
279
) ;
290
- self . pending_replies . push_back ( Reply :: Queued {
280
+ self . requests . push_back ( Request {
291
281
peer : peer_id,
292
282
io : sender,
293
283
observed : observed. clone ( ) ,
@@ -305,7 +295,7 @@ impl NetworkBehaviour for Behaviour {
305
295
306
296
fn poll (
307
297
& mut self ,
308
- cx : & mut Context < ' _ > ,
298
+ _cx : & mut Context < ' _ > ,
309
299
params : & mut impl PollParameters ,
310
300
) -> Poll < NetworkBehaviourAction < Self :: OutEvent , Self :: ConnectionHandler > > {
311
301
if let Some ( event) = self . events . pop_front ( ) {
@@ -333,7 +323,7 @@ impl NetworkBehaviour for Behaviour {
333
323
observed_addr,
334
324
} ;
335
325
336
- ( * peer, Push ( info) )
326
+ ( * peer, InEvent :: Push ( info) )
337
327
} )
338
328
} ) ;
339
329
@@ -346,55 +336,21 @@ impl NetworkBehaviour for Behaviour {
346
336
} ) ;
347
337
}
348
338
349
- // Check for pending replies to send.
350
- if let Some ( r) = self . pending_replies . pop_front ( ) {
351
- let mut sending = 0 ;
352
- let to_send = self . pending_replies . len ( ) + 1 ;
353
- let mut reply = Some ( r) ;
354
- loop {
355
- match reply {
356
- Some ( Reply :: Queued { peer, io, observed } ) => {
357
- let info = Info {
358
- listen_addrs : listen_addrs ( params) ,
359
- protocols : supported_protocols ( params) ,
360
- public_key : self . config . local_public_key . clone ( ) ,
361
- protocol_version : self . config . protocol_version . clone ( ) ,
362
- agent_version : self . config . agent_version . clone ( ) ,
363
- observed_addr : observed,
364
- } ;
365
- let io = Box :: pin ( io. send ( info) ) ;
366
- reply = Some ( Reply :: Sending { peer, io } ) ;
367
- }
368
- Some ( Reply :: Sending { peer, mut io } ) => {
369
- sending += 1 ;
370
- match Future :: poll ( Pin :: new ( & mut io) , cx) {
371
- Poll :: Ready ( Ok ( ( ) ) ) => {
372
- let event = Event :: Sent { peer_id : peer } ;
373
- return Poll :: Ready ( NetworkBehaviourAction :: GenerateEvent ( event) ) ;
374
- }
375
- Poll :: Pending => {
376
- self . pending_replies . push_back ( Reply :: Sending { peer, io } ) ;
377
- if sending == to_send {
378
- // All remaining futures are NotReady
379
- break ;
380
- } else {
381
- reply = self . pending_replies . pop_front ( ) ;
382
- }
383
- }
384
- Poll :: Ready ( Err ( err) ) => {
385
- let event = Event :: Error {
386
- peer_id : peer,
387
- error : ConnectionHandlerUpgrErr :: Upgrade (
388
- libp2p_core:: upgrade:: UpgradeError :: Apply ( err) ,
389
- ) ,
390
- } ;
391
- return Poll :: Ready ( NetworkBehaviourAction :: GenerateEvent ( event) ) ;
392
- }
393
- }
394
- }
395
- None => unreachable ! ( ) ,
396
- }
397
- }
339
+ // Check for pending requests to send back to the handler for reply.
340
+ if let Some ( Request { peer, io, observed } ) = self . requests . pop_front ( ) {
341
+ let info = Info {
342
+ listen_addrs : listen_addrs ( params) ,
343
+ protocols : supported_protocols ( params) ,
344
+ public_key : self . config . local_public_key . clone ( ) ,
345
+ protocol_version : self . config . protocol_version . clone ( ) ,
346
+ agent_version : self . config . agent_version . clone ( ) ,
347
+ observed_addr : observed,
348
+ } ;
349
+ return Poll :: Ready ( NetworkBehaviourAction :: NotifyHandler {
350
+ peer_id : peer,
351
+ handler : NotifyHandler :: Any ,
352
+ event : InEvent :: Identify ( Reply { peer, info, io } ) ,
353
+ } ) ;
398
354
}
399
355
400
356
Poll :: Pending
@@ -557,6 +513,7 @@ impl PeerCache {
557
513
mod tests {
558
514
use super :: * ;
559
515
use futures:: pin_mut;
516
+ use futures:: prelude:: * ;
560
517
use libp2p:: mplex:: MplexConfig ;
561
518
use libp2p:: noise;
562
519
use libp2p:: tcp;
0 commit comments