fix(adb): read socket too slow can cause data loss

This commit is contained in:
Simon Chan 2023-10-11 00:25:07 +08:00
parent dce44ae9ac
commit 1aa7a92d2c
No known key found for this signature in database
GPG key ID: A8B69F750B9BCEDD
7 changed files with 190 additions and 164 deletions

View file

@ -87,20 +87,8 @@ export class AdbPacketDispatcher implements Closeable {
await this.#handleClose(packet);
break;
case AdbCommand.Write:
if (this.#sockets.has(packet.arg1)) {
await this.#sockets
.get(packet.arg1)!
.enqueue(packet.payload);
await this.sendPacket(
AdbCommand.OK,
packet.arg1,
packet.arg0,
);
break;
}
throw new Error(
`Unknown local socket id: ${packet.arg1}`,
);
await this.#handleWrite(packet);
break;
case AdbCommand.Open:
await this.#handleOpen(packet);
break;
@ -200,6 +188,17 @@ export class AdbPacketDispatcher implements Closeable {
// the device may also respond with two `CLSE` packets.
}
async #handleWrite(packet: AdbPacketData) {
const socket = this.#sockets.get(packet.arg1);
if (!socket) {
throw new Error(`Unknown local socket id: ${packet.arg1}`);
}
await socket.enqueue(packet.payload);
await this.sendPacket(AdbCommand.OK, packet.arg1, packet.arg0);
return;
}
addReverseTunnel(service: string, handler: AdbIncomingSocketHandler) {
this.#incomingSocketHandlers.set(service, handler);
}