Skip to content

Commit b061715

Browse files
committed
Catch inputStream error
1 parent 5aba2f6 commit b061715

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

app/src/main/java/org/matrix/chromext/devtools/WebSocketClient.kt

+24-21
Original file line numberDiff line numberDiff line change
@@ -56,29 +56,32 @@ class DevToolClient(tabId: String) : LocalSocket() {
5656
}
5757

5858
fun listen(callback: (JSONObject) -> Unit = { msg -> Log.d(msg.toString()) }) {
59-
while (true) {
60-
val type = inputStream.read()
61-
if (type == 0x80 or 0x1) {
62-
var len = inputStream.read()
63-
if (len == 0x7e) {
64-
len = inputStream.read() shl 8
65-
len += inputStream.read()
66-
} else if (len == 0x7f) {
67-
len = 0
68-
for (i in 0 until 8) {
69-
len = len or (inputStream.read() shl (8 * (7 - i)))
59+
runCatching {
60+
while (true) {
61+
val type = inputStream.read()
62+
if (type == 0x80 or 0x1) {
63+
var len = inputStream.read()
64+
if (len == 0x7e) {
65+
len = inputStream.read() shl 8
66+
len += inputStream.read()
67+
} else if (len == 0x7f) {
68+
len = 0
69+
for (i in 0 until 8) {
70+
len = len or (inputStream.read() shl (8 * (7 - i)))
71+
}
72+
} else if (len > 0x7d) {
73+
throw Exception("Payload from server has invalid length byte ${len}")
74+
}
75+
callback(JSONObject(String(inputStream.readNBytes(len))))
76+
} else {
77+
throw Exception("Invalid frame type ${type} received from devtools server")
78+
}
7079
}
71-
} else if (len > 0x7d) {
72-
Log.e("Payload from server has invalid length byte ${len}")
73-
break
7480
}
75-
runCatching { callback(JSONObject(String(inputStream.readNBytes(len)))) }
76-
.onFailure { Log.e("Fail to fetch data of type ${type} and length ${len}") }
77-
} else {
78-
Log.e("Invalid frame type ${type} received from devtools server")
79-
break
80-
}
81-
}
81+
.onFailure {
82+
Log.e("Fails when listening at tab ${tabId}: ${it.message}")
83+
close()
84+
}
8285
}
8386
}
8487

0 commit comments

Comments
 (0)