Skip to content

Commit

Permalink
fix(sdk): gracefully handle replay EOF in XStreamClient
Browse files Browse the repository at this point in the history
Fixes #427
  • Loading branch information
xeruf committed Apr 22, 2024
1 parent deeaecb commit 3caa1b2
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions sdk/src/main/server-api/sc/networking/clients/XStreamClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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() {
Expand Down

0 comments on commit 3caa1b2

Please sign in to comment.