Taobin-Recipe-Manager/client-electron/electron/adbaemonDirectSocketsDevice.ts
2024-03-15 14:10:24 +07:00

57 lines
1.4 KiB
TypeScript

import type { AdbDaemonDevice } from '@yume-chan/adb'
import { AdbPacket, AdbPacketSerializeStream } from '@yume-chan/adb'
import {
StructDeserializeStream,
UnwrapConsumableStream,
WrapReadableStream,
WrapWritableStream
} from '@yume-chan/stream-extra'
import { TCPSocket } from './socketToTCPSocket'
export interface AdbDaemonDirectSocketDeviceOptions {
host: string
port?: number
name?: string
unref?: boolean
}
export class AdbDaemonDirectSocketsDevice implements AdbDaemonDevice {
static isSupported(): boolean {
return true
}
#options: AdbDaemonDirectSocketDeviceOptions
readonly serial: string
get host(): string {
return this.#options.host
}
readonly port: number
get name(): string | undefined {
return this.#options.name
}
constructor(options: AdbDaemonDirectSocketDeviceOptions) {
this.#options = options
this.port = options.port ?? 5555
this.serial = `${this.host}:${this.port}`
}
async connect() {
const socket = new TCPSocket(this.host, this.port, {
noDelay: true,
unref: this.#options.unref
})
const { readable, writable } = await socket.opened
return {
readable: new WrapReadableStream(readable).pipeThrough(new StructDeserializeStream(AdbPacket)),
writable: new WrapWritableStream(writable)
.bePipedThroughFrom(new UnwrapConsumableStream())
.bePipedThroughFrom(new AdbPacketSerializeStream())
}
}
}