mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-10 09:39:39 +02:00
SMACK-417 If both PacketReader and PacketWriter fail at the same time, connectionClosedonError() is called two times
Refactored notifyConnectionError() and notifyReconnection() from PacketReader to XMPPConnection. Made PacketReader.done and PacketWriter.done volatile. Prevent duplicate connectionClosedonError() calls by making the method synchronzied and protected them with an enter guard: if (packetReader.done && packetWriter.done) return; git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/branches/smack_3_3_0@13568 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
6dcf8e0123
commit
a55b54f20b
5 changed files with 65 additions and 61 deletions
|
@ -473,6 +473,10 @@ public class XMPPConnection extends Connection {
|
|||
return;
|
||||
}
|
||||
|
||||
if (!isConnected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
shutdown(unavailablePresence);
|
||||
|
||||
if (roster != null) {
|
||||
|
@ -483,9 +487,7 @@ public class XMPPConnection extends Connection {
|
|||
wasAuthenticated = false;
|
||||
|
||||
packetWriter.cleanup();
|
||||
packetWriter = null;
|
||||
packetReader.cleanup();
|
||||
packetReader = null;
|
||||
}
|
||||
|
||||
public void sendPacket(Packet packet) {
|
||||
|
@ -614,10 +616,8 @@ public class XMPPConnection extends Connection {
|
|||
*/
|
||||
private void initConnection() throws XMPPException {
|
||||
boolean isFirstInitialization = packetReader == null || packetWriter == null;
|
||||
if (!isFirstInitialization) {
|
||||
compressionHandler = null;
|
||||
serverAckdCompression = false;
|
||||
}
|
||||
compressionHandler = null;
|
||||
serverAckdCompression = false;
|
||||
|
||||
// Set the reader and writer instance variables
|
||||
initReaderAndWriter();
|
||||
|
@ -661,7 +661,7 @@ public class XMPPConnection extends Connection {
|
|||
}
|
||||
}
|
||||
else if (!wasAuthenticated) {
|
||||
packetReader.notifyReconnection();
|
||||
notifyReconnection();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -773,7 +773,7 @@ public class XMPPConnection extends Connection {
|
|||
void startTLSReceived(boolean required) {
|
||||
if (required && config.getSecurityMode() ==
|
||||
ConnectionConfiguration.SecurityMode.disabled) {
|
||||
packetReader.notifyConnectionError(new IllegalStateException(
|
||||
notifyConnectionError(new IllegalStateException(
|
||||
"TLS required by server but not allowed by connection configuration"));
|
||||
return;
|
||||
}
|
||||
|
@ -787,7 +787,7 @@ public class XMPPConnection extends Connection {
|
|||
writer.flush();
|
||||
}
|
||||
catch (IOException e) {
|
||||
packetReader.notifyConnectionError(e);
|
||||
notifyConnectionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -977,7 +977,7 @@ public class XMPPConnection extends Connection {
|
|||
writer.flush();
|
||||
}
|
||||
catch (IOException e) {
|
||||
packetReader.notifyConnectionError(e);
|
||||
notifyConnectionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1041,7 +1041,7 @@ public class XMPPConnection extends Connection {
|
|||
login(config.getUsername(), config.getPassword(),
|
||||
config.getResource());
|
||||
}
|
||||
packetReader.notifyReconnection();
|
||||
notifyReconnection();
|
||||
}
|
||||
catch (XMPPException e) {
|
||||
e.printStackTrace();
|
||||
|
@ -1059,4 +1059,50 @@ public class XMPPConnection extends Connection {
|
|||
this.wasAuthenticated = wasAuthenticated;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends out a notification that there was an error with the connection
|
||||
* and closes the connection. Also prints the stack trace of the given exception
|
||||
*
|
||||
* @param e the exception that causes the connection close event.
|
||||
*/
|
||||
synchronized void notifyConnectionError(Exception e) {
|
||||
// Listeners were already notified of the exception, return right here.
|
||||
if (packetReader.done && packetWriter.done) return;
|
||||
|
||||
packetReader.done = true;
|
||||
packetWriter.done = true;
|
||||
// Closes the connection temporary. A reconnection is possible
|
||||
shutdown(new Presence(Presence.Type.unavailable));
|
||||
// Print the stack trace to help catch the problem
|
||||
e.printStackTrace();
|
||||
// Notify connection listeners of the error.
|
||||
for (ConnectionListener listener : getConnectionListeners()) {
|
||||
try {
|
||||
listener.connectionClosedOnError(e);
|
||||
}
|
||||
catch (Exception e2) {
|
||||
// Catch and print any exception so we can recover
|
||||
// from a faulty listener
|
||||
e2.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a notification indicating that the connection was reconnected successfully.
|
||||
*/
|
||||
protected void notifyReconnection() {
|
||||
// Notify connection listeners of the reconnection.
|
||||
for (ConnectionListener listener : getConnectionListeners()) {
|
||||
try {
|
||||
listener.reconnectionSuccessful();
|
||||
}
|
||||
catch (Exception e) {
|
||||
// Catch and print any exception so we can recover
|
||||
// from a faulty listener
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue