Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aba3930417 | ||
|
|
4476f41aa6 |
@@ -4,6 +4,7 @@ import android.util.Base64;
|
|||||||
|
|
||||||
import java.io.BufferedInputStream;
|
import java.io.BufferedInputStream;
|
||||||
import java.io.BufferedOutputStream;
|
import java.io.BufferedOutputStream;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
@@ -14,16 +15,22 @@ import java.util.Arrays;
|
|||||||
import javax.net.ssl.SSLSocketFactory;
|
import javax.net.ssl.SSLSocketFactory;
|
||||||
|
|
||||||
final class AppEventSocketClient {
|
final class AppEventSocketClient {
|
||||||
|
private static final long HEARTBEAT_INTERVAL_MS = 15000L;
|
||||||
|
private static final int SOCKET_CONNECT_TIMEOUT_MS = 10000;
|
||||||
|
private static final int SOCKET_READ_TIMEOUT_MS = 45000;
|
||||||
|
|
||||||
interface Listener {
|
interface Listener {
|
||||||
void onMessage(String text);
|
void onMessage(String text);
|
||||||
void onClosed();
|
void onClosed();
|
||||||
}
|
}
|
||||||
|
|
||||||
private final Listener listener;
|
private final Listener listener;
|
||||||
|
private final Object writeLock = new Object();
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
private BufferedInputStream input;
|
private BufferedInputStream input;
|
||||||
private BufferedOutputStream output;
|
private BufferedOutputStream output;
|
||||||
private volatile boolean closed;
|
private volatile boolean closed;
|
||||||
|
private Thread heartbeatThread;
|
||||||
|
|
||||||
AppEventSocketClient(Listener listener) {
|
AppEventSocketClient(Listener listener) {
|
||||||
this.listener = listener;
|
this.listener = listener;
|
||||||
@@ -40,12 +47,11 @@ final class AppEventSocketClient {
|
|||||||
sendFrame(8, new byte[0]);
|
sendFrame(8, new byte[0]);
|
||||||
} catch (Exception ignored) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
try {
|
closeSocketQuietly();
|
||||||
if (socket != null) {
|
}
|
||||||
socket.close();
|
|
||||||
}
|
boolean isClosed() {
|
||||||
} catch (Exception ignored) {
|
return closed;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void run(String baseUrl) {
|
private void run(String baseUrl) {
|
||||||
@@ -55,6 +61,7 @@ final class AppEventSocketClient {
|
|||||||
input = new BufferedInputStream(socket.getInputStream());
|
input = new BufferedInputStream(socket.getInputStream());
|
||||||
output = new BufferedOutputStream(socket.getOutputStream());
|
output = new BufferedOutputStream(socket.getOutputStream());
|
||||||
handshake(uri);
|
handshake(uri);
|
||||||
|
startHeartbeat();
|
||||||
while (!closed) {
|
while (!closed) {
|
||||||
Frame frame = readFrame();
|
Frame frame = readFrame();
|
||||||
if (frame.opcode == 1) {
|
if (frame.opcode == 1) {
|
||||||
@@ -69,12 +76,7 @@ final class AppEventSocketClient {
|
|||||||
} finally {
|
} finally {
|
||||||
closed = true;
|
closed = true;
|
||||||
listener.onClosed();
|
listener.onClosed();
|
||||||
try {
|
closeSocketQuietly();
|
||||||
if (socket != null) {
|
|
||||||
socket.close();
|
|
||||||
}
|
|
||||||
} catch (Exception ignored) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,10 +93,39 @@ final class AppEventSocketClient {
|
|||||||
if (port == -1) {
|
if (port == -1) {
|
||||||
port = "wss".equalsIgnoreCase(uri.getScheme()) ? 443 : 80;
|
port = "wss".equalsIgnoreCase(uri.getScheme()) ? 443 : 80;
|
||||||
}
|
}
|
||||||
|
Socket raw = new Socket();
|
||||||
|
raw.connect(new InetSocketAddress(uri.getHost(), port), SOCKET_CONNECT_TIMEOUT_MS);
|
||||||
|
Socket connected;
|
||||||
if ("wss".equalsIgnoreCase(uri.getScheme())) {
|
if ("wss".equalsIgnoreCase(uri.getScheme())) {
|
||||||
return SSLSocketFactory.getDefault().createSocket(uri.getHost(), port);
|
connected = ((SSLSocketFactory) SSLSocketFactory.getDefault())
|
||||||
|
.createSocket(raw, uri.getHost(), port, true);
|
||||||
|
} else {
|
||||||
|
connected = raw;
|
||||||
}
|
}
|
||||||
return new Socket(uri.getHost(), port);
|
connected.setKeepAlive(true);
|
||||||
|
connected.setTcpNoDelay(true);
|
||||||
|
connected.setSoTimeout(SOCKET_READ_TIMEOUT_MS);
|
||||||
|
return connected;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startHeartbeat() {
|
||||||
|
heartbeatThread = new Thread(() -> {
|
||||||
|
while (!closed) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(HEARTBEAT_INTERVAL_MS);
|
||||||
|
if (!closed) {
|
||||||
|
sendFrame(9, new byte[0]);
|
||||||
|
}
|
||||||
|
} catch (InterruptedException ignored) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
return;
|
||||||
|
} catch (Exception error) {
|
||||||
|
closeSocketQuietly();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, "app-events-ws-heartbeat");
|
||||||
|
heartbeatThread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handshake(URI uri) throws Exception {
|
private void handshake(URI uri) throws Exception {
|
||||||
@@ -181,32 +212,47 @@ final class AppEventSocketClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void sendFrame(int opcode, byte[] payload) throws Exception {
|
private void sendFrame(int opcode, byte[] payload) throws Exception {
|
||||||
if (output == null) {
|
synchronized (writeLock) {
|
||||||
return;
|
if (output == null) {
|
||||||
}
|
return;
|
||||||
output.write(0x80 | opcode);
|
|
||||||
byte[] mask = new byte[4];
|
|
||||||
new SecureRandom().nextBytes(mask);
|
|
||||||
int length = payload.length;
|
|
||||||
if (length < 126) {
|
|
||||||
output.write(0x80 | length);
|
|
||||||
} else if (length <= 0xffff) {
|
|
||||||
output.write(0x80 | 126);
|
|
||||||
output.write((length >>> 8) & 0xff);
|
|
||||||
output.write(length & 0xff);
|
|
||||||
} else {
|
|
||||||
output.write(0x80 | 127);
|
|
||||||
for (int i = 7; i >= 0; i--) {
|
|
||||||
output.write((length >>> (8 * i)) & 0xff);
|
|
||||||
}
|
}
|
||||||
|
output.write(0x80 | opcode);
|
||||||
|
byte[] mask = new byte[4];
|
||||||
|
new SecureRandom().nextBytes(mask);
|
||||||
|
int length = payload.length;
|
||||||
|
if (length < 126) {
|
||||||
|
output.write(0x80 | length);
|
||||||
|
} else if (length <= 0xffff) {
|
||||||
|
output.write(0x80 | 126);
|
||||||
|
output.write((length >>> 8) & 0xff);
|
||||||
|
output.write(length & 0xff);
|
||||||
|
} else {
|
||||||
|
output.write(0x80 | 127);
|
||||||
|
for (int i = 7; i >= 0; i--) {
|
||||||
|
output.write((length >>> (8 * i)) & 0xff);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
output.write(mask);
|
||||||
|
byte[] masked = Arrays.copyOf(payload, payload.length);
|
||||||
|
for (int i = 0; i < masked.length; i++) {
|
||||||
|
masked[i] = (byte) (masked[i] ^ mask[i % 4]);
|
||||||
|
}
|
||||||
|
output.write(masked);
|
||||||
|
output.flush();
|
||||||
}
|
}
|
||||||
output.write(mask);
|
}
|
||||||
byte[] masked = Arrays.copyOf(payload, payload.length);
|
|
||||||
for (int i = 0; i < masked.length; i++) {
|
private void closeSocketQuietly() {
|
||||||
masked[i] = (byte) (masked[i] ^ mask[i % 4]);
|
if (heartbeatThread != null) {
|
||||||
|
heartbeatThread.interrupt();
|
||||||
|
heartbeatThread = null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (socket != null) {
|
||||||
|
socket.close();
|
||||||
|
}
|
||||||
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
output.write(masked);
|
|
||||||
output.flush();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final class Frame {
|
private static final class Frame {
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ import android.graphics.drawable.StateListDrawable;
|
|||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Looper;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.text.InputType;
|
import android.text.InputType;
|
||||||
@@ -68,6 +70,7 @@ public final class MainActivity extends Activity {
|
|||||||
private static final int STATUS_SUCCESS = 2;
|
private static final int STATUS_SUCCESS = 2;
|
||||||
private static final int STATUS_ERROR = 3;
|
private static final int STATUS_ERROR = 3;
|
||||||
private static final long TERMINAL_RENDER_INTERVAL_MS = 80L;
|
private static final long TERMINAL_RENDER_INTERVAL_MS = 80L;
|
||||||
|
private static final long[] SOCKET_RECONNECT_DELAYS_MS = {1000L, 2000L, 4000L, 8000L, 15000L};
|
||||||
private static final int COLOR_APP_BG = Color.rgb(9, 11, 13);
|
private static final int COLOR_APP_BG = Color.rgb(9, 11, 13);
|
||||||
private static final int COLOR_BAR = Color.rgb(15, 18, 21);
|
private static final int COLOR_BAR = Color.rgb(15, 18, 21);
|
||||||
private static final int COLOR_PANEL = Color.rgb(22, 26, 30);
|
private static final int COLOR_PANEL = Color.rgb(22, 26, 30);
|
||||||
@@ -104,6 +107,7 @@ public final class MainActivity extends Activity {
|
|||||||
};
|
};
|
||||||
|
|
||||||
private final ExecutorService executor = Executors.newSingleThreadExecutor();
|
private final ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||||
|
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||||
private SharedPreferences prefs;
|
private SharedPreferences prefs;
|
||||||
private UpdateManager updateManager;
|
private UpdateManager updateManager;
|
||||||
private SessionApiClient api;
|
private SessionApiClient api;
|
||||||
@@ -131,10 +135,18 @@ public final class MainActivity extends Activity {
|
|||||||
private TerminalScreenBuffer terminalScreen = new TerminalScreenBuffer(DEFAULT_TERMINAL_COLS, DEFAULT_TERMINAL_ROWS);
|
private TerminalScreenBuffer terminalScreen = new TerminalScreenBuffer(DEFAULT_TERMINAL_COLS, DEFAULT_TERMINAL_ROWS);
|
||||||
private final StringBuilder queuedTerminalInput = new StringBuilder();
|
private final StringBuilder queuedTerminalInput = new StringBuilder();
|
||||||
private boolean terminalConnected;
|
private boolean terminalConnected;
|
||||||
|
private boolean terminalConnecting;
|
||||||
private boolean terminalRenderPending;
|
private boolean terminalRenderPending;
|
||||||
private boolean terminalSelectionEnabled;
|
private boolean terminalSelectionEnabled;
|
||||||
private boolean terminalFollowOutput = true;
|
private boolean terminalFollowOutput = true;
|
||||||
private int terminalKeyPage;
|
private int terminalKeyPage;
|
||||||
|
private int terminalReconnectAttempt;
|
||||||
|
private int terminalConnectionGeneration;
|
||||||
|
private int eventReconnectAttempt;
|
||||||
|
private int eventConnectionGeneration;
|
||||||
|
private boolean activityDestroyed;
|
||||||
|
private Runnable terminalReconnectTask;
|
||||||
|
private Runnable eventReconnectTask;
|
||||||
private long lastTerminalRenderMs;
|
private long lastTerminalRenderMs;
|
||||||
private int terminalCols = DEFAULT_TERMINAL_COLS;
|
private int terminalCols = DEFAULT_TERMINAL_COLS;
|
||||||
private int terminalRows = DEFAULT_TERMINAL_ROWS;
|
private int terminalRows = DEFAULT_TERMINAL_ROWS;
|
||||||
@@ -2307,9 +2319,22 @@ public final class MainActivity extends Activity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void connectTerminal(String sessionName) {
|
private void connectTerminal(String sessionName) {
|
||||||
closeTerminalSocket();
|
terminalReconnectAttempt = 0;
|
||||||
queuedTerminalInput.setLength(0);
|
terminalConnectionGeneration++;
|
||||||
|
cancelTerminalReconnect();
|
||||||
|
closeTerminalSocket(false);
|
||||||
|
connectTerminalSocket(sessionName, terminalConnectionGeneration);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void connectTerminalSocket(String sessionName, int generation) {
|
||||||
|
if (activityDestroyed
|
||||||
|
|| activeSessionName == null
|
||||||
|
|| !sessionName.equals(activeSessionName)
|
||||||
|
|| generation != terminalConnectionGeneration) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
terminalConnected = false;
|
terminalConnected = false;
|
||||||
|
terminalConnecting = true;
|
||||||
terminalSocketStatus = "terminal connecting";
|
terminalSocketStatus = "terminal connecting";
|
||||||
updateTerminalMeta();
|
updateTerminalMeta();
|
||||||
setStatus("Connecting " + sessionName);
|
setStatus("Connecting " + sessionName);
|
||||||
@@ -2319,7 +2344,12 @@ public final class MainActivity extends Activity {
|
|||||||
@Override
|
@Override
|
||||||
public void onConnected() {
|
public void onConnected() {
|
||||||
runOnUiThread(() -> {
|
runOnUiThread(() -> {
|
||||||
|
if (!isCurrentTerminalConnection(sessionName, generation)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
terminalConnected = true;
|
terminalConnected = true;
|
||||||
|
terminalConnecting = false;
|
||||||
|
terminalReconnectAttempt = 0;
|
||||||
terminalSocketStatus = "terminal connected";
|
terminalSocketStatus = "terminal connected";
|
||||||
updateTerminalMeta();
|
updateTerminalMeta();
|
||||||
setStatus("Connected " + sessionName);
|
setStatus("Connected " + sessionName);
|
||||||
@@ -2330,12 +2360,20 @@ public final class MainActivity extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onOutput(String data) {
|
public void onOutput(String data) {
|
||||||
runOnUiThread(() -> appendTerminal(data));
|
runOnUiThread(() -> {
|
||||||
|
if (isCurrentTerminalConnection(sessionName, generation)) {
|
||||||
|
appendTerminal(data);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onError(String message) {
|
public void onError(String message) {
|
||||||
runOnUiThread(() -> {
|
runOnUiThread(() -> {
|
||||||
|
if (!isCurrentTerminalConnection(sessionName, generation)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
terminalConnecting = false;
|
||||||
appendTerminal("\r\n[error] " + message + "\r\n");
|
appendTerminal("\r\n[error] " + message + "\r\n");
|
||||||
terminalSocketStatus = "terminal error";
|
terminalSocketStatus = "terminal error";
|
||||||
updateTerminalMeta();
|
updateTerminalMeta();
|
||||||
@@ -2346,10 +2384,15 @@ public final class MainActivity extends Activity {
|
|||||||
@Override
|
@Override
|
||||||
public void onClosed() {
|
public void onClosed() {
|
||||||
runOnUiThread(() -> {
|
runOnUiThread(() -> {
|
||||||
|
if (!isCurrentTerminalConnection(sessionName, generation)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
terminalConnected = false;
|
terminalConnected = false;
|
||||||
|
terminalConnecting = false;
|
||||||
terminalSocketStatus = "terminal disconnected";
|
terminalSocketStatus = "terminal disconnected";
|
||||||
updateTerminalMeta();
|
updateTerminalMeta();
|
||||||
setStatus("Disconnected " + sessionName);
|
setStatus("Disconnected " + sessionName);
|
||||||
|
scheduleTerminalReconnect(sessionName, generation);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -2410,7 +2453,8 @@ public final class MainActivity extends Activity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void sendTerminalInput(String data) {
|
private void sendTerminalInput(String data) {
|
||||||
if (activeSessionName == null) {
|
String sessionName = activeSessionName;
|
||||||
|
if (sessionName == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
TerminalSocketClient socket = terminalSocket;
|
TerminalSocketClient socket = terminalSocket;
|
||||||
@@ -2419,7 +2463,7 @@ public final class MainActivity extends Activity {
|
|||||||
setStatus("Sent input");
|
setStatus("Sent input");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (socket != null && !socket.isClosed()) {
|
if ((socket != null && !socket.isClosed()) || terminalConnecting || terminalReconnectTask != null) {
|
||||||
queuedTerminalInput.append(data);
|
queuedTerminalInput.append(data);
|
||||||
setStatus("Queued input until terminal connects");
|
setStatus("Queued input until terminal connects");
|
||||||
return;
|
return;
|
||||||
@@ -2427,7 +2471,7 @@ public final class MainActivity extends Activity {
|
|||||||
executor.execute(() -> {
|
executor.execute(() -> {
|
||||||
try {
|
try {
|
||||||
for (int i = 0; i < data.length(); i += 200) {
|
for (int i = 0; i < data.length(); i += 200) {
|
||||||
api.sendInput(activeSessionName, data.substring(i, Math.min(i + 200, data.length())));
|
api.sendInput(sessionName, data.substring(i, Math.min(i + 200, data.length())));
|
||||||
}
|
}
|
||||||
runOnUiThread(() -> setStatus("Sent input"));
|
runOnUiThread(() -> setStatus("Sent input"));
|
||||||
} catch (Exception error) {
|
} catch (Exception error) {
|
||||||
@@ -2948,9 +2992,53 @@ public final class MainActivity extends Activity {
|
|||||||
return Math.round(value * getResources().getDisplayMetrics().density);
|
return Math.round(value * getResources().getDisplayMetrics().density);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isCurrentTerminalConnection(String sessionName, int generation) {
|
||||||
|
return !activityDestroyed
|
||||||
|
&& generation == terminalConnectionGeneration
|
||||||
|
&& sessionName.equals(activeSessionName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void scheduleTerminalReconnect(String sessionName, int generation) {
|
||||||
|
if (!isCurrentTerminalConnection(sessionName, generation) || terminalReconnectTask != null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
long delay = reconnectDelay(terminalReconnectAttempt++);
|
||||||
|
terminalSocketStatus = "retry in " + Math.max(1L, delay / 1000L) + "s";
|
||||||
|
updateTerminalMeta();
|
||||||
|
setStatus("Terminal reconnecting in " + Math.max(1L, delay / 1000L) + "s");
|
||||||
|
terminalReconnectTask = () -> {
|
||||||
|
terminalReconnectTask = null;
|
||||||
|
if (isCurrentTerminalConnection(sessionName, generation)) {
|
||||||
|
connectTerminalSocket(sessionName, generation);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
mainHandler.postDelayed(terminalReconnectTask, delay);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void cancelTerminalReconnect() {
|
||||||
|
if (terminalReconnectTask != null) {
|
||||||
|
mainHandler.removeCallbacks(terminalReconnectTask);
|
||||||
|
terminalReconnectTask = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private long reconnectDelay(int attempt) {
|
||||||
|
return SOCKET_RECONNECT_DELAYS_MS[Math.min(attempt, SOCKET_RECONNECT_DELAYS_MS.length - 1)];
|
||||||
|
}
|
||||||
|
|
||||||
private void closeTerminalSocket() {
|
private void closeTerminalSocket() {
|
||||||
|
closeTerminalSocket(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void closeTerminalSocket(boolean invalidateConnection) {
|
||||||
|
if (invalidateConnection) {
|
||||||
|
terminalConnectionGeneration++;
|
||||||
|
terminalReconnectAttempt = 0;
|
||||||
|
cancelTerminalReconnect();
|
||||||
|
queuedTerminalInput.setLength(0);
|
||||||
|
}
|
||||||
terminalConnected = false;
|
terminalConnected = false;
|
||||||
queuedTerminalInput.setLength(0);
|
terminalConnecting = false;
|
||||||
terminalRenderPending = false;
|
terminalRenderPending = false;
|
||||||
if (terminalSocket != null) {
|
if (terminalSocket != null) {
|
||||||
terminalSocket.close();
|
terminalSocket.close();
|
||||||
@@ -2959,25 +3047,69 @@ public final class MainActivity extends Activity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void connectAppEvents() {
|
private void connectAppEvents() {
|
||||||
|
eventReconnectAttempt = 0;
|
||||||
|
eventConnectionGeneration++;
|
||||||
|
cancelEventReconnect();
|
||||||
if (eventSocket != null) {
|
if (eventSocket != null) {
|
||||||
eventSocket.close();
|
eventSocket.close();
|
||||||
|
eventSocket = null;
|
||||||
|
}
|
||||||
|
connectAppEventSocket(api.getBaseUrl(), eventConnectionGeneration);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void connectAppEventSocket(String baseUrl, int generation) {
|
||||||
|
if (activityDestroyed || generation != eventConnectionGeneration) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
eventSocket = new AppEventSocketClient(new AppEventSocketClient.Listener() {
|
eventSocket = new AppEventSocketClient(new AppEventSocketClient.Listener() {
|
||||||
@Override
|
@Override
|
||||||
public void onMessage(String text) {
|
public void onMessage(String text) {
|
||||||
runOnUiThread(() -> handleAppEvent(text));
|
runOnUiThread(() -> {
|
||||||
|
if (generation == eventConnectionGeneration && !activityDestroyed) {
|
||||||
|
handleAppEvent(text);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onClosed() {
|
public void onClosed() {
|
||||||
runOnUiThread(() -> {
|
runOnUiThread(() -> {
|
||||||
|
if (generation != eventConnectionGeneration || activityDestroyed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
terminalEventStatus = "events disconnected";
|
terminalEventStatus = "events disconnected";
|
||||||
updateTerminalMeta();
|
updateTerminalMeta();
|
||||||
setStatus("Event stream disconnected");
|
setStatus("Event stream disconnected");
|
||||||
|
scheduleEventReconnect(baseUrl, generation);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
eventSocket.connect(api.getBaseUrl());
|
eventSocket.connect(baseUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void scheduleEventReconnect(String baseUrl, int generation) {
|
||||||
|
if (activityDestroyed
|
||||||
|
|| generation != eventConnectionGeneration
|
||||||
|
|| eventReconnectTask != null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
long delay = reconnectDelay(eventReconnectAttempt++);
|
||||||
|
terminalEventStatus = "events retry in " + Math.max(1L, delay / 1000L) + "s";
|
||||||
|
updateTerminalMeta();
|
||||||
|
eventReconnectTask = () -> {
|
||||||
|
eventReconnectTask = null;
|
||||||
|
if (!activityDestroyed && generation == eventConnectionGeneration) {
|
||||||
|
connectAppEventSocket(baseUrl, generation);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
mainHandler.postDelayed(eventReconnectTask, delay);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void cancelEventReconnect() {
|
||||||
|
if (eventReconnectTask != null) {
|
||||||
|
mainHandler.removeCallbacks(eventReconnectTask);
|
||||||
|
eventReconnectTask = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleAppEvent(String text) {
|
private void handleAppEvent(String text) {
|
||||||
@@ -2985,6 +3117,7 @@ public final class MainActivity extends Activity {
|
|||||||
JSONObject event = new JSONObject(text);
|
JSONObject event = new JSONObject(text);
|
||||||
String type = event.optString("type", "");
|
String type = event.optString("type", "");
|
||||||
if ("hello".equals(type)) {
|
if ("hello".equals(type)) {
|
||||||
|
eventReconnectAttempt = 0;
|
||||||
terminalEventStatus = "events connected";
|
terminalEventStatus = "events connected";
|
||||||
updateTerminalMeta();
|
updateTerminalMeta();
|
||||||
setStatus("Event stream connected");
|
setStatus("Event stream connected");
|
||||||
@@ -3077,11 +3210,23 @@ public final class MainActivity extends Activity {
|
|||||||
if (updateManager != null) {
|
if (updateManager != null) {
|
||||||
updateManager.resumePendingInstall();
|
updateManager.resumePendingInstall();
|
||||||
}
|
}
|
||||||
|
if (activeSessionName != null
|
||||||
|
&& !terminalConnected
|
||||||
|
&& !terminalConnecting
|
||||||
|
&& terminalReconnectTask == null) {
|
||||||
|
scheduleTerminalReconnect(activeSessionName, terminalConnectionGeneration);
|
||||||
|
}
|
||||||
|
if ((eventSocket == null || eventSocket.isClosed()) && eventReconnectTask == null) {
|
||||||
|
connectAppEvents();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onDestroy() {
|
protected void onDestroy() {
|
||||||
|
activityDestroyed = true;
|
||||||
closeTerminalSocket();
|
closeTerminalSocket();
|
||||||
|
eventConnectionGeneration++;
|
||||||
|
cancelEventReconnect();
|
||||||
if (eventSocket != null) {
|
if (eventSocket != null) {
|
||||||
eventSocket.close();
|
eventSocket.close();
|
||||||
eventSocket = null;
|
eventSocket = null;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import org.json.JSONObject;
|
|||||||
|
|
||||||
import java.io.BufferedInputStream;
|
import java.io.BufferedInputStream;
|
||||||
import java.io.BufferedOutputStream;
|
import java.io.BufferedOutputStream;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
@@ -19,6 +20,10 @@ import java.util.concurrent.RejectedExecutionException;
|
|||||||
import javax.net.ssl.SSLSocketFactory;
|
import javax.net.ssl.SSLSocketFactory;
|
||||||
|
|
||||||
final class TerminalSocketClient {
|
final class TerminalSocketClient {
|
||||||
|
private static final long HEARTBEAT_INTERVAL_MS = 15000L;
|
||||||
|
private static final int SOCKET_CONNECT_TIMEOUT_MS = 10000;
|
||||||
|
private static final int SOCKET_READ_TIMEOUT_MS = 45000;
|
||||||
|
|
||||||
interface Listener {
|
interface Listener {
|
||||||
void onConnected();
|
void onConnected();
|
||||||
void onOutput(String data);
|
void onOutput(String data);
|
||||||
@@ -35,6 +40,7 @@ final class TerminalSocketClient {
|
|||||||
private BufferedOutputStream output;
|
private BufferedOutputStream output;
|
||||||
private volatile boolean closed;
|
private volatile boolean closed;
|
||||||
private Thread thread;
|
private Thread thread;
|
||||||
|
private Thread heartbeatThread;
|
||||||
|
|
||||||
TerminalSocketClient(Listener listener) {
|
TerminalSocketClient(Listener listener) {
|
||||||
this.listener = listener;
|
this.listener = listener;
|
||||||
@@ -97,6 +103,7 @@ final class TerminalSocketClient {
|
|||||||
"rows", rows
|
"rows", rows
|
||||||
);
|
);
|
||||||
listener.onConnected();
|
listener.onConnected();
|
||||||
|
startHeartbeat();
|
||||||
readLoop();
|
readLoop();
|
||||||
} catch (Exception error) {
|
} catch (Exception error) {
|
||||||
if (!closed) {
|
if (!closed) {
|
||||||
@@ -123,10 +130,39 @@ final class TerminalSocketClient {
|
|||||||
if (port == -1) {
|
if (port == -1) {
|
||||||
port = "wss".equalsIgnoreCase(uri.getScheme()) ? 443 : 80;
|
port = "wss".equalsIgnoreCase(uri.getScheme()) ? 443 : 80;
|
||||||
}
|
}
|
||||||
|
Socket raw = new Socket();
|
||||||
|
raw.connect(new InetSocketAddress(uri.getHost(), port), SOCKET_CONNECT_TIMEOUT_MS);
|
||||||
|
Socket connected;
|
||||||
if ("wss".equalsIgnoreCase(uri.getScheme())) {
|
if ("wss".equalsIgnoreCase(uri.getScheme())) {
|
||||||
return SSLSocketFactory.getDefault().createSocket(uri.getHost(), port);
|
connected = ((SSLSocketFactory) SSLSocketFactory.getDefault())
|
||||||
|
.createSocket(raw, uri.getHost(), port, true);
|
||||||
|
} else {
|
||||||
|
connected = raw;
|
||||||
}
|
}
|
||||||
return new Socket(uri.getHost(), port);
|
connected.setKeepAlive(true);
|
||||||
|
connected.setTcpNoDelay(true);
|
||||||
|
connected.setSoTimeout(SOCKET_READ_TIMEOUT_MS);
|
||||||
|
return connected;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startHeartbeat() {
|
||||||
|
heartbeatThread = new Thread(() -> {
|
||||||
|
while (!closed) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(HEARTBEAT_INTERVAL_MS);
|
||||||
|
if (!closed) {
|
||||||
|
sendFrame(9, new byte[0]);
|
||||||
|
}
|
||||||
|
} catch (InterruptedException ignored) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
return;
|
||||||
|
} catch (Exception error) {
|
||||||
|
closeSocketQuietly();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, "terminal-ws-heartbeat");
|
||||||
|
heartbeatThread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handshake(URI uri) throws Exception {
|
private void handshake(URI uri) throws Exception {
|
||||||
@@ -279,6 +315,10 @@ final class TerminalSocketClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void closeSocketQuietly() {
|
private void closeSocketQuietly() {
|
||||||
|
if (heartbeatThread != null) {
|
||||||
|
heartbeatThread.interrupt();
|
||||||
|
heartbeatThread = null;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
if (socket != null) {
|
if (socket != null) {
|
||||||
socket.close();
|
socket.close();
|
||||||
|
|||||||
Reference in New Issue
Block a user