Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 38a76a2e authored by Simon Chan's avatar Simon Chan
Browse files

fix(adb): handle connection lost

parent 6013cf9f
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -182,6 +182,9 @@ function _Connect(): JSX.Element | null {
                        })
                    );
                    device = await Adb.authenticate({ readable, writable }, CredentialStore, undefined);
                    device.disconnected.then(() => {
                        globalState.setDevice(undefined, undefined);
                    });
                    globalState.setDevice(selectedBackend, device);
                } catch (e) {
                    device?.dispose();
+2 −0
Original line number Diff line number Diff line
@@ -134,6 +134,8 @@ export class Adb {

    private readonly packetDispatcher: AdbPacketDispatcher;

    public get disconnected() { return this.packetDispatcher.disconnected; }

    private _protocolVersion: number | undefined;
    public get protocolVersion() { return this._protocolVersion; }

+13 −5
Original line number Diff line number Diff line
import { AsyncOperationManager } from '@yume-chan/async';
import { AsyncOperationManager, PromiseResolver } from '@yume-chan/async';
import { AutoDisposable, EventEmitter } from '@yume-chan/event';
import { AdbCommand, AdbPacket, calculateChecksum, type AdbPacketCore, type AdbPacketInit } from '../packet.js';
import { AbortController, WritableStream, WritableStreamDefaultWriter, type ReadableWritablePair } from '../stream/index.js';
@@ -29,6 +29,9 @@ export class AdbPacketDispatcher extends AutoDisposable {
    public calculateChecksum = true;
    public appendNullToServiceString = true;

    private _disconnected = new PromiseResolver<void>();
    public get disconnected() { return this._disconnected.promise; }

    private readonly incomingSocketEvent = this.addDisposable(new EventEmitter<AdbIncomingSocketEventArgs>());
    public get onIncomingSocket() { return this.incomingSocketEvent.event; }

@@ -69,18 +72,21 @@ export class AdbPacketDispatcher extends AutoDisposable {
                    } catch (e) {
                        this.errorEvent.fire(e as Error);

                        this.dispose();

                        // Throw error here will stop the pipe
                        // But won't close `readable` because of `preventCancel: true`
                        throw e;
                    }
                }
                },
            }), {
                preventCancel: false,
                signal: this._abortController.signal,
            })
            .catch(() => { });
            .then(() => {
                this.dispose();
            }, () => {
                // TODO: AdbPacketDispatcher: reject `_disconnected` when pipe errored?
                this.dispose();
            });

        this._writer = connection.writable.getWriter();
    }
@@ -243,6 +249,8 @@ export class AdbPacketDispatcher extends AutoDisposable {

        this._writer.releaseLock();

        this._disconnected.resolve();

        super.dispose();
    }
}
+3 −2
Original line number Diff line number Diff line
@@ -202,9 +202,10 @@ export class StructDeserializeStream<T extends Struct<any, any, any, any>>
            async write(chunk) {
                await incomingStreamController.enqueue(chunk);
            },
            abort() {
                incomingStreamController.close();
            },
            close() {
                // @ts-ignore
                console.log('deserialization stream closed');
                incomingStreamController.close();
            },
        });