From 3caa1b2a014a9828d40106400678411bb6b10be7 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Mon, 22 Apr 2024 03:53:33 +0300 Subject: [PATCH] fix(sdk): gracefully handle replay EOF in XStreamClient Fixes #427 --- .../sc/networking/clients/XStreamClient.java | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/sdk/src/main/server-api/sc/networking/clients/XStreamClient.java b/sdk/src/main/server-api/sc/networking/clients/XStreamClient.java index 28b1951f6..1ef699766 100644 --- a/sdk/src/main/server-api/sc/networking/clients/XStreamClient.java +++ b/sdk/src/main/server-api/sc/networking/clients/XStreamClient.java @@ -94,21 +94,21 @@ public void run() { public void receiveThread() { try (ObjectInputStream in = xStream.createObjectInputStream(networkInterface.getInputStream())) { synchronized(readyLock) { - while (!isReady()) { + while(!isReady()) { readyLock.wait(); } } - while (!Thread.interrupted()) { + while(!Thread.interrupted()) { Object object = in.readObject(); - if (object instanceof ProtocolPacket) { + if(object instanceof ProtocolPacket) { ProtocolPacket response = (ProtocolPacket) object; logger.debug("Received {} via {}", response, networkInterface); - if (logger.isTraceEnabled()) - logger.trace("Dumping {}:\n{}", response, xStream.toXML(response)); + if(logger.isTraceEnabled()) + logger.trace("Dumping received {}:\n{}", response, xStream.toXML(response)); - if (response instanceof CloseConnection) { + if(response instanceof CloseConnection) { handleDisconnect(DisconnectCause.RECEIVED_DISCONNECT); break; } else { @@ -118,6 +118,9 @@ public void receiveThread() { throw new ClassNotFoundException("Received object of unknown class " + object.getClass().getName()); } } + } catch (EOFException e) { + logger.info("End of input reached, disconnecting {}", this); + logger.trace("Disconnected with", e); } catch (IOException e) { // The other side closed the connection. // It is better when the other side sends a CloseConnection message beforehand, @@ -182,7 +185,7 @@ protected synchronized void sendObject(Object packet) { logger.debug("Sending {} via {} from {}", packet, networkInterface, this); if (logger.isTraceEnabled()) - logger.trace("Dumping {}:\n{}", packet, xStream.toXML(packet)); + logger.trace("Dumping sent {}:\n{}", packet, xStream.toXML(packet)); try { this.out.writeObject(packet); @@ -207,12 +210,12 @@ protected final void handleDisconnect(DisconnectCause cause) { } protected final void handleDisconnect(DisconnectCause cause, Throwable exception) { - logger.warn("{} disconnecting (Cause: {}, Exception: {})", this, cause, exception.toString(), exception); + logger.warn("Disconnecting with {} because of {}: {}", cause, exception.toString(), this, exception); handleDisconnect(cause); } protected void onDisconnected(DisconnectCause cause) { - logger.info("{} disconnected (Cause: {})", this, cause); + logger.info("Disconnected with {}: {}", cause, this); } public DisconnectCause getDisconnectCause() {