[Java] Update LDK batteries to latest upstream API
[ldk-java] / src / main / java / org / ldk / batteries / NioPeerHandler.java
index f0a38282d04d7f7fcaf74c8818bbebdf9328fe58..2d38726450b435c52c9ec63f07383f593c9a1e30 100644 (file)
@@ -1,10 +1,14 @@
 package org.ldk.batteries;
 
+import org.ldk.impl.bindings;
 import org.ldk.structs.*;
 
 import java.io.IOException;
-import java.net.SocketAddress;
-import java.net.StandardSocketOptions;
+import java.lang.reflect.Field;
+import java.lang.ref.Reference;
+import java.net.*;
+import java.util.LinkedList;
+import java.nio.Buffer;
 import java.nio.ByteBuffer;
 import java.nio.channels.*;
 
@@ -15,13 +19,7 @@ import java.nio.channels.*;
 public class NioPeerHandler {
     private static class Peer {
         SocketDescriptor descriptor;
-        // When we are told by LDK to disconnect, we can't return to LDK until we are sure
-        // won't call any more read/write PeerManager functions with the same connection.
-        // This is set to true if we're in such a condition (with disconnect checked
-        // before with the Peer monitor lock held) and false when we can return.
-        boolean block_disconnect_socket = false;
-        // Indicates LDK told us to disconnect this peer, and thus we should not call socket_disconnected.
-        boolean disconnect_requested = false;
+        long descriptor_raw_pointer;
         SelectionKey key;
     }
 
@@ -50,6 +48,19 @@ public class NioPeerHandler {
         }
     }
 
+    static private Field CommonBasePointer;
+    static {
+        try {
+            Class c = PeerManager.class.getSuperclass();
+            CommonBasePointer = c.getDeclaredField("ptr");
+            CommonBasePointer.setAccessible(true);
+            long _dummy_check = CommonBasePointer.getLong(Ping.of((short)0, (short)0));
+        } catch (NoSuchFieldException | IllegalAccessException e) {
+            throw new IllegalArgumentException(
+                    "We currently use reflection to access protected fields as Java has no reasonable access controls", e);
+        }
+    }
+
     private Peer setup_socket(SocketChannel chan) throws IOException {
         chan.configureBlocking(false);
         // Lightning tends to send a number of small messages back and forth between peers quickly, which Nagle is
@@ -66,15 +77,16 @@ public class NioPeerHandler {
             @Override
             public long send_data(byte[] data, boolean resume_read) {
                 try {
-                    if (resume_read) {
-                        do_selector_action(() -> peer.key.interestOps(peer.key.interestOps() | SelectionKey.OP_READ));
-                    }
                     long written = chan.write(ByteBuffer.wrap(data));
                     if (written != data.length) {
-                        do_selector_action(() -> peer.key.interestOps(peer.key.interestOps() | SelectionKey.OP_WRITE));
-                    }
+                        do_selector_action(() -> peer.key.interestOps(
+                                                       (peer.key.interestOps() | SelectionKey.OP_WRITE) & (~SelectionKey.OP_READ)));
+                    } else if (resume_read) {
+                        do_selector_action(() -> peer.key.interestOps(
+                                                       (peer.key.interestOps() | SelectionKey.OP_READ) & (~SelectionKey.OP_WRITE)));
+                                       }
                     return written;
-                } catch (IOException e) {
+                } catch (IOException|CancelledKeyException ignored) {
                     // Most likely the socket is disconnected, let the background thread handle it.
                     return 0;
                 }
@@ -82,27 +94,23 @@ public class NioPeerHandler {
 
             @Override
             public void disconnect_socket() {
-                synchronized (peer) {
-                    peer.disconnect_requested = true;
-                }
                 try {
                     do_selector_action(() -> {
-                        peer.key.cancel();
+                        try { peer.key.cancel(); } catch (CancelledKeyException ignored) {}
                         peer.key.channel().close();
                     });
                 } catch (IOException ignored) { }
-                synchronized (peer) {
-                    while (peer.block_disconnect_socket) {
-                        try {
-                            peer.wait();
-                        } catch (InterruptedException ignored) { }
-                    }
-                }
             }
             @Override public boolean eq(SocketDescriptor other_arg) { return other_arg.hash() == our_id; }
             @Override public long hash() { return our_id; }
         });
         peer.descriptor = descriptor;
+        try {
+            peer.descriptor_raw_pointer = CommonBasePointer.getLong(descriptor);
+        } catch (IllegalAccessException e) {
+            throw new IllegalArgumentException(
+                    "We currently use reflection to access protected fields as Java has no reasonable access controls", e);
+        }
         return peer;
     }
 
@@ -112,9 +120,21 @@ public class NioPeerHandler {
     long socket_id;
     volatile boolean shutdown = false;
 
+    private static Option_NetAddressZ get_netaddr_from_sockaddr(SocketAddress sockaddr) {
+        if (sockaddr instanceof InetSocketAddress) {
+            InetAddress addr = ((InetSocketAddress) sockaddr).getAddress();
+            short port = (short) ((InetSocketAddress) sockaddr).getPort();
+            if (addr instanceof Inet4Address) {
+                return Option_NetAddressZ.some(NetAddress.ipv4(addr.getAddress(), port));
+            } else if (addr instanceof Inet6Address) {
+                return Option_NetAddressZ.some(NetAddress.ipv6(addr.getAddress(), port));
+            }
+        }
+        return Option_NetAddressZ.none();
+    }
+
     /**
      * Constructs a new peer handler, spawning a thread to monitor for socket events.
-     * The background thread will call the PeerManager's timer_tick_occured() function for you on an appropriate schedule.
      *
      * @param manager The LDK PeerManager which connection data will be provided to.
      * @throws IOException If an internal java.nio error occurs.
@@ -123,8 +143,16 @@ public class NioPeerHandler {
         this.peer_manager = manager;
         this.selector = Selector.open();
         io_thread = new Thread(() -> {
-            ByteBuffer buf = ByteBuffer.allocate(8192);
-            long lastTimerTick = System.currentTimeMillis();
+            int BUF_SZ = 16 * 1024;
+            byte[] max_buf_byte_object = new byte[BUF_SZ];
+            ByteBuffer buf = ByteBuffer.allocate(BUF_SZ);
+
+            long peer_manager_raw_pointer;
+            try {
+                peer_manager_raw_pointer = CommonBasePointer.getLong(this.peer_manager);
+            } catch (IllegalAccessException e) {
+                throw new RuntimeException(e);
+            }
             while (true) {
                 try {
                     if (IS_ANDROID) {
@@ -159,74 +187,86 @@ public class NioPeerHandler {
                                 if (chan == null) continue;
                                 try {
                                     Peer peer = setup_socket(chan);
-                                    Result_NonePeerHandleErrorZ res = this.peer_manager.new_inbound_connection(peer.descriptor);
-                                    if (res instanceof Result_NonePeerHandleErrorZ.Result_NonePeerHandleErrorZ_OK) {
-                                        peer.key = chan.register(this.selector, SelectionKey.OP_READ, peer);
-                                    }
+                                    peer.key = chan.register(this.selector, SelectionKey.OP_READ, peer);
+                                    Option_NetAddressZ netaddr = get_netaddr_from_sockaddr(chan.getRemoteAddress());
+                                    Result_NonePeerHandleErrorZ res = this.peer_manager.new_inbound_connection(peer.descriptor, netaddr);
+                                    if (res instanceof Result_NonePeerHandleErrorZ.Result_NonePeerHandleErrorZ_Err) {
+                                                                               peer.descriptor.disconnect_socket();
+                                                                       }
                                 } catch (IOException ignored) { }
                             }
                             continue; // There is no attachment so the rest of the loop is useless
                         }
                         Peer peer = (Peer) key.attachment();
-                        synchronized (peer) {
-                            if (peer.disconnect_requested)
-                                continue;
-                            peer.block_disconnect_socket = true;
-                        }
                         try {
                             if (key.isValid() && (key.interestOps() & SelectionKey.OP_WRITE) != 0 && key.isWritable()) {
                                 Result_NonePeerHandleErrorZ res = this.peer_manager.write_buffer_space_avail(peer.descriptor);
                                 if (res instanceof Result_NonePeerHandleErrorZ.Result_NonePeerHandleErrorZ_Err) {
-                                    key.channel().close();
                                     key.cancel();
+                                    key.channel().close();
                                 }
                             }
                             if (key.isValid() && (key.interestOps() & SelectionKey.OP_READ) != 0 && key.isReadable()) {
-                                buf.clear();
+                                ((Buffer)buf).clear();
                                 int read = ((SocketChannel) key.channel()).read(buf);
                                 if (read == -1) {
                                     this.peer_manager.socket_disconnected(peer.descriptor);
                                     key.cancel();
+                                    key.channel().close(); // This may throw, we read -1 so the channel should already be closed, but do this to be safe
                                 } else if (read > 0) {
-                                    buf.flip();
-                                    byte[] read_bytes = new byte[read];
+                                    ((Buffer)buf).flip();
+                                    // This code is quite hot during initial network graph sync, so we go a ways out of
+                                    // our way to avoid object allocations that'll make the GC sweat later -
+                                    // * when we're hot, we'll likely often be reading the full buffer, so we keep
+                                    //   around a full-buffer-sized byte array to reuse across reads,
+                                    // * We use the manual memory management call logic directly in bindings instead of
+                                    //   the nice "human-readable" wrappers. This puts us at risk of memory issues,
+                                    //   so we indirectly ensure compile fails if the types change by writing the
+                                    //   "human-readable" form of the same code in the dummy function below.
+                                    byte[] read_bytes;
+                                    if (read == BUF_SZ) {
+                                        read_bytes = max_buf_byte_object;
+                                    } else {
+                                        read_bytes = new byte[read];
+                                    }
                                     buf.get(read_bytes, 0, read);
-                                    Result_boolPeerHandleErrorZ res = this.peer_manager.read_event(peer.descriptor, read_bytes);
-                                    if (res instanceof Result_boolPeerHandleErrorZ.Result_boolPeerHandleErrorZ_OK) {
-                                        if (((Result_boolPeerHandleErrorZ.Result_boolPeerHandleErrorZ_OK) res).res) {
+                                    long read_result_pointer = bindings.PeerManager_read_event(
+                                            peer_manager_raw_pointer, peer.descriptor_raw_pointer, read_bytes);
+                                    if (bindings.CResult_boolPeerHandleErrorZ_is_ok(read_result_pointer)) {
+                                        if (bindings.CResult_boolPeerHandleErrorZ_get_ok(read_result_pointer)) {
                                             key.interestOps(key.interestOps() & (~SelectionKey.OP_READ));
                                         }
                                     } else {
-                                        key.channel().close();
                                         key.cancel();
+                                        key.channel().close();
                                     }
+                                    bindings.CResult_boolPeerHandleErrorZ_free(read_result_pointer);
                                 }
                             }
                         } catch (IOException ignored) {
-                            try { key.channel().close(); } catch (IOException ignored2) { }
                             key.cancel();
+                            try { key.channel().close(); } catch (IOException ignored2) { }
                             peer_manager.socket_disconnected(peer.descriptor);
                         }
-                        synchronized (peer) {
-                            peer.block_disconnect_socket = false;
-                            peer.notifyAll();
-                        }
                     } catch (CancelledKeyException e) {
                         try { key.channel().close(); } catch (IOException ignored) { }
                         // The key is only cancelled when we have notified the PeerManager that the socket is closed, so
                         // no need to do anything here with the PeerManager.
                     }
                 }
-                if (lastTimerTick < System.currentTimeMillis() - 30 * 1000) {
-                    peer_manager.timer_tick_occurred();
-                    lastTimerTick = System.currentTimeMillis();
-                }
                 peer_manager.process_events();
             }
         }, "NioPeerHandler NIO Thread");
         io_thread.start();
     }
 
+    // Ensure the types used in the above manual code match what they were when the code was written.
+    // Ensure the above manual bindings.* code changes if this fails to compile.
+    private void dummy_check_return_type_matches_manual_memory_code_above(Peer peer) {
+        byte[] read_bytes = new byte[32];
+        Result_boolPeerHandleErrorZ res = this.peer_manager.read_event(peer.descriptor, read_bytes);
+    }
+
     /**
      * Connect to a peer given their node id and socket address. Blocks until a connection is established (or returns
      * IOException) and then the connection handling runs in the background.
@@ -239,28 +279,53 @@ public class NioPeerHandler {
      */
     public void connect(byte[] their_node_id, SocketAddress remote, int timeout_ms) throws IOException {
         SocketChannel chan = SocketChannel.open();
-        chan.configureBlocking(false);
-        Selector open_selector = Selector.open();
-        chan.register(open_selector, SelectionKey.OP_CONNECT);
-        if (!chan.connect(remote)) {
-            open_selector.select(timeout_ms);
+        boolean connected;
+        try {
+            chan.configureBlocking(false);
+            Selector open_selector = Selector.open();
+            chan.register(open_selector, SelectionKey.OP_CONNECT);
+            if (!chan.connect(remote)) {
+                open_selector.select(timeout_ms);
+            }
+            connected = chan.finishConnect();
+        } catch (IOException e) {
+            try { chan.close(); } catch (IOException _e) { }
+            throw e;
         }
-        if (!chan.finishConnect()) { // Note that this may throw its own IOException if we failed for another reason
+        if (!connected) {
+            try { chan.close(); } catch (IOException _e) { }
             throw new IOException("Timed out");
         }
         Peer peer = setup_socket(chan);
-        Result_CVec_u8ZPeerHandleErrorZ res = this.peer_manager.new_outbound_connection(their_node_id, peer.descriptor);
+        do_selector_action(() -> peer.key = chan.register(this.selector, SelectionKey.OP_READ, peer));
+        Result_CVec_u8ZPeerHandleErrorZ res = this.peer_manager.new_outbound_connection(their_node_id, peer.descriptor, get_netaddr_from_sockaddr(remote));
         if (res instanceof  Result_CVec_u8ZPeerHandleErrorZ.Result_CVec_u8ZPeerHandleErrorZ_OK) {
             byte[] initial_bytes = ((Result_CVec_u8ZPeerHandleErrorZ.Result_CVec_u8ZPeerHandleErrorZ_OK) res).res;
             if (chan.write(ByteBuffer.wrap(initial_bytes)) != initial_bytes.length) {
+                peer.descriptor.disconnect_socket();
+                this.peer_manager.socket_disconnected(peer.descriptor);
                 throw new IOException("We assume TCP socket buffer is at least a single packet in length");
             }
-            do_selector_action(() -> peer.key = chan.register(this.selector, SelectionKey.OP_READ, peer));
         } else {
+            peer.descriptor.disconnect_socket();
             throw new IOException("LDK rejected outbound connection. This likely shouldn't ever happen.");
         }
     }
 
+    /**
+     * Disconnects any connections currently open with the peer with the given node id.
+     *
+     * @param their_node_id must be a valid 33-byte public key
+     */
+    public void disconnect(byte[] their_node_id) {
+        this.peer_manager.disconnect_by_node_id(their_node_id, false);
+    }
+
+    /**
+     * Before shutdown, we have to ensure all of our listening sockets are closed manually, as they appear
+     * to otherwise remain open and lying around on OSX (though no other platform).
+     */
+    private LinkedList<ServerSocketChannel> listening_sockets = new LinkedList();
     /**
      * Binds a listening socket to the given address, accepting incoming connections and handling them on the background
      * thread.
@@ -270,21 +335,37 @@ public class NioPeerHandler {
      */
     public void bind_listener(SocketAddress socket_address) throws IOException {
         ServerSocketChannel listen_channel = ServerSocketChannel.open();
+        listen_channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
         listen_channel.bind(socket_address);
         listen_channel.configureBlocking(false);
         do_selector_action(() -> listen_channel.register(this.selector, SelectionKey.OP_ACCEPT));
+        synchronized(listening_sockets) {
+            listening_sockets.add(listen_channel);
+        }
     }
 
     /**
-     * Interrupt the background thread, stopping all peer handling. Disconnection events to the PeerHandler are not made,
-     * potentially leaving the PeerHandler in an inconsistent state.
+     * Interrupt the background thread, stopping all peer handling.
+     *
+     * After this method is called, the behavior of future calls to methods on this NioPeerHandler are undefined.
      */
     public void interrupt() {
+        this.peer_manager.disconnect_all_peers();
         shutdown = true;
         selector.wakeup();
         try {
             io_thread.join();
         } catch (InterruptedException ignored) { }
+        synchronized(listening_sockets) {
+            try {
+                selector.close();
+                for (ServerSocketChannel chan : listening_sockets) {
+                    chan.close();
+                }
+                listening_sockets.clear();
+            } catch (IOException ignored) {}
+        }
+        Reference.reachabilityFence(this.peer_manager); // Almost certainly overkill, but no harm in it
     }
 
     /**
@@ -294,4 +375,4 @@ public class NioPeerHandler {
     public void check_events() {
         selector.wakeup();
     }
-}
\ No newline at end of file
+}