Move open_channel message handling into an event
[rust-lightning] / src / ln / peer_handler.rs
index aec3a32e945b3f773c0519e46aec957696d1f50c..2472518d613a4199678a6dbe0c811d9fe42e38ea 100644 (file)
@@ -21,7 +21,9 @@ pub struct MessageHandler {
 /// implement Hash to meet the PeerManager API.
 /// For efficiency, Clone should be relatively cheap for this type.
 /// You probably want to just extend an int and put a file descriptor in a struct and implement
-/// send_data.
+/// send_data. Note that if you are using a higher-level net library that may close() itself, be
+/// careful to ensure you don't have races whereby you might register a new connection with an fd
+/// the same as a yet-to-be-disconnect_event()-ed.
 pub trait SocketDescriptor : cmp::Eq + hash::Hash + Clone {
        /// Attempts to send some data from the given Vec starting at the given offset to the peer.
        /// Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
@@ -409,8 +411,10 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
 
                                                                                        18 => {
                                                                                                let msg = try_potential_decodeerror!(msgs::Ping::decode(&msg_data[2..]));
-                                                                                               let resp = msgs::Pong { byteslen: msg.ponglen };
-                                                                                               encode_and_send_msg!(resp, 19);
+                                                                                               if msg.ponglen < 65532 {
+                                                                                                       let resp = msgs::Pong { byteslen: msg.ponglen };
+                                                                                                       encode_and_send_msg!(resp, 19);
+                                                                                               }
                                                                                        },
                                                                                        19 => {
                                                                                                try_potential_decodeerror!(msgs::Pong::decode(&msg_data[2..]));
@@ -610,6 +614,14 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                                                //TODO: Handle upstream in some confused form so that upstream just knows
                                                //to call us somehow?
                                        },
+                                       Event::SendOpenChannel { ref node_id, ref msg } => {
+                                               let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
+                                                               //TODO: Drop the pending channel? (or just let it timeout, but that sucks)
+                                                       });
+                                               peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 32)));
+                                               Self::do_attempt_write_data(&mut descriptor, peer);
+                                               continue;
+                                       },
                                        Event::SendFundingCreated { ref node_id, ref msg } => {
                                                let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
                                                                //TODO: generate a DiscardFunding event indicating to the wallet that