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