From 5019b31b286398ecdee773afe203a5417a97bd65 Mon Sep 17 00:00:00 2001 From: Max Fang Date: Tue, 19 Jul 2022 21:57:12 -0700 Subject: [PATCH] lightning-net-tokio: Allow custom smart pointers --- lightning-net-tokio/src/lib.rs | 76 +++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 25 deletions(-) diff --git a/lightning-net-tokio/src/lib.rs b/lightning-net-tokio/src/lib.rs index 2ac10762b..645a7434e 100644 --- a/lightning-net-tokio/src/lib.rs +++ b/lightning-net-tokio/src/lib.rs @@ -84,6 +84,7 @@ use lightning::ln::peer_handler::CustomMessageHandler; use lightning::ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, NetAddress}; use lightning::util::logger::Logger; +use std::ops::Deref; use std::task; use std::net::SocketAddr; use std::net::TcpStream as StdTcpStream; @@ -120,11 +121,16 @@ struct Connection { id: u64, } impl Connection { - async fn poll_event_process(peer_manager: Arc, Arc, Arc, Arc>>, mut event_receiver: mpsc::Receiver<()>) where - CMH: ChannelMessageHandler + 'static + Send + Sync, - RMH: RoutingMessageHandler + 'static + Send + Sync, - L: Logger + 'static + ?Sized + Send + Sync, - UMH: CustomMessageHandler + 'static + Send + Sync { + async fn poll_event_process(peer_manager: Arc>, mut event_receiver: mpsc::Receiver<()>) where + CMH: Deref + 'static + Send + Sync, + RMH: Deref + 'static + Send + Sync, + L: Deref + 'static + Send + Sync, + UMH: Deref + 'static + Send + Sync, + CMH::Target: ChannelMessageHandler + Send + Sync, + RMH::Target: RoutingMessageHandler + Send + Sync, + L::Target: Logger + Send + Sync, + UMH::Target: CustomMessageHandler + Send + Sync, + { loop { if event_receiver.recv().await.is_none() { return; @@ -133,11 +139,16 @@ impl Connection { } } - async fn schedule_read(peer_manager: Arc, Arc, Arc, Arc>>, us: Arc>, mut reader: io::ReadHalf, mut read_wake_receiver: mpsc::Receiver<()>, mut write_avail_receiver: mpsc::Receiver<()>) where - CMH: ChannelMessageHandler + 'static + Send + Sync, - RMH: RoutingMessageHandler + 'static + Send + Sync, - L: Logger + 'static + ?Sized + Send + Sync, - UMH: CustomMessageHandler + 'static + Send + Sync { + async fn schedule_read(peer_manager: Arc>, us: Arc>, mut reader: io::ReadHalf, mut read_wake_receiver: mpsc::Receiver<()>, mut write_avail_receiver: mpsc::Receiver<()>) where + CMH: Deref + 'static + Send + Sync, + RMH: Deref + 'static + Send + Sync, + L: Deref + 'static + Send + Sync, + UMH: Deref + 'static + Send + Sync, + CMH::Target: ChannelMessageHandler + 'static + Send + Sync, + RMH::Target: RoutingMessageHandler + 'static + Send + Sync, + L::Target: Logger + 'static + Send + Sync, + UMH::Target: CustomMessageHandler + 'static + Send + Sync, + { // Create a waker to wake up poll_event_process, above let (event_waker, event_receiver) = mpsc::channel(1); tokio::spawn(Self::poll_event_process(Arc::clone(&peer_manager), event_receiver)); @@ -255,11 +266,16 @@ fn get_addr_from_stream(stream: &StdTcpStream) -> Option { /// The returned future will complete when the peer is disconnected and associated handling /// futures are freed, though, because all processing futures are spawned with tokio::spawn, you do /// not need to poll the provided future in order to make progress. -pub fn setup_inbound(peer_manager: Arc, Arc, Arc, Arc>>, stream: StdTcpStream) -> impl std::future::Future where - CMH: ChannelMessageHandler + 'static + Send + Sync, - RMH: RoutingMessageHandler + 'static + Send + Sync, - L: Logger + 'static + ?Sized + Send + Sync, - UMH: CustomMessageHandler + 'static + Send + Sync { +pub fn setup_inbound(peer_manager: Arc>, stream: StdTcpStream) -> impl std::future::Future where + CMH: Deref + 'static + Send + Sync, + RMH: Deref + 'static + Send + Sync, + L: Deref + 'static + Send + Sync, + UMH: Deref + 'static + Send + Sync, + CMH::Target: ChannelMessageHandler + Send + Sync, + RMH::Target: RoutingMessageHandler + Send + Sync, + L::Target: Logger + Send + Sync, + UMH::Target: CustomMessageHandler + Send + Sync, +{ let remote_addr = get_addr_from_stream(&stream); let (reader, write_receiver, read_receiver, us) = Connection::new(stream); #[cfg(debug_assertions)] @@ -297,11 +313,16 @@ pub fn setup_inbound(peer_manager: Arc(peer_manager: Arc, Arc, Arc, Arc>>, their_node_id: PublicKey, stream: StdTcpStream) -> impl std::future::Future where - CMH: ChannelMessageHandler + 'static + Send + Sync, - RMH: RoutingMessageHandler + 'static + Send + Sync, - L: Logger + 'static + ?Sized + Send + Sync, - UMH: CustomMessageHandler + 'static + Send + Sync { +pub fn setup_outbound(peer_manager: Arc>, their_node_id: PublicKey, stream: StdTcpStream) -> impl std::future::Future where + CMH: Deref + 'static + Send + Sync, + RMH: Deref + 'static + Send + Sync, + L: Deref + 'static + Send + Sync, + UMH: Deref + 'static + Send + Sync, + CMH::Target: ChannelMessageHandler + Send + Sync, + RMH::Target: RoutingMessageHandler + Send + Sync, + L::Target: Logger + Send + Sync, + UMH::Target: CustomMessageHandler + Send + Sync, +{ let remote_addr = get_addr_from_stream(&stream); let (reader, mut write_receiver, read_receiver, us) = Connection::new(stream); #[cfg(debug_assertions)] @@ -368,11 +389,16 @@ pub fn setup_outbound(peer_manager: Arc(peer_manager: Arc, Arc, Arc, Arc>>, their_node_id: PublicKey, addr: SocketAddr) -> Option> where - CMH: ChannelMessageHandler + 'static + Send + Sync, - RMH: RoutingMessageHandler + 'static + Send + Sync, - L: Logger + 'static + ?Sized + Send + Sync, - UMH: CustomMessageHandler + 'static + Send + Sync { +pub async fn connect_outbound(peer_manager: Arc>, their_node_id: PublicKey, addr: SocketAddr) -> Option> where + CMH: Deref + 'static + Send + Sync, + RMH: Deref + 'static + Send + Sync, + L: Deref + 'static + Send + Sync, + UMH: Deref + 'static + Send + Sync, + CMH::Target: ChannelMessageHandler + Send + Sync, + RMH::Target: RoutingMessageHandler + Send + Sync, + L::Target: Logger + Send + Sync, + UMH::Target: CustomMessageHandler + Send + Sync, +{ if let Ok(Ok(stream)) = time::timeout(Duration::from_secs(10), async { TcpStream::connect(&addr).await.map(|s| s.into_std().unwrap()) }).await { Some(setup_outbound(peer_manager, their_node_id, stream)) } else { None } -- 2.39.5