[NioPeerHandler] Un-set Read interest when we fail to write fully
[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                     long written = chan.write(ByteBuffer.wrap(data));
65                     if (written != data.length) {
66                         do_selector_action(() -> peer.key.interestOps(
67                                                         (peer.key.interestOps() | SelectionKey.OP_WRITE) & (~SelectionKey.OP_READ)));
68                     } else if (resume_read) {
69                         do_selector_action(() -> peer.key.interestOps(
70                                                         (peer.key.interestOps() | SelectionKey.OP_READ) & (~SelectionKey.OP_WRITE)));
71                                         }
72                     return written;
73                 } catch (IOException e) {
74                     // Most likely the socket is disconnected, let the background thread handle it.
75                     return 0;
76                 }
77             }
78
79             @Override
80             public void disconnect_socket() {
81                 try {
82                     do_selector_action(() -> {
83                         peer.key.cancel();
84                         peer.key.channel().close();
85                     });
86                 } catch (IOException ignored) { }
87             }
88             @Override public boolean eq(SocketDescriptor other_arg) { return other_arg.hash() == our_id; }
89             @Override public long hash() { return our_id; }
90         });
91         peer.descriptor = descriptor;
92         return peer;
93     }
94
95     PeerManager peer_manager;
96     Thread io_thread;
97     final Selector selector;
98     long socket_id;
99     volatile boolean shutdown = false;
100
101     /**
102      * Constructs a new peer handler, spawning a thread to monitor for socket events.
103      *
104      * @param manager The LDK PeerManager which connection data will be provided to.
105      * @throws IOException If an internal java.nio error occurs.
106      */
107     public NioPeerHandler(PeerManager manager) throws IOException {
108         this.peer_manager = manager;
109         this.selector = Selector.open();
110         io_thread = new Thread(() -> {
111             ByteBuffer buf = ByteBuffer.allocate(8192);
112             while (true) {
113                 try {
114                     if (IS_ANDROID) {
115                         while (true) {
116                             synchronized (this.selector) {
117                                 if (!wakeup_selector) {
118                                     this.selector.select(1000);
119                                     break;
120                                 }
121                             }
122                         }
123                     } else {
124                         this.selector.select(1000);
125                     }
126                 } catch (IOException ignored) {
127                     System.err.println("java.nio threw an unexpected IOException. Stopping PeerHandler thread!");
128                     return;
129                 }
130                 if (shutdown) return;
131                 if (Thread.interrupted()) return;
132                 for (SelectionKey key : this.selector.selectedKeys()) {
133                     try {
134                         if ((key.interestOps() & SelectionKey.OP_ACCEPT) != 0) {
135                             if (key.isAcceptable()) {
136                                 SocketChannel chan;
137                                 try {
138                                     chan = ((ServerSocketChannel) key.channel()).accept();
139                                 } catch (IOException ignored) {
140                                     key.cancel();
141                                     continue;
142                                 }
143                                 if (chan == null) continue;
144                                 try {
145                                     Peer peer = setup_socket(chan);
146                                     Result_NonePeerHandleErrorZ res = this.peer_manager.new_inbound_connection(peer.descriptor);
147                                     if (res instanceof Result_NonePeerHandleErrorZ.Result_NonePeerHandleErrorZ_OK) {
148                                         peer.key = chan.register(this.selector, SelectionKey.OP_READ, peer);
149                                     }
150                                 } catch (IOException ignored) { }
151                             }
152                             continue; // There is no attachment so the rest of the loop is useless
153                         }
154                         Peer peer = (Peer) key.attachment();
155                         try {
156                             if (key.isValid() && (key.interestOps() & SelectionKey.OP_WRITE) != 0 && key.isWritable()) {
157                                 Result_NonePeerHandleErrorZ res = this.peer_manager.write_buffer_space_avail(peer.descriptor);
158                                 if (res instanceof Result_NonePeerHandleErrorZ.Result_NonePeerHandleErrorZ_Err) {
159                                     key.channel().close();
160                                     key.cancel();
161                                 }
162                             }
163                             if (key.isValid() && (key.interestOps() & SelectionKey.OP_READ) != 0 && key.isReadable()) {
164                                 ((Buffer)buf).clear();
165                                 int read = ((SocketChannel) key.channel()).read(buf);
166                                 if (read == -1) {
167                                     this.peer_manager.socket_disconnected(peer.descriptor);
168                                     key.cancel();
169                                 } else if (read > 0) {
170                                     ((Buffer)buf).flip();
171                                     byte[] read_bytes = new byte[read];
172                                     buf.get(read_bytes, 0, read);
173                                     Result_boolPeerHandleErrorZ res = this.peer_manager.read_event(peer.descriptor, read_bytes);
174                                     if (res instanceof Result_boolPeerHandleErrorZ.Result_boolPeerHandleErrorZ_OK) {
175                                         if (((Result_boolPeerHandleErrorZ.Result_boolPeerHandleErrorZ_OK) res).res) {
176                                             key.interestOps(key.interestOps() & (~SelectionKey.OP_READ));
177                                         }
178                                     } else {
179                                         key.channel().close();
180                                         key.cancel();
181                                     }
182                                 }
183                             }
184                         } catch (IOException ignored) {
185                             try { key.channel().close(); } catch (IOException ignored2) { }
186                             key.cancel();
187                             peer_manager.socket_disconnected(peer.descriptor);
188                         }
189                     } catch (CancelledKeyException e) {
190                         try { key.channel().close(); } catch (IOException ignored) { }
191                         // The key is only cancelled when we have notified the PeerManager that the socket is closed, so
192                         // no need to do anything here with the PeerManager.
193                     }
194                 }
195                 peer_manager.process_events();
196             }
197         }, "NioPeerHandler NIO Thread");
198         io_thread.start();
199     }
200
201     /**
202      * Connect to a peer given their node id and socket address. Blocks until a connection is established (or returns
203      * IOException) and then the connection handling runs in the background.
204      *
205      * @param their_node_id A valid 33-byte public key representing the peer's Lightning Node ID. If this is invalid,
206      *                      undefined behavior (read: Segfault, etc) may occur.
207      * @param remote The socket address to connect to.
208      * @param timeout_ms The amount of time, in milliseconds, up to which we will wait for connection to complete.
209      * @throws IOException If connecting to the remote endpoint fails or internal java.nio errors occur.
210      */
211     public void connect(byte[] their_node_id, SocketAddress remote, int timeout_ms) throws IOException {
212         SocketChannel chan = SocketChannel.open();
213         chan.configureBlocking(false);
214         Selector open_selector = Selector.open();
215         chan.register(open_selector, SelectionKey.OP_CONNECT);
216         if (!chan.connect(remote)) {
217             open_selector.select(timeout_ms);
218         }
219         if (!chan.finishConnect()) { // Note that this may throw its own IOException if we failed for another reason
220             throw new IOException("Timed out");
221         }
222         Peer peer = setup_socket(chan);
223         Result_CVec_u8ZPeerHandleErrorZ res = this.peer_manager.new_outbound_connection(their_node_id, peer.descriptor);
224         if (res instanceof  Result_CVec_u8ZPeerHandleErrorZ.Result_CVec_u8ZPeerHandleErrorZ_OK) {
225             byte[] initial_bytes = ((Result_CVec_u8ZPeerHandleErrorZ.Result_CVec_u8ZPeerHandleErrorZ_OK) res).res;
226             if (chan.write(ByteBuffer.wrap(initial_bytes)) != initial_bytes.length) {
227                 throw new IOException("We assume TCP socket buffer is at least a single packet in length");
228             }
229             do_selector_action(() -> peer.key = chan.register(this.selector, SelectionKey.OP_READ, peer));
230         } else {
231             throw new IOException("LDK rejected outbound connection. This likely shouldn't ever happen.");
232         }
233     }
234
235     /**
236      * Disconnects any connections currently open with the peer with the given node id.
237      *
238      * @param their_node_id must be a valid 33-byte public key
239      */
240     public void disconnect(byte[] their_node_id) {
241         this.peer_manager.disconnect_by_node_id(their_node_id, false);
242     }
243
244     /**
245      * Before shutdown, we have to ensure all of our listening sockets are closed manually, as they appear
246      * to otherwise remain open and lying around on OSX (though no other platform).
247      */
248     private LinkedList<ServerSocketChannel> listening_sockets = new LinkedList();
249     /**
250      * Binds a listening socket to the given address, accepting incoming connections and handling them on the background
251      * thread.
252      *
253      * @param socket_address The address to bind the listening socket to.
254      * @throws IOException if binding the listening socket fail.
255      */
256     public void bind_listener(SocketAddress socket_address) throws IOException {
257         ServerSocketChannel listen_channel = ServerSocketChannel.open();
258         listen_channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
259         listen_channel.bind(socket_address);
260         listen_channel.configureBlocking(false);
261         do_selector_action(() -> listen_channel.register(this.selector, SelectionKey.OP_ACCEPT));
262         synchronized(listening_sockets) {
263             listening_sockets.add(listen_channel);
264         }
265     }
266
267     /**
268      * Interrupt the background thread, stopping all peer handling. Disconnection events to the PeerHandler are not made,
269      * potentially leaving the PeerHandler in an inconsistent state.
270      */
271     public void interrupt() {
272         shutdown = true;
273         selector.wakeup();
274         try {
275             io_thread.join();
276         } catch (InterruptedException ignored) { }
277         synchronized(listening_sockets) {
278             try {
279                 selector.close();
280                 for (ServerSocketChannel chan : listening_sockets) {
281                     chan.close();
282                 }
283             } catch (IOException ignored) {}
284         }
285     }
286
287     /**
288      * Calls process_events on the PeerManager immediately. Normally process_events is polled regularly to check for new
289      * messages which need to be sent, but you can interrupt the poll and check immediately by calling this function.
290      */
291     public void check_events() {
292         selector.wakeup();
293     }
294 }