From 3ad5499b0df05bf0689aec3fc50bc55757cab5a5 Mon Sep 17 00:00:00 2001 From: Ralf Kohrt Date: Sat, 3 Feb 2018 16:24:03 +0000 Subject: [PATCH 1/2] fix Thread.sleep for Android - rewrite of pullrequest #45 --- .../signalservice/api/util/SignalThread.java | 24 +++++++++++++++++++ .../websocket/WebSocketConnection.java | 3 ++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 java/src/main/java/org/whispersystems/signalservice/api/util/SignalThread.java diff --git a/java/src/main/java/org/whispersystems/signalservice/api/util/SignalThread.java b/java/src/main/java/org/whispersystems/signalservice/api/util/SignalThread.java new file mode 100644 index 0000000000..4d7dadb877 --- /dev/null +++ b/java/src/main/java/org/whispersystems/signalservice/api/util/SignalThread.java @@ -0,0 +1,24 @@ +/** + * Copyright (C) 2014-2016 Open Whisper Systems + * + * Licensed according to the LICENSE file in this repository. + */ + +package org.whispersystems.signalservice.api.util; + +import org.whispersystems.signalservice.internal.util.Util; + +public class SignalThread { + + private static boolean active = false; + + public static synchronized void onTrigger() { + active = true; + SignalThread.class.notifyAll(); + } + + public static synchronized void sleep(long millis) { + Util.wait(SignalThread.class, active ? 0 : millis); + } + +} \ No newline at end of file diff --git a/java/src/main/java/org/whispersystems/signalservice/internal/websocket/WebSocketConnection.java b/java/src/main/java/org/whispersystems/signalservice/internal/websocket/WebSocketConnection.java index b6a845a033..125199b1f5 100644 --- a/java/src/main/java/org/whispersystems/signalservice/internal/websocket/WebSocketConnection.java +++ b/java/src/main/java/org/whispersystems/signalservice/internal/websocket/WebSocketConnection.java @@ -6,6 +6,7 @@ import org.whispersystems.libsignal.util.Pair; import org.whispersystems.signalservice.api.push.TrustStore; import org.whispersystems.signalservice.api.util.CredentialsProvider; +import org.whispersystems.signalservice.api.util.SignalThread; import org.whispersystems.signalservice.api.websocket.ConnectivityListener; import org.whispersystems.signalservice.internal.util.BlacklistingTrustManager; import org.whispersystems.signalservice.internal.util.Util; @@ -297,7 +298,7 @@ private class KeepAliveSender extends Thread { public void run() { while (!stop.get()) { try { - Thread.sleep(TimeUnit.SECONDS.toMillis(KEEPALIVE_TIMEOUT_SECONDS)); + SignalThread.sleep(TimeUnit.SECONDS.toMillis(KEEPALIVE_TIMEOUT_SECONDS)); Log.w(TAG, "Sending keep alive..."); sendKeepAlive(); From d523f6975bc8c49c8177336501b2d8c60f6ff3a2 Mon Sep 17 00:00:00 2001 From: Ralf Kohrt Date: Sun, 4 Feb 2018 10:16:24 +0000 Subject: [PATCH 2/2] fix timeout duration --- .../signalservice/internal/websocket/WebSocketConnection.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/src/main/java/org/whispersystems/signalservice/internal/websocket/WebSocketConnection.java b/java/src/main/java/org/whispersystems/signalservice/internal/websocket/WebSocketConnection.java index 125199b1f5..b9d26fcffd 100644 --- a/java/src/main/java/org/whispersystems/signalservice/internal/websocket/WebSocketConnection.java +++ b/java/src/main/java/org/whispersystems/signalservice/internal/websocket/WebSocketConnection.java @@ -43,7 +43,7 @@ public class WebSocketConnection extends WebSocketListener { private static final String TAG = WebSocketConnection.class.getSimpleName(); - private static final int KEEPALIVE_TIMEOUT_SECONDS = 55; + private static final int KEEPALIVE_TIMEOUT_SECONDS = 60; private final LinkedList incomingRequests = new LinkedList<>(); private final Map>> outgoingRequests = new HashMap<>();