1 package org.ldk.batteries;
3 import org.ldk.structs.*;
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.*;
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.
17 public class NioPeerHandler {
18 private static class Peer {
19 SocketDescriptor descriptor;
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;
29 IS_ANDROID = System.getProperty("java.vendor").toLowerCase().contains("android");
31 private boolean wakeup_selector = false;
32 private interface SelectorCall {
33 void meth() throws IOException;
35 private void do_selector_action(SelectorCall meth) throws IOException {
37 wakeup_selector = true;
38 this.selector.wakeup();
39 synchronized (this.selector) {
41 wakeup_selector = false;
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);
55 this.socket_id = this.socket_id + 1;
56 our_id = this.socket_id;
59 final Peer peer = new Peer();
60 SocketDescriptor descriptor = SocketDescriptor.new_impl(new SocketDescriptor.SocketDescriptorInterface() {
62 public long send_data(byte[] data, boolean resume_read) {
65 do_selector_action(() -> peer.key.interestOps(peer.key.interestOps() | SelectionKey.OP_READ));
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));
72 } catch (IOException e) {
73 // Most likely the socket is disconnected, let the background thread handle it.
79 public void disconnect_socket() {
81 do_selector_action(() -> {
83 peer.key.channel().close();
85 } catch (IOException ignored) { }
87 @Override public boolean eq(SocketDescriptor other_arg) { return other_arg.hash() == our_id; }
88 @Override public long hash() { return our_id; }
90 peer.descriptor = descriptor;
94 PeerManager peer_manager;
96 final Selector selector;
98 volatile boolean shutdown = false;
101 * Constructs a new peer handler, spawning a thread to monitor for socket events.
103 * @param manager The LDK PeerManager which connection data will be provided to.
104 * @throws IOException If an internal java.nio error occurs.
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);
115 synchronized (this.selector) {
116 if (!wakeup_selector) {
117 this.selector.select(1000);
123 this.selector.select(1000);
125 } catch (IOException ignored) {
126 System.err.println("java.nio threw an unexpected IOException. Stopping PeerHandler thread!");
129 if (shutdown) return;
130 if (Thread.interrupted()) return;
131 for (SelectionKey key : this.selector.selectedKeys()) {
133 if ((key.interestOps() & SelectionKey.OP_ACCEPT) != 0) {
134 if (key.isAcceptable()) {
137 chan = ((ServerSocketChannel) key.channel()).accept();
138 } catch (IOException ignored) {
142 if (chan == null) continue;
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);
149 } catch (IOException ignored) { }
151 continue; // There is no attachment so the rest of the loop is useless
153 Peer peer = (Peer) key.attachment();
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();
162 if (key.isValid() && (key.interestOps() & SelectionKey.OP_READ) != 0 && key.isReadable()) {
163 ((Buffer)buf).clear();
164 int read = ((SocketChannel) key.channel()).read(buf);
166 this.peer_manager.socket_disconnected(peer.descriptor);
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));
178 key.channel().close();
183 } catch (IOException ignored) {
184 try { key.channel().close(); } catch (IOException ignored2) { }
186 peer_manager.socket_disconnected(peer.descriptor);
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.
194 peer_manager.process_events();
196 }, "NioPeerHandler NIO Thread");
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.
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.
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);
218 if (!chan.finishConnect()) { // Note that this may throw its own IOException if we failed for another reason
219 throw new IOException("Timed out");
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");
228 do_selector_action(() -> peer.key = chan.register(this.selector, SelectionKey.OP_READ, peer));
230 throw new IOException("LDK rejected outbound connection. This likely shouldn't ever happen.");
235 * Disconnects any connections currently open with the peer with the given node id.
237 * @param their_node_id must be a valid 33-byte public key
239 public void disconnect(byte[] their_node_id) {
240 this.peer_manager.disconnect_by_node_id(their_node_id, false);
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).
247 private LinkedList<ServerSocketChannel> listening_sockets = new LinkedList();
249 * Binds a listening socket to the given address, accepting incoming connections and handling them on the background
252 * @param socket_address The address to bind the listening socket to.
253 * @throws IOException if binding the listening socket fail.
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);
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.
270 public void interrupt() {
275 } catch (InterruptedException ignored) { }
276 synchronized(listening_sockets) {
279 for (ServerSocketChannel chan : listening_sockets) {
282 } catch (IOException ignored) {}
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.
290 public void check_events() {