import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
+/**
+ * A NioPeerHandler maps LDK's PeerHandler to Java's NIO I/O interface. It spawns a single background thread which
+ * processes socket events and provides the data to LDK for decryption and processing.
+ */
public class NioPeerHandler {
private static class Peer {
SocketDescriptor descriptor;
public long send_data(byte[] data, boolean resume_read) {
if (resume_read) {
peer.key.interestOps(peer.key.interestOps() | SelectionKey.OP_READ);
+ selector.wakeup();
}
try {
long written = chan.write(ByteBuffer.wrap(data));
if (written != data.length) {
peer.key.interestOps(peer.key.interestOps() | SelectionKey.OP_WRITE);
+ selector.wakeup();
}
return written;
} catch (IOException e) {
* @throws IOException If an internal java.nio error occurs.
*/
public NioPeerHandler(PeerManager manager) throws IOException {
-long id = manager._test_only_get_ptr();
this.peer_manager = manager;
this.selector = Selector.open();
io_thread = new Thread(() -> {
if (use_nio_peer_handler) {
peer1.nio_peer_handler.check_events();
peer2.nio_peer_handler.check_events();
- try { Thread.sleep(500); } catch (InterruptedException e) { assert false; }
+ try { Thread.sleep(400); } catch (InterruptedException e) { assert false; }
} else {
synchronized (runqueue) {
ran = false;
}
if (use_nio_peer_handler) {
+ state.peer1.peer_manager.disconnect_by_node_id(state.peer2.chan_manager.get_our_node_id(), false);
+ wait_events_processed(state.peer1, state.peer2);
+ assert state.peer1.peer_manager.get_peer_node_ids().length == 0;
+ assert state.peer2.peer_manager.get_peer_node_ids().length == 0;
state.peer1.nio_peer_handler.interrupt();
state.peer2.nio_peer_handler.interrupt();
}