Skip to content

Commit

Permalink
Catch inputStream error
Browse files Browse the repository at this point in the history
  • Loading branch information
JingMatrix committed Jul 11, 2023
1 parent 5aba2f6 commit b061715
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions app/src/main/java/org/matrix/chromext/devtools/WebSocketClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,32 @@ class DevToolClient(tabId: String) : LocalSocket() {
}

fun listen(callback: (JSONObject) -> Unit = { msg -> Log.d(msg.toString()) }) {
while (true) {
val type = inputStream.read()
if (type == 0x80 or 0x1) {
var len = inputStream.read()
if (len == 0x7e) {
len = inputStream.read() shl 8
len += inputStream.read()
} else if (len == 0x7f) {
len = 0
for (i in 0 until 8) {
len = len or (inputStream.read() shl (8 * (7 - i)))
runCatching {
while (true) {
val type = inputStream.read()
if (type == 0x80 or 0x1) {
var len = inputStream.read()
if (len == 0x7e) {
len = inputStream.read() shl 8
len += inputStream.read()
} else if (len == 0x7f) {
len = 0
for (i in 0 until 8) {
len = len or (inputStream.read() shl (8 * (7 - i)))
}
} else if (len > 0x7d) {
throw Exception("Payload from server has invalid length byte ${len}")
}
callback(JSONObject(String(inputStream.readNBytes(len))))
} else {
throw Exception("Invalid frame type ${type} received from devtools server")
}
}
} else if (len > 0x7d) {
Log.e("Payload from server has invalid length byte ${len}")
break
}
runCatching { callback(JSONObject(String(inputStream.readNBytes(len)))) }
.onFailure { Log.e("Fail to fetch data of type ${type} and length ${len}") }
} else {
Log.e("Invalid frame type ${type} received from devtools server")
break
}
}
.onFailure {
Log.e("Fails when listening at tab ${tabId}: ${it.message}")
close()
}
}
}

Expand Down

0 comments on commit b061715

Please sign in to comment.