forked from GGist/bip-rs
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: peer manager, sink, stream, messages and fused
- Loading branch information
Showing
8 changed files
with
466 additions
and
432 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use futures::Sink; | ||
|
||
use crate::PeerInfo; | ||
|
||
/// Trait for giving `PeerManager` message information it needs. | ||
/// | ||
/// For any `PeerProtocol` (or plain `Codec`), that wants to be managed | ||
/// by `PeerManager`, it must ensure that it's message type implements | ||
/// this trait so that we have the hooks necessary to manage the peer. | ||
pub trait ManagedMessage { | ||
/// Retrieve a keep alive message variant. | ||
fn keep_alive() -> Self; | ||
|
||
/// Whether or not this message is a keep alive message. | ||
fn is_keep_alive(&self) -> bool; | ||
} | ||
|
||
//----------------------------------------------------------------------------// | ||
|
||
/// Identifier for matching sent messages with received messages. | ||
pub type MessageId = u64; | ||
|
||
/// Message that can be sent to the `PeerManager`. | ||
pub enum IPeerManagerMessage<P> | ||
where | ||
P: Sink, | ||
{ | ||
/// Add a peer to the peer manager. | ||
AddPeer(PeerInfo, P), | ||
/// Remove a peer from the peer manager. | ||
RemovePeer(PeerInfo), | ||
/// Send a message to a peer. | ||
SendMessage(PeerInfo, MessageId, P::SinkItem), // TODO: Support querying for statistics | ||
} | ||
|
||
/// Message that can be received from the `PeerManager`. | ||
pub enum OPeerManagerMessage<M> { | ||
/// Message indicating a peer has been added to the peer manager. | ||
PeerAdded(PeerInfo), | ||
/// Message indicating a peer has been removed from the peer manager. | ||
PeerRemoved(PeerInfo), | ||
/// Message indicating a message has been sent to the given peer. | ||
SentMessage(PeerInfo, MessageId), | ||
/// Message indicating we have received a message from a peer. | ||
ReceivedMessage(PeerInfo, M), | ||
/// Message indicating a peer has disconnected from us. | ||
/// | ||
/// Same semantics as `PeerRemoved`, but the peer is not returned. | ||
PeerDisconnect(PeerInfo), | ||
/// Message indicating a peer errored out. | ||
/// | ||
/// Same semantics as `PeerRemoved`, but the peer is not returned. | ||
PeerError(PeerInfo, std::io::Error), | ||
} |
Oops, something went wrong.