From c8fd9719d3267b088f6e8883b6fbacba6fcce26b Mon Sep 17 00:00:00 2001 From: Ralf Kohrt Date: Sun, 4 Feb 2018 11:13:56 +0000 Subject: [PATCH] let KeepAliveSender detect if websocket is no longer alive and trigger recreation of SignalServiceMessagePipe - fixes #6447 --- .../websocket/WebSocketConnection.java | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) 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..869b2799e5 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 @@ -18,6 +18,7 @@ import java.util.Iterator; import java.util.LinkedList; import java.util.Map; +import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -163,21 +164,15 @@ public synchronized void sendResponse(WebSocketResponseMessage response) throws } } - private synchronized void sendKeepAlive() throws IOException { - if (keepAliveSender != null && client != null) { - byte[] message = WebSocketMessage.newBuilder() - .setType(WebSocketMessage.Type.REQUEST) - .setRequest(WebSocketRequestMessage.newBuilder() - .setId(System.currentTimeMillis()) - .setPath("/v1/keepalive") - .setVerb("GET") - .build()).build() - .toByteArray(); - - if (!client.send(ByteString.of(message))) { - throw new IOException("Write failed!"); - } - } + private synchronized Future> sendKeepAlive() throws IOException { + if (keepAliveSender == null || client == null) throw new IOException("Can't send KeepAlive Message!"); + + WebSocketRequestMessage request = WebSocketRequestMessage.newBuilder() + .setId(System.currentTimeMillis()) + .setPath("/v1/keepalive") + .setVerb("GET") + .build(); + return sendRequest(request); } @Override @@ -300,7 +295,16 @@ public void run() { Thread.sleep(TimeUnit.SECONDS.toMillis(KEEPALIVE_TIMEOUT_SECONDS)); Log.w(TAG, "Sending keep alive..."); - sendKeepAlive(); + sendKeepAlive().get(30, TimeUnit.SECONDS); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + Log.w(TAG, e); + disconnect(); + if (listener != null) { + listener.onDisconnected(); + } + synchronized(WebSocketConnection.this) { + WebSocketConnection.this.notifyAll(); + } } catch (Throwable e) { Log.w(TAG, e); }