X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fldk%2Fbatteries%2FNioPeerHandler.java;fp=src%2Fmain%2Fjava%2Forg%2Fldk%2Fbatteries%2FNioPeerHandler.java;h=a77f1f0f49bb8cb4152d284b09f05c6b0f068116;hb=7df17e5d4ad0db3c9837301acd9062ba500e2218;hp=14c5611d5daa0e6dc74c9af0cd0f1144c6fcdfea;hpb=1b870a3ffab1c0024411e30102bc6d198300f095;p=ldk-java diff --git a/src/main/java/org/ldk/batteries/NioPeerHandler.java b/src/main/java/org/ldk/batteries/NioPeerHandler.java index 14c5611d..a77f1f0f 100644 --- a/src/main/java/org/ldk/batteries/NioPeerHandler.java +++ b/src/main/java/org/ldk/batteries/NioPeerHandler.java @@ -189,10 +189,20 @@ public class NioPeerHandler { * @param their_node_id A valid 33-byte public key representing the peer's Lightning Node ID. If this is invalid, * undefined behavior (read: Segfault, etc) may occur. * @param remote The socket address to connect to. + * @param timeout_ms The amount of time, in milliseconds, up to which we will wait for connection to complete. * @throws IOException If connecting to the remote endpoint fails or internal java.nio errors occur. */ - public void connect(byte[] their_node_id, SocketAddress remote) throws IOException { - SocketChannel chan = SocketChannel.open(remote); + 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); + } + if (!chan.finishConnect()) { // Note that this may throw its own IOException if we failed for another reason + 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); if (res instanceof Result_CVec_u8ZPeerHandleErrorZ.Result_CVec_u8ZPeerHandleErrorZ_OK) {