+//! Top level peer message handling and socket handling logic lives here.
+//! Instead of actually servicing sockets ourselves we require that you implement the
+//! SocketDescriptor interface and use that to receive actions which you should perform on the
+//! socket, and call into PeerManager with bytes read from the socket. The PeerManager will then
+//! call into the provided message handlers (probably a ChannelManager and Router) with messages
+//! they should handle, and encoding/sending response messages.
+
use secp256k1::key::{SecretKey,PublicKey};
use ln::msgs;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::{cmp,error,mem,hash,fmt};
+/// Provides references to trait impls which handle different types of messages.
pub struct MessageHandler {
+ /// A message handler which handles messages specific to channels. Usually this is just a
+ /// ChannelManager object.
pub chan_handler: Arc<msgs::ChannelMessageHandler>,
+ /// A message handler which handles messages updating our knowledge of the network channel
+ /// graph. Usually this is just a Router object.
pub route_handler: Arc<msgs::RoutingMessageHandler>,
}
/// disconnect_event (unless it was provided in response to a new_*_connection event, in which case
/// no such disconnect_event must be generated and the socket be silently disconencted).
pub struct PeerHandleError {
+ /// Used to indicate that we probably can't make any future connections to this peer, implying
+ /// we should go ahead and force-close any channels we have with it.
no_connection_possible: bool,
}
impl fmt::Debug for PeerHandleError {
}
}
+/// A PeerManager manages a set of peers, described by their SocketDescriptor and marshalls socket
+/// events into messages which it passes on to its MessageHandlers.
pub struct PeerManager<Descriptor: SocketDescriptor> {
message_handler: MessageHandler,
peers: Mutex<PeerHolder<Descriptor>>,
/// Manages and reacts to connection events. You probably want to use file descriptors as PeerIds.
/// PeerIds may repeat, but only after disconnect_event() has been called.
impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
+ /// Constructs a new PeerManager with the given message handlers and node_id secret key
pub fn new(message_handler: MessageHandler, our_node_secret: SecretKey, logger: Arc<Logger>) -> PeerManager<Descriptor> {
PeerManager {
message_handler: message_handler,
Ok(x) => x,
Err(e) => {
match e {
- msgs::DecodeError::UnknownRealmByte => return Err(PeerHandleError{ no_connection_possible: false }),
+ msgs::DecodeError::UnknownVersion => return Err(PeerHandleError{ no_connection_possible: false }),
msgs::DecodeError::UnknownRequiredFeature => {
log_debug!(self, "Got a channel/node announcement with an known required feature flag, you may want to udpate!");
continue;
},
- msgs::DecodeError::BadPublicKey => return Err(PeerHandleError{ no_connection_possible: false }),
- msgs::DecodeError::BadSignature => return Err(PeerHandleError{ no_connection_possible: false }),
- msgs::DecodeError::BadText => return Err(PeerHandleError{ no_connection_possible: false }),
+ msgs::DecodeError::InvalidValue => return Err(PeerHandleError{ no_connection_possible: false }),
msgs::DecodeError::ShortRead => return Err(PeerHandleError{ no_connection_possible: false }),
msgs::DecodeError::ExtraAddressesPerType => {
log_debug!(self, "Error decoding message, ignoring due to lnd spec incompatibility. See https://github.com/lightningnetwork/lnd/issues/1407");
},
msgs::DecodeError::BadLengthDescriptor => return Err(PeerHandleError{ no_connection_possible: false }),
msgs::DecodeError::Io(_) => return Err(PeerHandleError{ no_connection_possible: false }),
- msgs::DecodeError::InvalidValue => panic!("should not happen with message decoding"),
}
}
};