Initiate connection teardown only once

Otherwise, this gets called multiple times from different places via MSG_TEARDOWN. This causes the reconnect delay to increase with each call to scheduleReconnect(), increasing the time we stay disconnected.

This commit introduces a boolean flag preventing handleTeardown() to run twice or more until connect() was called again.

Change-Id: I3d7cb08d696be48532a61819fbb279a908919a3d
This commit is contained in:
Torsten Grote 2021-10-20 16:31:55 -03:00 committed by Marvin W
parent 10455df7e2
commit bb68674cae

View File

@ -127,6 +127,7 @@ public class McsService extends Service implements Handler.Callback {
private static long lastIncomingNetworkRealtime = 0; private static long lastIncomingNetworkRealtime = 0;
private static long startTimestamp = 0; private static long startTimestamp = 0;
public static String activeNetworkPref = null; public static String activeNetworkPref = null;
private boolean wasTornDown = false;
private AtomicInteger nextMessageId = new AtomicInteger(0x1000000); private AtomicInteger nextMessageId = new AtomicInteger(0x1000000);
private static Socket sslSocket; private static Socket sslSocket;
@ -431,6 +432,7 @@ public class McsService extends Service implements Handler.Callback {
} }
private synchronized void connect() { private synchronized void connect() {
wasTornDown = false;
try { try {
closeAll(); closeAll();
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
@ -692,7 +694,7 @@ public class McsService extends Service implements Handler.Callback {
handleOutputDone(msg); handleOutputDone(msg);
return true; return true;
} }
Log.w(TAG, "Unknown message: " + msg); Log.w(TAG, "Unknown message (" + msg.what + "): " + msg);
return false; return false;
} }
@ -754,6 +756,14 @@ public class McsService extends Service implements Handler.Callback {
} }
private void handleTeardown(android.os.Message msg) { private void handleTeardown(android.os.Message msg) {
if (wasTornDown) {
// This can get called multiple times from different places via MSG_TEARDOWN
// this causes the reconnect delay to increase with each call to scheduleReconnect(),
// increasing the time we are disconnected.
logd(this, "Was torn down already, not doing it again");
return;
}
wasTornDown = true;
closeAll(); closeAll();
scheduleReconnect(this); scheduleReconnect(this);