Skip to content

Commit 5677e89

Browse files
committed
fix(network): never send CloseConnection manually
1 parent cdc107a commit 5677e89

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

helpers/test-client/src/sc/TestClient.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import sc.api.plugins.IGamePlugin;
1010
import sc.framework.plugins.Player;
1111
import sc.networking.clients.XStreamClient;
12-
import sc.protocol.CloseConnection;
1312
import sc.protocol.ProtocolPacket;
1413
import sc.protocol.room.RoomPacket;
1514
import sc.protocol.requests.*;
@@ -338,8 +337,7 @@ private void prepareNewClients() {
338337

339338
private static void exit(int status) {
340339
if (testclient != null) {
341-
if (!testclient.isClosed())
342-
testclient.send(new CloseConnection());
340+
testclient.stop();
343341
testclient.waiter.shutdownNow();
344342
}
345343

sdk/src/server-api/sc/networking/clients/XStreamClient.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void run() {
9191

9292
/** Used by the receiving thread. All exceptions should be handled. */
9393
public void receiveThread() {
94-
try(ObjectInputStream in = xStream.createObjectInputStream(networkInterface.getInputStream())) {
94+
try (ObjectInputStream in = xStream.createObjectInputStream(networkInterface.getInputStream())) {
9595
synchronized(readyLock) {
9696
while (!isReady()) {
9797
readyLock.wait();
@@ -109,7 +109,7 @@ public void receiveThread() {
109109

110110
if (response instanceof CloseConnection) {
111111
handleDisconnect(DisconnectCause.RECEIVED_DISCONNECT);
112-
// handleDisconnect takes care of stopping the thread
112+
break;
113113
} else {
114114
onObject(response);
115115
}
@@ -133,13 +133,13 @@ public void receiveThread() {
133133
if (exceptionCause instanceof SocketException) {
134134
// If the thread was interrupted, we have a regular disconnect.
135135
// Unfortunately, OIS.readObject() doesn't react to interruptions directly.
136-
if(!Thread.interrupted())
136+
if (!Thread.interrupted())
137137
handleDisconnect(DisconnectCause.LOST_CONNECTION, e);
138138
} else if (exceptionCause instanceof EOFException) {
139139
handleDisconnect(DisconnectCause.LOST_CONNECTION, e);
140140
} else if (exceptionCause instanceof IOException
141-
&& exceptionCause.getCause() != null && exceptionCause
142-
.getCause() instanceof InterruptedException) {
141+
&& exceptionCause.getCause() != null && exceptionCause
142+
.getCause() instanceof InterruptedException) {
143143
handleDisconnect(DisconnectCause.LOST_CONNECTION, e);
144144
} else {
145145
handleDisconnect(DisconnectCause.PROTOCOL_ERROR, e);
@@ -164,7 +164,11 @@ public void sendCustomData(byte[] data) throws IOException {
164164
networkInterface.getOutputStream().flush();
165165
}
166166

167-
public synchronized void send(ProtocolPacket packet) {
167+
public void send(ProtocolPacket packet) {
168+
sendObject(packet);
169+
}
170+
171+
protected synchronized void sendObject(Object packet) {
168172
if (!isReady())
169173
throw new IllegalStateException(
170174
String.format("Trying to write packet on %s which wasn't started: %s", shortString(), packet));
@@ -176,7 +180,7 @@ public synchronized void send(ProtocolPacket packet) {
176180
return;
177181
}
178182

179-
logger.debug("{}: Sending {} via {} from {}", shortString(), packet, networkInterface, toString());
183+
logger.debug("{}: Sending {} via {} from {}", shortString(), packet, networkInterface, this);
180184
if (logger.isTraceEnabled())
181185
logger.trace("Dumping {}:\n{}", packet, xStream.toXML(packet));
182186

@@ -231,7 +235,8 @@ public DisconnectCause getDisconnectCause() {
231235
*/
232236
public void stop() {
233237
// this side caused disconnect, notify other side
234-
send(new CloseConnection());
238+
if (!isClosed())
239+
send(new CloseConnection());
235240
handleDisconnect(DisconnectCause.DISCONNECTED);
236241
}
237242

sdk/src/server-api/sc/protocol/CloseConnection.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import com.thoughtworks.xstream.annotations.XStreamAlias
55
/**
66
* Is sent by one party immediately before this party closes the communication
77
* connection and should make the receiving party also close the connection.
8+
*
9+
* This should not be sent manually, the XStreamClient will automatically send
10+
* it when stopped.
811
*/
912
@XStreamAlias(value = "close")
1013
class CloseConnection: ProtocolPacket {

0 commit comments

Comments
 (0)