88use std:: {
99 collections:: VecDeque ,
1010 convert:: Infallible ,
11+ sync:: Arc ,
1112 task:: { Context , Poll } ,
1213 time:: Duration ,
1314} ;
1415
1516use futures:: { future:: BoxFuture , prelude:: * } ;
1617use futures_timer:: Delay ;
1718use libp2p:: {
19+ PeerId ,
1820 core:: upgrade:: ReadyUpgrade ,
1921 swarm:: {
2022 ConnectionHandler , ConnectionHandlerEvent , Stream , StreamProtocol , StreamUpgradeError ,
@@ -26,7 +28,8 @@ use libp2p::{
2628} ;
2729
2830use crate :: {
29- PROTOCOL_NAME , config:: Config , failure:: Failure , peerinfopb:: v1:: peerinfo:: PeerInfo , protocol,
31+ PROTOCOL_NAME , config:: Config , failure:: Failure , peerinfopb:: v1:: peerinfo:: PeerInfo ,
32+ protocol:: ProtocolState ,
3033} ;
3134
3235/// Result of a successful peer info exchange.
@@ -55,6 +58,8 @@ pub struct Handler {
5558 inbound : Option < InboundFuture > ,
5659 /// Tracks the state of our handler.
5760 state : State ,
61+ /// The protocol state.
62+ protocol : Arc < ProtocolState > ,
5863}
5964
6065/// Tracks the state of the handler.
@@ -71,8 +76,9 @@ enum State {
7176
7277impl Handler {
7378 /// Builds a new [`Handler`] with the given configuration.
74- pub fn new ( config : Config ) -> Self {
79+ pub fn new ( config : Config , peer : PeerId ) -> Self {
7580 let interval = config. interval ( ) ;
81+ let local_info = config. local_info ( ) . clone ( ) ;
7682 Handler {
7783 config,
7884 interval : Delay :: new ( interval) ,
@@ -81,6 +87,7 @@ impl Handler {
8187 outbound : None ,
8288 inbound : None ,
8389 state : State :: Active ,
90+ protocol : Arc :: new ( ProtocolState :: new ( peer, local_info) ) ,
8491 }
8592 }
8693
@@ -158,8 +165,14 @@ impl ConnectionHandler for Handler {
158165 }
159166 Poll :: Ready ( Ok ( ( stream, _request) ) ) => {
160167 tracing:: trace!( "Answered inbound peerinfo request from peer" ) ;
161- self . inbound =
162- Some ( recv_peer_info ( stream, self . config . local_info ( ) . to_proto ( ) ) . boxed ( ) ) ;
168+ self . inbound = Some (
169+ recv_peer_info (
170+ self . protocol . clone ( ) ,
171+ stream,
172+ self . config . local_info ( ) . to_proto ( ) ,
173+ )
174+ . boxed ( ) ,
175+ ) ;
163176 }
164177 }
165178 }
@@ -207,6 +220,7 @@ impl ConnectionHandler for Handler {
207220 Poll :: Ready ( _) => {
208221 self . outbound = Some ( OutboundState :: Request (
209222 send_peer_info (
223+ self . protocol . clone ( ) ,
210224 stream,
211225 self . config . local_info ( ) . to_proto ( ) ,
212226 self . config . timeout ( ) ,
@@ -246,7 +260,8 @@ impl ConnectionHandler for Handler {
246260 } ) => {
247261 stream. ignore_for_keep_alive ( ) ;
248262 let local_info = self . config . local_info ( ) . to_proto ( ) ;
249- self . inbound = Some ( recv_peer_info ( stream, local_info) . boxed ( ) ) ;
263+ self . inbound =
264+ Some ( recv_peer_info ( self . protocol . clone ( ) , stream, local_info) . boxed ( ) ) ;
250265 }
251266 ConnectionEvent :: FullyNegotiatedOutbound ( FullyNegotiatedOutbound {
252267 protocol : mut stream,
@@ -256,7 +271,13 @@ impl ConnectionHandler for Handler {
256271 self . interval . reset ( Duration :: new ( 0 , 0 ) ) ;
257272 let request = self . config . local_info ( ) . to_proto ( ) ;
258273 self . outbound = Some ( OutboundState :: Request (
259- send_peer_info ( stream, request, self . config . timeout ( ) ) . boxed ( ) ,
274+ send_peer_info (
275+ self . protocol . clone ( ) ,
276+ stream,
277+ request,
278+ self . config . timeout ( ) ,
279+ )
280+ . boxed ( ) ,
260281 ) ) ;
261282 }
262283 ConnectionEvent :: DialUpgradeError ( dial_upgrade_error) => {
@@ -282,11 +303,12 @@ enum OutboundState {
282303
283304/// A wrapper around [`protocol::send_peer_info`] that enforces a timeout.
284305async fn send_peer_info (
306+ protocol : Arc < ProtocolState > ,
285307 stream : Stream ,
286308 request : PeerInfo ,
287309 timeout : Duration ,
288310) -> Result < ( Stream , PeerInfo ) , Failure > {
289- let send = protocol:: send_peer_info ( stream, & request) ;
311+ let send = protocol. send_peer_info ( stream, & request) ;
290312 futures:: pin_mut!( send) ;
291313
292314 match future:: select ( send, Delay :: new ( timeout) ) . await {
@@ -299,8 +321,9 @@ async fn send_peer_info(
299321/// A wrapper around [`protocol::recv_peer_info`] that returns only the stream
300322/// and request (for use in inbound handling).
301323async fn recv_peer_info (
324+ protocol : Arc < ProtocolState > ,
302325 stream : Stream ,
303326 local_info : PeerInfo ,
304327) -> Result < ( Stream , PeerInfo ) , std:: io:: Error > {
305- protocol:: recv_peer_info ( stream, & local_info) . await
328+ protocol. recv_peer_info ( stream, & local_info) . await
306329}
0 commit comments