From 171c58d8ffb29c08ce55d789f0cc1b593c7f5e86 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Sat, 23 Nov 2024 11:59:15 -0500 Subject: runicast.Retransmit: catch ClosedChannelException before IOException --- src/main/java/derms/net/runicast/Retransmit.java | 27 ++++++++++++------------ 1 file changed, 13 insertions(+), 14 deletions(-) (limited to 'src/main/java/derms/net/runicast/Retransmit.java') diff --git a/src/main/java/derms/net/runicast/Retransmit.java b/src/main/java/derms/net/runicast/Retransmit.java index 16f8859..6b8fb74 100644 --- a/src/main/java/derms/net/runicast/Retransmit.java +++ b/src/main/java/derms/net/runicast/Retransmit.java @@ -15,7 +15,7 @@ import java.util.logging.Logger; /** Retransmit unacknowledged messages. */ class Retransmit implements Runnable { - private static final Duration timeout = Duration.ofMillis(500); + private static final Duration period = Duration.ofMillis(500); private final AtomicLong unacked; private final Queue> sent; @@ -31,28 +31,27 @@ class Retransmit implements Runnable { @Override public void run() { - try { - for (;;) { - Wait.forDuration(timeout); + for (;;) { + try { + Wait.forDuration(period); for (Message msg : sent) { if (msg.seq >= unacked.get()) { retransmit(msg); } } + } catch (InterruptedException | ClosedChannelException e) { + log.info("Shutting down."); + return; + } catch (IOException e) { + log.warning(e.getMessage()); } - } catch (InterruptedException | ClosedChannelException e) { - log.info("Shutting down."); } } - private void retransmit(Message msg) throws ClosedChannelException { - try { - ByteBuffer buf = Serial.encode(msg); - sock.send(buf, sock.getRemoteAddress()); - log.info("Retransmitted " + msg); - } catch (IOException e) { - log.warning("Failed to retransmit " + msg + ": " + e.getMessage()); - } + private void retransmit(Message msg) throws IOException { + ByteBuffer buf = Serial.encode(msg); + sock.send(buf, sock.getRemoteAddress()); + log.info("Retransmitted " + msg); } } -- cgit v1.2.3