wait up to 2s to let server close socket before force closing

This commit is contained in:
Daniel Gultsch 2017-08-23 12:33:40 +02:00
parent 94e0c6b38c
commit d348780dfc
1 changed files with 89 additions and 84 deletions

View File

@ -63,6 +63,7 @@ import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.entities.ServiceDiscoveryResult;
import eu.siacs.conversations.generator.IqGenerator;
import eu.siacs.conversations.persistance.FileBackend;
import eu.siacs.conversations.services.NotificationService;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.CryptoHelper;
@ -446,6 +447,7 @@ public class XmppConnection implements Runnable {
/**
* Starts xmpp protocol, call after connecting to socket
*
* @return true if server returns with valid xmpp, false otherwise
*/
private boolean startXmpp(Socket socket) throws Exception {
@ -802,7 +804,6 @@ public class XmppConnection implements Runnable {
}
private void switchOverToTls(final Tag currentTag) throws XmlPullParserException, IOException {
tagReader.readTag();
try {
@ -1024,10 +1025,7 @@ public class XmppConnection implements Runnable {
private void sendBindRequest() {
while (!mXmppConnectionService.areMessagesInitialized() && socket != null && !socket.isClosed()) {
try {
Thread.sleep(500);
} catch (final InterruptedException ignored) {
}
uninterruptedSleep(500);
}
needsBinding = false;
clearIqCallbacks();
@ -1441,39 +1439,45 @@ public class XmppConnection implements Runnable {
public void disconnect(final boolean force) {
interrupt();
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": disconnecting force="+Boolean.valueOf(force));
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": disconnecting force=" + Boolean.toString(force));
if (force) {
forceCloseSocket();
} else {
if (tagWriter.isActive()) {
tagWriter.finish();
final Socket currentSocket = socket;
try {
int i = 0;
boolean warned = false;
while (!tagWriter.finished() && socket.isConnected() && i <= 10) {
if (!warned) {
Log.d(Config.LOGTAG, account.getJid().toBareJid()+": waiting for tag writer to finish");
warned = true;
}
try {
Thread.sleep(200);
} catch(InterruptedException e) {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": sleep interrupted");
}
i++;
}
if (warned) {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": tag writer has finished");
for (int i = 0; i <= 10 && !tagWriter.finished() && !currentSocket.isClosed(); ++i) {
uninterruptedSleep(100);
}
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": closing stream");
tagWriter.writeTag(Tag.end("stream:stream"));
for (int i = 0; i <= 20 && !currentSocket.isClosed(); ++i) {
uninterruptedSleep(100);
}
if (currentSocket.isClosed()) {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": remote closed socket");
} else {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": remote has not closed socket. force closing");
}
} catch (final IOException e) {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": io exception during disconnect (" + e.getMessage() + ")");
} finally {
FileBackend.close(currentSocket);
forceCloseSocket();
}
} else {
forceCloseSocket();
}
}
}
private static void uninterruptedSleep(int time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
//ignore
}
}
public void resetStreamId() {
@ -1553,6 +1557,7 @@ public class XmppConnection implements Runnable {
public long getLastDiscoStarted() {
return this.lastDiscoStarted;
}
public long getLastPacketReceived() {
return this.lastPacketReceived;
}