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
- mod error;
22
- pub ( crate ) mod handler;
23
- mod listeners;
24
- mod substream;
25
-
26
- pub ( crate ) mod pool;
27
-
28
- pub use error:: {
29
- ConnectionError , PendingConnectionError , PendingInboundConnectionError ,
30
- PendingOutboundConnectionError ,
31
- } ;
32
- pub use handler:: { ConnectionHandler , ConnectionHandlerEvent , IntoConnectionHandler } ;
33
- pub use listeners:: { ListenerId , ListenersEvent , ListenersStream } ;
34
- pub use pool:: { ConnectionCounters , ConnectionLimits } ;
35
- pub use pool:: { EstablishedConnection , EstablishedConnectionIter , PendingConnection } ;
36
- pub use substream:: { Close , Substream , SubstreamEndpoint } ;
37
-
38
21
use crate :: multiaddr:: { Multiaddr , Protocol } ;
39
- use crate :: muxing:: StreamMuxer ;
40
- use crate :: PeerId ;
41
- use std:: hash:: Hash ;
42
- use std:: { error:: Error , fmt, pin:: Pin , task:: Context , task:: Poll } ;
43
- use substream:: { Muxing , SubstreamEvent } ;
44
22
45
23
/// Connection identifier.
46
24
#[ derive( Debug , Copy , Clone , Hash , PartialEq , Eq , PartialOrd , Ord ) ]
@@ -53,7 +31,34 @@ impl ConnectionId {
53
31
/// in test environments. There is in general no guarantee
54
32
/// that all connection IDs are based on non-negative integers.
55
33
pub fn new ( id : usize ) -> Self {
56
- ConnectionId ( id)
34
+ Self ( id)
35
+ }
36
+ }
37
+
38
+ impl std:: ops:: Add < usize > for ConnectionId {
39
+ type Output = Self ;
40
+
41
+ fn add ( self , other : usize ) -> Self {
42
+ Self ( self . 0 + other)
43
+ }
44
+ }
45
+
46
+ /// The ID of a single listener.
47
+ #[ derive( Copy , Clone , Debug , PartialEq , Eq , Hash , PartialOrd , Ord ) ]
48
+ pub struct ListenerId ( u64 ) ;
49
+
50
+ impl ListenerId {
51
+ /// Creates a `ListenerId` from a non-negative integer.
52
+ pub fn new ( id : u64 ) -> Self {
53
+ Self ( id)
54
+ }
55
+ }
56
+
57
+ impl std:: ops:: Add < u64 > for ListenerId {
58
+ type Output = Self ;
59
+
60
+ fn add ( self , other : u64 ) -> Self {
61
+ Self ( self . 0 + other)
57
62
}
58
63
}
59
64
@@ -236,181 +241,3 @@ impl ConnectedPoint {
236
241
}
237
242
}
238
243
}
239
-
240
- /// Information about a successfully established connection.
241
- #[ derive( Debug , Clone , PartialEq , Eq ) ]
242
- pub struct Connected {
243
- /// The connected endpoint, including network address information.
244
- pub endpoint : ConnectedPoint ,
245
- /// Information obtained from the transport.
246
- pub peer_id : PeerId ,
247
- }
248
-
249
- /// Event generated by a [`Connection`].
250
- #[ derive( Debug , Clone ) ]
251
- pub enum Event < T > {
252
- /// Event generated by the [`ConnectionHandler`].
253
- Handler ( T ) ,
254
- /// Address of the remote has changed.
255
- AddressChange ( Multiaddr ) ,
256
- }
257
-
258
- /// A multiplexed connection to a peer with an associated `ConnectionHandler`.
259
- pub struct Connection < TMuxer , THandler >
260
- where
261
- TMuxer : StreamMuxer ,
262
- THandler : ConnectionHandler < Substream = Substream < TMuxer > > ,
263
- {
264
- /// Node that handles the muxing.
265
- muxing : substream:: Muxing < TMuxer , THandler :: OutboundOpenInfo > ,
266
- /// Handler that processes substreams.
267
- handler : THandler ,
268
- }
269
-
270
- impl < TMuxer , THandler > fmt:: Debug for Connection < TMuxer , THandler >
271
- where
272
- TMuxer : StreamMuxer ,
273
- THandler : ConnectionHandler < Substream = Substream < TMuxer > > + fmt:: Debug ,
274
- {
275
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
276
- f. debug_struct ( "Connection" )
277
- . field ( "muxing" , & self . muxing )
278
- . field ( "handler" , & self . handler )
279
- . finish ( )
280
- }
281
- }
282
-
283
- impl < TMuxer , THandler > Unpin for Connection < TMuxer , THandler >
284
- where
285
- TMuxer : StreamMuxer ,
286
- THandler : ConnectionHandler < Substream = Substream < TMuxer > > ,
287
- {
288
- }
289
-
290
- impl < TMuxer , THandler > Connection < TMuxer , THandler >
291
- where
292
- TMuxer : StreamMuxer ,
293
- THandler : ConnectionHandler < Substream = Substream < TMuxer > > ,
294
- {
295
- /// Builds a new `Connection` from the given substream multiplexer
296
- /// and connection handler.
297
- pub fn new ( muxer : TMuxer , handler : THandler ) -> Self {
298
- Connection {
299
- muxing : Muxing :: new ( muxer) ,
300
- handler,
301
- }
302
- }
303
-
304
- /// Returns a reference to the `ConnectionHandler`
305
- pub fn handler ( & self ) -> & THandler {
306
- & self . handler
307
- }
308
-
309
- /// Returns a mutable reference to the `ConnectionHandler`
310
- pub fn handler_mut ( & mut self ) -> & mut THandler {
311
- & mut self . handler
312
- }
313
-
314
- /// Notifies the connection handler of an event.
315
- pub fn inject_event ( & mut self , event : THandler :: InEvent ) {
316
- self . handler . inject_event ( event) ;
317
- }
318
-
319
- /// Begins an orderly shutdown of the connection, returning the connection
320
- /// handler and a `Future` that resolves when connection shutdown is complete.
321
- pub fn close ( self ) -> ( THandler , Close < TMuxer > ) {
322
- ( self . handler , self . muxing . close ( ) . 0 )
323
- }
324
-
325
- /// Polls the connection for events produced by the associated handler
326
- /// as a result of I/O activity on the substream multiplexer.
327
- pub fn poll (
328
- mut self : Pin < & mut Self > ,
329
- cx : & mut Context < ' _ > ,
330
- ) -> Poll < Result < Event < THandler :: OutEvent > , ConnectionError < THandler :: Error > > > {
331
- loop {
332
- let mut io_pending = false ;
333
-
334
- // Perform I/O on the connection through the muxer, informing the handler
335
- // of new substreams.
336
- match self . muxing . poll ( cx) {
337
- Poll :: Pending => io_pending = true ,
338
- Poll :: Ready ( Ok ( SubstreamEvent :: InboundSubstream { substream } ) ) => self
339
- . handler
340
- . inject_substream ( substream, SubstreamEndpoint :: Listener ) ,
341
- Poll :: Ready ( Ok ( SubstreamEvent :: OutboundSubstream {
342
- user_data,
343
- substream,
344
- } ) ) => {
345
- let endpoint = SubstreamEndpoint :: Dialer ( user_data) ;
346
- self . handler . inject_substream ( substream, endpoint)
347
- }
348
- Poll :: Ready ( Ok ( SubstreamEvent :: AddressChange ( address) ) ) => {
349
- self . handler . inject_address_change ( & address) ;
350
- return Poll :: Ready ( Ok ( Event :: AddressChange ( address) ) ) ;
351
- }
352
- Poll :: Ready ( Err ( err) ) => return Poll :: Ready ( Err ( ConnectionError :: IO ( err) ) ) ,
353
- }
354
-
355
- // Poll the handler for new events.
356
- match self . handler . poll ( cx) {
357
- Poll :: Pending => {
358
- if io_pending {
359
- return Poll :: Pending ; // Nothing to do
360
- }
361
- }
362
- Poll :: Ready ( Ok ( ConnectionHandlerEvent :: OutboundSubstreamRequest ( user_data) ) ) => {
363
- self . muxing . open_substream ( user_data) ;
364
- }
365
- Poll :: Ready ( Ok ( ConnectionHandlerEvent :: Custom ( event) ) ) => {
366
- return Poll :: Ready ( Ok ( Event :: Handler ( event) ) ) ;
367
- }
368
- Poll :: Ready ( Err ( err) ) => return Poll :: Ready ( Err ( ConnectionError :: Handler ( err) ) ) ,
369
- }
370
- }
371
- }
372
- }
373
-
374
- /// Borrowed information about an incoming connection currently being negotiated.
375
- #[ derive( Debug , Copy , Clone ) ]
376
- pub struct IncomingInfo < ' a > {
377
- /// Local connection address.
378
- pub local_addr : & ' a Multiaddr ,
379
- /// Address used to send back data to the remote.
380
- pub send_back_addr : & ' a Multiaddr ,
381
- }
382
-
383
- impl < ' a > IncomingInfo < ' a > {
384
- /// Builds the [`PendingPoint`] corresponding to the incoming connection.
385
- pub fn to_pending_point ( & self ) -> PendingPoint {
386
- PendingPoint :: Listener {
387
- local_addr : self . local_addr . clone ( ) ,
388
- send_back_addr : self . send_back_addr . clone ( ) ,
389
- }
390
- }
391
- /// Builds the [`ConnectedPoint`] corresponding to the incoming connection.
392
- pub fn to_connected_point ( & self ) -> ConnectedPoint {
393
- ConnectedPoint :: Listener {
394
- local_addr : self . local_addr . clone ( ) ,
395
- send_back_addr : self . send_back_addr . clone ( ) ,
396
- }
397
- }
398
- }
399
-
400
- /// Information about a connection limit.
401
- #[ derive( Debug , Clone ) ]
402
- pub struct ConnectionLimit {
403
- /// The maximum number of connections.
404
- pub limit : u32 ,
405
- /// The current number of connections.
406
- pub current : u32 ,
407
- }
408
-
409
- impl fmt:: Display for ConnectionLimit {
410
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
411
- write ! ( f, "{}/{}" , self . current, self . limit)
412
- }
413
- }
414
-
415
- /// A `ConnectionLimit` can represent an error if it has been exceeded.
416
- impl Error for ConnectionLimit { }
0 commit comments