From: Arik Sosman Date: Mon, 18 May 2020 17:55:28 +0000 (-0700) Subject: Extract encode_and_send_msg into a method. X-Git-Tag: v0.0.12~63^2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=dbdef7fac193b9e6921d40d6b5a373ac2ac3405b;p=rust-lightning Extract encode_and_send_msg into a method. This is a response to splitting #585 into smaller components. This extraction will permit a future extraction of all message handling into a separate method, and then later even into a separate trait that will be accessible through language bindings. --- diff --git a/lightning/src/ln/peer_handler.rs b/lightning/src/ln/peer_handler.rs index dc3c37c41..e75c2c96e 100644 --- a/lightning/src/ln/peer_handler.rs +++ b/lightning/src/ln/peer_handler.rs @@ -12,7 +12,7 @@ use ln::features::InitFeatures; use ln::msgs; use ln::msgs::ChannelMessageHandler; use ln::channelmanager::{SimpleArcChannelManager, SimpleRefChannelManager}; -use util::ser::VecWriter; +use util::ser::{VecWriter, Writeable}; use ln::peer_channel_encryptor::{PeerChannelEncryptor,NextNoiseStep}; use ln::wire; use ln::wire::Encode; @@ -459,6 +459,17 @@ impl PeerManager(&self, peers_needing_send: &mut HashSet, peer: &mut Peer, descriptor: Descriptor, message: &M) { + let mut buffer = VecWriter(Vec::new()); + wire::write(message, &mut buffer).unwrap(); // crash if the write failed + let encoded_message = buffer.0; + + log_trace!(self.logger, "Enqueueing message of type {} to {}", message.type_id(), log_pubkey!(peer.their_node_id.unwrap())); + peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encoded_message[..])); + peers_needing_send.insert(descriptor); + } + fn do_read_event(&self, peer_descriptor: &mut Descriptor, data: &[u8]) -> Result { let pause_read = { let mut peers_lock = self.peers.lock().unwrap(); @@ -481,16 +492,6 @@ impl PeerManager { - { - log_trace!(self.logger, "Encoding and sending message of type {} to {}", $msg.type_id(), log_pubkey!(peer.their_node_id.unwrap())); - peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(&$msg)[..])); - peers.peers_needing_send.insert(peer_descriptor.clone()); - } - } - } - macro_rules! try_potential_handleerror { ($thing: expr) => { match $thing { @@ -508,7 +509,7 @@ impl PeerManager { log_trace!(self.logger, "Got Err handling message, sending Error message because {}", e.err); - encode_and_send_msg!(msg); + self.enqueue_message(&mut peers.peers_needing_send, peer, peer_descriptor.clone(), &msg); continue; }, } @@ -554,7 +555,7 @@ impl PeerManager { let their_node_id = try_potential_handleerror!(peer.channel_encryptor.process_act_three(&peer.pending_read_buffer[..])); @@ -653,7 +654,7 @@ impl PeerManager PeerManager { if msg.ponglen < 65532 { let resp = msgs::Pong { byteslen: msg.ponglen }; - encode_and_send_msg!(resp); + self.enqueue_message(&mut peers.peers_needing_send, peer, peer_descriptor.clone(), &resp); } }, wire::Message::Pong(_msg) => {