Revert "fix: 尝试修复断联问题"

This reverts commit e07250637e5a7777e007256f692d6874c86ad656.
This commit is contained in:
yarnom 2025-10-29 10:34:58 +08:00
parent c252b11d3c
commit a28251d787

View File

@ -19,7 +19,6 @@ import java.io.*;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.time.LocalDateTime;
@ -224,56 +223,35 @@ public class RtkClusterService implements ApplicationRunner {
private void classifyConnection(Socket s) {
exec.submit(() -> {
boolean assigned = false;
try {
s.setTcpNoDelay(true);
s.setKeepAlive(true);
s.setSoTimeout(1500);
// short probe to classify role (increase to 1000ms to reduce misclassification)
s.setSoTimeout(1000);
InputStream in = s.getInputStream();
byte[] probe = new byte[256];
int n;
try {
n = in.read(probe);
} catch (SocketTimeoutException timeout) {
n = 0; // treat as no immediate data
} catch (IOException readError) {
LOGGER.debug("Endpoint {} initial read failed: {}", port, readError.getMessage());
closeQuietly(s);
return;
} finally {
int n = 0;
try { n = in.read(probe); } catch (IOException ignore) {}
// restore to blocking mode for steady-state
s.setSoTimeout(0);
}
if (n < 0) {
LOGGER.debug("Endpoint {} connection closed during probe", port);
return;
}
if (n > 0 && isLikelyText(probe, n)) {
if (!isSocketAlive(outConn)) {
// OUT connection (NMEA etc.)
closeQuietly(outConn);
s.setTcpNoDelay(true);
s.setKeepAlive(true);
outConn = s;
assigned = true;
LOGGER.debug("Endpoint {} OUT connected", port);
pumpOut(s, probe, n);
pumpOut(outConn, probe, n);
} else {
LOGGER.info("Endpoint {} additional OUT connection detected; keeping existing", port);
}
} else {
if (!isSocketAlive(inConn)) {
// IN connection (RTCM sink)
closeQuietly(inConn);
s.setTcpNoDelay(true);
s.setKeepAlive(true);
inConn = s;
assigned = true;
LOGGER.debug("Endpoint {} IN connected", port);
} else {
LOGGER.info("Endpoint {} additional IN connection detected; keeping existing", port);
}
}
} catch (IOException e) {
LOGGER.warn("classifyConnection error: {}", e.getMessage());
} finally {
if (!assigned) {
closeQuietly(s);
}
}
});
}
@ -314,7 +292,8 @@ public class RtkClusterService implements ApplicationRunner {
while (true) {
try {
Socket sink = inConn;
if (!isSocketAlive(sink)) {
if (sink == null || sink.isClosed()) {
// avoid consuming queue when no sink, preventing starvation
Thread.sleep(20);
continue;
}
@ -324,12 +303,7 @@ public class RtkClusterService implements ApplicationRunner {
os.write(data);
os.flush();
} catch (IOException e) {
String msg = e.getMessage();
if (msg != null && msg.toLowerCase().contains("closed")) {
LOGGER.debug("Write RTCM failed on {}: {}", port, msg);
} else {
LOGGER.warn("Write RTCM failed on {}: {}", port, msg);
}
LOGGER.warn("Write RTCM failed on {}: {}", port, e.getMessage());
closeQuietly(sink);
inConn = null;
}
@ -340,10 +314,6 @@ public class RtkClusterService implements ApplicationRunner {
}
}
private boolean isSocketAlive(Socket s) {
return s != null && s.isConnected() && !s.isClosed() && !s.isInputShutdown() && !s.isOutputShutdown();
}
private void closeQuietly(Socket s) {
if (s == null) return;
try { s.close(); } catch (IOException ignore) {}