Skip to content

Commit

Permalink
Fix: Added a timeout to NTR UDP socket
Browse files Browse the repository at this point in the history
This fixes an issue where the thread could get softlocked if it stopped receiving UDP packets, and could not be forcibly stopped.
That became an issue after 710df6e, in which close() was changed to not close the socket.
I could have basically just reverted that change, but I think this is cleaner.
  • Loading branch information
ChainSwordCS committed Feb 23, 2024
1 parent 819cda1 commit 7d663cb
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/java/chokistream/NTRUDPThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -62,6 +63,7 @@ public class NTRUDPThread extends Thread {
NTRUDPThread(DSScreen screen, ColorMode colorMode, int port) throws SocketException {
activeScreen = screen;
socket = new DatagramSocket(port); // 8001
socket.setSoTimeout(2000);
this.colorMode = colorMode;
}

Expand Down Expand Up @@ -166,6 +168,9 @@ public void run() {
secondaryExpectedFrame = 0;
secondaryExpectedPacket = 0;
}
} catch (SocketTimeoutException e) {
amIReceivingFrames = false;
logger.log("[NTR UDP] "+e.getClass()+": "+e.getMessage());
} catch (IOException e) {
amIReceivingFrames = false;
close();
Expand Down

0 comments on commit 7d663cb

Please sign in to comment.