@@ -189,10 +189,20 @@ public NioPeerHandler(PeerManager manager) throws IOException {
189189 * @param their_node_id A valid 33-byte public key representing the peer's Lightning Node ID. If this is invalid,
190190 * undefined behavior (read: Segfault, etc) may occur.
191191 * @param remote The socket address to connect to.
192+ * @param timeout_ms The amount of time, in milliseconds, up to which we will wait for connection to complete.
192193 * @throws IOException If connecting to the remote endpoint fails or internal java.nio errors occur.
193194 */
194- public void connect (byte [] their_node_id , SocketAddress remote ) throws IOException {
195- SocketChannel chan = SocketChannel .open (remote );
195+ public void connect (byte [] their_node_id , SocketAddress remote , int timeout_ms ) throws IOException {
196+ SocketChannel chan = SocketChannel .open ();
197+ chan .configureBlocking (false );
198+ Selector open_selector = Selector .open ();
199+ chan .register (open_selector , SelectionKey .OP_CONNECT );
200+ if (!chan .connect (remote )) {
201+ open_selector .select (timeout_ms );
202+ }
203+ if (!chan .finishConnect ()) { // Note that this may throw its own IOException if we failed for another reason
204+ throw new IOException ("Timed out" );
205+ }
196206 Peer peer = setup_socket (chan );
197207 Result_CVec_u8ZPeerHandleErrorZ res = this .peer_manager .new_outbound_connection (their_node_id , peer .descriptor );
198208 if (res instanceof Result_CVec_u8ZPeerHandleErrorZ .Result_CVec_u8ZPeerHandleErrorZ_OK ) {
0 commit comments