@@ -91,7 +91,7 @@ public void run() {
91
91
92
92
/** Used by the receiving thread. All exceptions should be handled. */
93
93
public void receiveThread () {
94
- try (ObjectInputStream in = xStream .createObjectInputStream (networkInterface .getInputStream ())) {
94
+ try (ObjectInputStream in = xStream .createObjectInputStream (networkInterface .getInputStream ())) {
95
95
synchronized (readyLock ) {
96
96
while (!isReady ()) {
97
97
readyLock .wait ();
@@ -109,7 +109,7 @@ public void receiveThread() {
109
109
110
110
if (response instanceof CloseConnection ) {
111
111
handleDisconnect (DisconnectCause .RECEIVED_DISCONNECT );
112
- // handleDisconnect takes care of stopping the thread
112
+ break ;
113
113
} else {
114
114
onObject (response );
115
115
}
@@ -133,13 +133,13 @@ public void receiveThread() {
133
133
if (exceptionCause instanceof SocketException ) {
134
134
// If the thread was interrupted, we have a regular disconnect.
135
135
// Unfortunately, OIS.readObject() doesn't react to interruptions directly.
136
- if (!Thread .interrupted ())
136
+ if (!Thread .interrupted ())
137
137
handleDisconnect (DisconnectCause .LOST_CONNECTION , e );
138
138
} else if (exceptionCause instanceof EOFException ) {
139
139
handleDisconnect (DisconnectCause .LOST_CONNECTION , e );
140
140
} else if (exceptionCause instanceof IOException
141
- && exceptionCause .getCause () != null && exceptionCause
142
- .getCause () instanceof InterruptedException ) {
141
+ && exceptionCause .getCause () != null && exceptionCause
142
+ .getCause () instanceof InterruptedException ) {
143
143
handleDisconnect (DisconnectCause .LOST_CONNECTION , e );
144
144
} else {
145
145
handleDisconnect (DisconnectCause .PROTOCOL_ERROR , e );
@@ -164,7 +164,11 @@ public void sendCustomData(byte[] data) throws IOException {
164
164
networkInterface .getOutputStream ().flush ();
165
165
}
166
166
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 ) {
168
172
if (!isReady ())
169
173
throw new IllegalStateException (
170
174
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) {
176
180
return ;
177
181
}
178
182
179
- logger .debug ("{}: Sending {} via {} from {}" , shortString (), packet , networkInterface , toString () );
183
+ logger .debug ("{}: Sending {} via {} from {}" , shortString (), packet , networkInterface , this );
180
184
if (logger .isTraceEnabled ())
181
185
logger .trace ("Dumping {}:\n {}" , packet , xStream .toXML (packet ));
182
186
@@ -231,7 +235,8 @@ public DisconnectCause getDisconnectCause() {
231
235
*/
232
236
public void stop () {
233
237
// this side caused disconnect, notify other side
234
- send (new CloseConnection ());
238
+ if (!isClosed ())
239
+ send (new CloseConnection ());
235
240
handleDisconnect (DisconnectCause .DISCONNECTED );
236
241
}
237
242
0 commit comments