+ peer.pending_read_buffer_pos = 0;
+
+ macro_rules! encode_and_send_msg {
+ ($msg: expr, $msg_code: expr) => {
+ {
+ log_trace!(self, "Encoding and sending message of type {} to {}", $msg_code, log_pubkey!(peer.their_node_id.unwrap()));
+ peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!($msg, $msg_code)[..]));
+ peers.peers_needing_send.insert(peer_descriptor.clone());
+ }
+ }
+ }
+
+ macro_rules! try_potential_handleerror {
+ ($thing: expr) => {
+ match $thing {
+ Ok(x) => x,
+ Err(e) => {
+ if let Some(action) = e.action {
+ match action {
+ msgs::ErrorAction::DisconnectPeer { msg: _ } => {
+ //TODO: Try to push msg
+ log_trace!(self, "Got Err handling message, disconnecting peer because {}", e.err);
+ return Err(PeerHandleError{ no_connection_possible: false });
+ },
+ msgs::ErrorAction::IgnoreError => {
+ log_trace!(self, "Got Err handling message, ignoring because {}", e.err);
+ continue;
+ },
+ msgs::ErrorAction::SendErrorMessage { msg } => {
+ log_trace!(self, "Got Err handling message, sending Error message because {}", e.err);
+ encode_and_send_msg!(msg, 17);
+ continue;
+ },
+ }
+ } else {
+ log_debug!(self, "Got Err handling message, action not yet filled in: {}", e.err);
+ return Err(PeerHandleError{ no_connection_possible: false });
+ }
+ }
+ };
+ }
+ }
+
+ macro_rules! try_potential_decodeerror {
+ ($thing: expr) => {
+ match $thing {
+ Ok(x) => x,
+ Err(e) => {
+ match e {
+ 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 update!");
+ continue;
+ },
+ msgs::DecodeError::InvalidValue => {
+ log_debug!(self, "Got an invalid value while deserializing message");
+ return Err(PeerHandleError{ no_connection_possible: false });
+ },
+ msgs::DecodeError::ShortRead => {
+ log_debug!(self, "Deserialization failed due to shortness of message");
+ 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");
+ continue;
+ },
+ msgs::DecodeError::BadLengthDescriptor => return Err(PeerHandleError{ no_connection_possible: false }),
+ msgs::DecodeError::Io(_) => return Err(PeerHandleError{ no_connection_possible: false }),
+ }
+ }
+ };
+ }
+ }
+
+ macro_rules! insert_node_id {
+ () => {
+ match peers.node_id_to_descriptor.entry(peer.their_node_id.unwrap()) {
+ hash_map::Entry::Occupied(_) => {
+ log_trace!(self, "Got second connection with {}, closing", log_pubkey!(peer.their_node_id.unwrap()));
+ peer.their_node_id = None; // Unset so that we don't generate a peer_disconnected event
+ return Err(PeerHandleError{ no_connection_possible: false })
+ },
+ hash_map::Entry::Vacant(entry) => {
+ log_trace!(self, "Finished noise handshake for connection with {}", log_pubkey!(peer.their_node_id.unwrap()));
+ entry.insert(peer_descriptor.clone())
+ },
+ };
+ }
+ }
+