Skip to content

Commit

Permalink
Drop disconnect_socket blocking in NioPeerHandler for 0.0.99
Browse files Browse the repository at this point in the history
0.0.99 no longer requires that we block disconnect_socket and
simplifes our side of the SocketDescriptor interface substantially,
which we implement here.
  • Loading branch information
TheBlueMatt committed Jul 9, 2021
1 parent d62373c commit 0781835
Showing 1 changed file with 1 addition and 27 deletions.
28 changes: 1 addition & 27 deletions src/main/java/org/ldk/batteries/NioPeerHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@
public class NioPeerHandler {
private static class Peer {
SocketDescriptor descriptor;
// When we are told by LDK to disconnect, we can't return to LDK until we are sure
// won't call any more read/write PeerManager functions with the same connection.
// This is set to true if we're in such a condition (with disconnect checked
// before with the Peer monitor lock held) and false when we can return.
boolean block_disconnect_socket = false;
// Indicates LDK told us to disconnect this peer, and thus we should not call socket_disconnected.
boolean disconnect_requested = false;
SelectionKey key;
}

Expand Down Expand Up @@ -83,22 +76,12 @@ public long send_data(byte[] data, boolean resume_read) {

@Override
public void disconnect_socket() {
synchronized (peer) {
peer.disconnect_requested = true;
}
try {
do_selector_action(() -> {
peer.key.cancel();
peer.key.channel().close();
});
} catch (IOException ignored) { }
synchronized (peer) {
while (peer.block_disconnect_socket) {
try {
peer.wait();
} catch (InterruptedException ignored) { }
}
}
}
@Override public boolean eq(SocketDescriptor other_arg) { return other_arg.hash() == our_id; }
@Override public long hash() { return our_id; }
Expand Down Expand Up @@ -169,11 +152,6 @@ public NioPeerHandler(PeerManager manager) throws IOException {
continue; // There is no attachment so the rest of the loop is useless
}
Peer peer = (Peer) key.attachment();
synchronized (peer) {
if (peer.disconnect_requested)
continue;
peer.block_disconnect_socket = true;
}
try {
if (key.isValid() && (key.interestOps() & SelectionKey.OP_WRITE) != 0 && key.isWritable()) {
Result_NonePeerHandleErrorZ res = this.peer_manager.write_buffer_space_avail(peer.descriptor);
Expand Down Expand Up @@ -208,10 +186,6 @@ public NioPeerHandler(PeerManager manager) throws IOException {
key.cancel();
peer_manager.socket_disconnected(peer.descriptor);
}
synchronized (peer) {
peer.block_disconnect_socket = false;
peer.notifyAll();
}
} catch (CancelledKeyException e) {
try { key.channel().close(); } catch (IOException ignored) { }
// The key is only cancelled when we have notified the PeerManager that the socket is closed, so
Expand Down Expand Up @@ -295,4 +269,4 @@ public void interrupt() {
public void check_events() {
selector.wakeup();
}
}
}

0 comments on commit 0781835

Please sign in to comment.