Ensure listening sockets are closed to work around OSX breaking
[ldk-java] / src / main / java / org / ldk / batteries / NioPeerHandler.java
1 package org.ldk.batteries;
2
3 import org.ldk.structs.*;
4
5 import java.io.IOException;
6 import java.util.LinkedList;
7 import java.net.SocketAddress;
8 import java.net.StandardSocketOptions;
9 import java.nio.Buffer;
10 import java.nio.ByteBuffer;
11 import java.nio.channels.*;
12
13 /**
14  * A NioPeerHandler maps LDK's PeerHandler to Java's NIO I/O interface. It spawns a single background thread which
15  * processes socket events and provides the data to LDK for decryption and processing.
16  */
17 public class NioPeerHandler {
18     private static class Peer {
19         SocketDescriptor descriptor;
20         SelectionKey key;
21     }
22
23     // Android's java.nio implementation has a big lock inside the selector, preventing any concurrent access to it.
24     // This appears to largely defeat the entire purpose of java.nio, but we work around it here by explicitly checking
25     // for an Android environment and passing any selector access on any thread other than our internal one through
26     // do_selector_action, which wakes up the selector before accessing it.
27     private static boolean IS_ANDROID;
28     static {
29         IS_ANDROID = System.getProperty("java.vendor").toLowerCase().contains("android");
30     }
31     private boolean wakeup_selector = false;
32     private interface SelectorCall {
33         void meth() throws IOException;
34     }
35     private void do_selector_action(SelectorCall meth) throws IOException {
36         if (IS_ANDROID) {
37             wakeup_selector = true;
38             this.selector.wakeup();
39             synchronized (this.selector) {
40                 meth.meth();
41                 wakeup_selector = false;
42             }
43         } else {
44             meth.meth();
45         }
46     }
47
48     private Peer setup_socket(SocketChannel chan) throws IOException {
49         chan.configureBlocking(false);
50         // Lightning tends to send a number of small messages back and forth between peers quickly, which Nagle is
51         // particularly bad at handling, so we disable it here.
52         chan.setOption(StandardSocketOptions.TCP_NODELAY, true);
53         long our_id;
54         synchronized (this) {
55             this.socket_id = this.socket_id + 1;
56             our_id = this.socket_id;
57         }
58
59         final Peer peer = new Peer();
60         SocketDescriptor descriptor = SocketDescriptor.new_impl(new SocketDescriptor.SocketDescriptorInterface() {
61             @Override
62             public long send_data(byte[] data, boolean resume_read) {
63                 try {
64                     if (resume_read) {
65                         do_selector_action(() -> peer.key.interestOps(peer.key.interestOps() | SelectionKey.OP_READ));
66                     }
67                     long written = chan.write(ByteBuffer.wrap(data));
68                     if (written != data.length) {
69                         do_selector_action(() -> peer.key.interestOps(peer.key.interestOps() | SelectionKey.OP_WRITE));
70                     }
71                     return written;
72                 } catch (IOException e) {
73                     // Most likely the socket is disconnected, let the background thread handle it.
74                     return 0;
75                 }
76             }
77
78             @Override
79             public void disconnect_socket() {
80                 try {
81                     do_selector_action(() -> {
82                         peer.key.cancel();
83                         peer.key.channel().close();
84                     });
85                 } catch (IOException ignored) { }
86             }
87             @Override public boolean eq(SocketDescriptor other_arg) { return other_arg.hash() == our_id; }
88             @Override public long hash() { return our_id; }
89         });
90         peer.descriptor = descriptor;
91         return peer;
92     }
93
94     PeerManager peer_manager;
95     Thread io_thread;
96     final Selector selector;
97     long socket_id;
98     volatile boolean shutdown = false;
99
100     /**
101      * Constructs a new peer handler, spawning a thread to monitor for socket events.
102      *
103      * @param manager The LDK PeerManager which connection data will be provided to.
104      * @throws IOException If an internal java.nio error occurs.
105      */
106     public NioPeerHandler(PeerManager manager) throws IOException {
107         this.peer_manager = manager;
108         this.selector = Selector.open();
109         io_thread = new Thread(() -> {
110             ByteBuffer buf = ByteBuffer.allocate(8192);
111             while (true) {
112                 try {
113                     if (IS_ANDROID) {
114                         while (true) {
115                             synchronized (this.selector) {
116                                 if (!wakeup_selector) {
117                                     this.selector.select(1000);
118                                     break;
119                                 }
120                             }
121                         }
122                     } else {
123                         this.selector.select(1000);
124                     }
125                 } catch (IOException ignored) {
126                     System.err.println("java.nio threw an unexpected IOException. Stopping PeerHandler thread!");
127                     return;
128                 }
129                 if (shutdown) return;
130                 if (Thread.interrupted()) return;
131                 for (SelectionKey key : this.selector.selectedKeys()) {
132                     try {
133                         if ((key.interestOps() & SelectionKey.OP_ACCEPT) != 0) {
134                             if (key.isAcceptable()) {
135                                 SocketChannel chan;
136                                 try {
137                                     chan = ((ServerSocketChannel) key.channel()).accept();
138                                 } catch (IOException ignored) {
139                                     key.cancel();
140                                     continue;
141                                 }
142                                 if (chan == null) continue;
143                                 try {
144                                     Peer peer = setup_socket(chan);
145                                     Result_NonePeerHandleErrorZ res = this.peer_manager.new_inbound_connection(peer.descriptor);
146                                     if (res instanceof Result_NonePeerHandleErrorZ.Result_NonePeerHandleErrorZ_OK) {
147                                         peer.key = chan.register(this.selector, SelectionKey.OP_READ, peer);
148                                     }
149                                 } catch (IOException ignored) { }
150                             }
151                             continue; // There is no attachment so the rest of the loop is useless
152                         }
153                         Peer peer = (Peer) key.attachment();
154                         try {
155                             if (key.isValid() && (key.interestOps() & SelectionKey.OP_WRITE) != 0 && key.isWritable()) {
156                                 Result_NonePeerHandleErrorZ res = this.peer_manager.write_buffer_space_avail(peer.descriptor);
157                                 if (res instanceof Result_NonePeerHandleErrorZ.Result_NonePeerHandleErrorZ_Err) {
158                                     key.channel().close();
159                                     key.cancel();
160                                 }
161                             }
162                             if (key.isValid() && (key.interestOps() & SelectionKey.OP_READ) != 0 && key.isReadable()) {
163                                 ((Buffer)buf).clear();
164                                 int read = ((SocketChannel) key.channel()).read(buf);
165                                 if (read == -1) {
166                                     this.peer_manager.socket_disconnected(peer.descriptor);
167                                     key.cancel();
168                                 } else if (read > 0) {
169                                     ((Buffer)buf).flip();
170                                     byte[] read_bytes = new byte[read];
171                                     buf.get(read_bytes, 0, read);
172                                     Result_boolPeerHandleErrorZ res = this.peer_manager.read_event(peer.descriptor, read_bytes);
173                                     if (res instanceof Result_boolPeerHandleErrorZ.Result_boolPeerHandleErrorZ_OK) {
174                                         if (((Result_boolPeerHandleErrorZ.Result_boolPeerHandleErrorZ_OK) res).res) {
175                                             key.interestOps(key.interestOps() & (~SelectionKey.OP_READ));
176                                         }
177                                     } else {
178                                         key.channel().close();
179                                         key.cancel();
180                                     }
181                                 }
182                             }
183                         } catch (IOException ignored) {
184                             try { key.channel().close(); } catch (IOException ignored2) { }
185                             key.cancel();
186                             peer_manager.socket_disconnected(peer.descriptor);
187                         }
188                     } catch (CancelledKeyException e) {
189                         try { key.channel().close(); } catch (IOException ignored) { }
190                         // The key is only cancelled when we have notified the PeerManager that the socket is closed, so
191                         // no need to do anything here with the PeerManager.
192                     }
193                 }
194                 peer_manager.process_events();
195             }
196         }, "NioPeerHandler NIO Thread");
197         io_thread.start();
198     }
199
200     /**
201      * Connect to a peer given their node id and socket address. Blocks until a connection is established (or returns
202      * IOException) and then the connection handling runs in the background.
203      *
204      * @param their_node_id A valid 33-byte public key representing the peer's Lightning Node ID. If this is invalid,
205      *                      undefined behavior (read: Segfault, etc) may occur.
206      * @param remote The socket address to connect to.
207      * @param timeout_ms The amount of time, in milliseconds, up to which we will wait for connection to complete.
208      * @throws IOException If connecting to the remote endpoint fails or internal java.nio errors occur.
209      */
210     public void connect(byte[] their_node_id, SocketAddress remote, int timeout_ms) throws IOException {
211         SocketChannel chan = SocketChannel.open();
212         chan.configureBlocking(false);
213         Selector open_selector = Selector.open();
214         chan.register(open_selector, SelectionKey.OP_CONNECT);
215         if (!chan.connect(remote)) {
216             open_selector.select(timeout_ms);
217         }
218         if (!chan.finishConnect()) { // Note that this may throw its own IOException if we failed for another reason
219             throw new IOException("Timed out");
220         }
221         Peer peer = setup_socket(chan);
222         Result_CVec_u8ZPeerHandleErrorZ res = this.peer_manager.new_outbound_connection(their_node_id, peer.descriptor);
223         if (res instanceof  Result_CVec_u8ZPeerHandleErrorZ.Result_CVec_u8ZPeerHandleErrorZ_OK) {
224             byte[] initial_bytes = ((Result_CVec_u8ZPeerHandleErrorZ.Result_CVec_u8ZPeerHandleErrorZ_OK) res).res;
225             if (chan.write(ByteBuffer.wrap(initial_bytes)) != initial_bytes.length) {
226                 throw new IOException("We assume TCP socket buffer is at least a single packet in length");
227             }
228             do_selector_action(() -> peer.key = chan.register(this.selector, SelectionKey.OP_READ, peer));
229         } else {
230             throw new IOException("LDK rejected outbound connection. This likely shouldn't ever happen.");
231         }
232     }
233
234     /**
235      * Disconnects any connections currently open with the peer with the given node id.
236      *
237      * @param their_node_id must be a valid 33-byte public key
238      */
239     public void disconnect(byte[] their_node_id) {
240         this.peer_manager.disconnect_by_node_id(their_node_id, false);
241     }
242
243     /**
244      * Before shutdown, we have to ensure all of our listening sockets are closed manually, as they appear
245      * to otherwise remain open and lying around on OSX (though no other platform).
246      */
247     private LinkedList<ServerSocketChannel> listening_sockets = new LinkedList();
248     /**
249      * Binds a listening socket to the given address, accepting incoming connections and handling them on the background
250      * thread.
251      *
252      * @param socket_address The address to bind the listening socket to.
253      * @throws IOException if binding the listening socket fail.
254      */
255     public void bind_listener(SocketAddress socket_address) throws IOException {
256         ServerSocketChannel listen_channel = ServerSocketChannel.open();
257         listen_channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
258         listen_channel.bind(socket_address);
259         listen_channel.configureBlocking(false);
260         do_selector_action(() -> listen_channel.register(this.selector, SelectionKey.OP_ACCEPT));
261         synchronized(listening_sockets) {
262             listening_sockets.add(listen_channel);
263         }
264     }
265
266     /**
267      * Interrupt the background thread, stopping all peer handling. Disconnection events to the PeerHandler are not made,
268      * potentially leaving the PeerHandler in an inconsistent state.
269      */
270     public void interrupt() {
271         shutdown = true;
272         selector.wakeup();
273         try {
274             io_thread.join();
275         } catch (InterruptedException ignored) { }
276         synchronized(listening_sockets) {
277             try {
278                 selector.close();
279                 for (ServerSocketChannel chan : listening_sockets) {
280                     chan.close();
281                 }
282             } catch (IOException ignored) {}
283         }
284     }
285
286     /**
287      * Calls process_events on the PeerManager immediately. Normally process_events is polled regularly to check for new
288      * messages which need to be sent, but you can interrupt the poll and check immediately by calling this function.
289      */
290     public void check_events() {
291         selector.wakeup();
292     }
293 }