1616public class NioPeerHandler {
1717 private static class Peer {
1818 SocketDescriptor descriptor ;
19- // When we are told by LDK to disconnect, we can't return to LDK until we are sure
20- // won't call any more read/write PeerManager functions with the same connection.
21- // This is set to true if we're in such a condition (with disconnect checked
22- // before with the Peer monitor lock held) and false when we can return.
23- boolean block_disconnect_socket = false ;
24- // Indicates LDK told us to disconnect this peer, and thus we should not call socket_disconnected.
25- boolean disconnect_requested = false ;
2619 SelectionKey key ;
2720 }
2821
@@ -83,22 +76,12 @@ public long send_data(byte[] data, boolean resume_read) {
8376
8477 @ Override
8578 public void disconnect_socket () {
86- synchronized (peer ) {
87- peer .disconnect_requested = true ;
88- }
8979 try {
9080 do_selector_action (() -> {
9181 peer .key .cancel ();
9282 peer .key .channel ().close ();
9383 });
9484 } catch (IOException ignored ) { }
95- synchronized (peer ) {
96- while (peer .block_disconnect_socket ) {
97- try {
98- peer .wait ();
99- } catch (InterruptedException ignored ) { }
100- }
101- }
10285 }
10386 @ Override public boolean eq (SocketDescriptor other_arg ) { return other_arg .hash () == our_id ; }
10487 @ Override public long hash () { return our_id ; }
@@ -169,11 +152,6 @@ public NioPeerHandler(PeerManager manager) throws IOException {
169152 continue ; // There is no attachment so the rest of the loop is useless
170153 }
171154 Peer peer = (Peer ) key .attachment ();
172- synchronized (peer ) {
173- if (peer .disconnect_requested )
174- continue ;
175- peer .block_disconnect_socket = true ;
176- }
177155 try {
178156 if (key .isValid () && (key .interestOps () & SelectionKey .OP_WRITE ) != 0 && key .isWritable ()) {
179157 Result_NonePeerHandleErrorZ res = this .peer_manager .write_buffer_space_avail (peer .descriptor );
@@ -208,10 +186,6 @@ public NioPeerHandler(PeerManager manager) throws IOException {
208186 key .cancel ();
209187 peer_manager .socket_disconnected (peer .descriptor );
210188 }
211- synchronized (peer ) {
212- peer .block_disconnect_socket = false ;
213- peer .notifyAll ();
214- }
215189 } catch (CancelledKeyException e ) {
216190 try { key .channel ().close (); } catch (IOException ignored ) { }
217191 // The key is only cancelled when we have notified the PeerManager that the socket is closed, so
@@ -295,4 +269,4 @@ public void interrupt() {
295269 public void check_events () {
296270 selector .wakeup ();
297271 }
298- }
272+ }
0 commit comments