Bug fixed
This commit is contained in:
@@ -128,7 +128,6 @@ public class ConnectedClient {
|
||||
try { if (tcpSocket != null) tcpSocket.close(); } catch (IOException ignored) {}
|
||||
try { if (inputStream != null) inputStream.close(); } catch (IOException ignored) {}
|
||||
try { if (outputStream != null) outputStream.close(); } catch (IOException ignored) {}
|
||||
try { if (udpChannel != null) udpChannel.close(); } catch (IOException ignored) {}
|
||||
|
||||
tcpSocket = null;
|
||||
udpChannel = null;
|
||||
|
||||
@@ -209,39 +209,45 @@ public class NetworkServer {
|
||||
|
||||
private void udpReceiveLoop() {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(65536);
|
||||
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
try {
|
||||
buffer.clear();
|
||||
SocketAddress sender = udpChannel.receive(buffer);
|
||||
if (sender == null) continue;
|
||||
|
||||
buffer.flip();
|
||||
if (buffer.remaining() < 13) continue; // 12-byte IV + at least 1 byte payload
|
||||
if (buffer.remaining() < 13) continue;
|
||||
|
||||
UUID mappedUuid = clientUdpAddresses.get(sender);
|
||||
if (mappedUuid != null) {
|
||||
ConnectedClient mappedClient = getClientByUuid(mappedUuid);
|
||||
if (mappedClient != null) {
|
||||
if (tryHandleUdpPacket(mappedClient, sender, buffer.asReadOnlyBuffer())) {
|
||||
continue;
|
||||
}
|
||||
if (mappedClient != null && tryHandleUdpPacket(mappedClient, sender, buffer.asReadOnlyBuffer())) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: try all clients if mapping missing or decrypt failed (e.g., NAT rebinding)
|
||||
List<ConnectedClient> snapshot;
|
||||
synchronized (connectedClients) {
|
||||
snapshot = new ArrayList<>(connectedClients);
|
||||
}
|
||||
for (ConnectedClient client : snapshot) {
|
||||
if (tryHandleUdpPacket(client, sender, buffer.asReadOnlyBuffer())) {
|
||||
break;
|
||||
}
|
||||
if (tryHandleUdpPacket(client, sender, buffer.asReadOnlyBuffer())) break;
|
||||
}
|
||||
} catch (java.nio.channels.ClosedChannelException e) {
|
||||
// Expected on shutdown / close from another thread.
|
||||
break;
|
||||
} catch (IOException e) {
|
||||
if (tcpSocket != null) {
|
||||
try { tcpSocket.close(); } catch (IOException ignored) {}
|
||||
// Not fatal for TCP. Keep UDP loop alive unless channel is actually closed.
|
||||
if (udpChannel == null || !udpChannel.isOpen() || Thread.currentThread().isInterrupted()) {
|
||||
break;
|
||||
}
|
||||
if (Thread.currentThread().isInterrupted()) {
|
||||
e.printStackTrace();
|
||||
|
||||
try {
|
||||
Thread.sleep(50);
|
||||
} catch (InterruptedException ie) {
|
||||
Thread.currentThread().interrupt();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user