generated from ali/HarmonyTemplate
T2+T3: platform spike diag page + SSH wire/transport
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright (c) 2026 Haven for HarmonyOS contributors.
|
||||
* Port of Haven by GlassHaven (AGPL-3.0). See LICENSE.
|
||||
*/
|
||||
|
||||
import { socket } from '@kit.NetworkKit';
|
||||
import { BusinessError } from '@kit.BasicServicesKit';
|
||||
import { RawSocket } from './Transport';
|
||||
import { Log } from '../common/Log';
|
||||
|
||||
/**
|
||||
* RawSocket adapter over @ohos.net.socket TCPSocket.
|
||||
* Event handlers are registered in the constructor, so chunks that arrive
|
||||
* between connect() resolution and SshTransport.start() are queued in
|
||||
* `pending` and flushed when the transport installs its callback.
|
||||
*/
|
||||
export class TcpRawSocket implements RawSocket {
|
||||
private static readonly TAG: string = 'TcpRawSocket';
|
||||
|
||||
private sock: socket.TCPSocket = socket.constructTCPSocketInstance();
|
||||
private pending: Uint8Array[] = [];
|
||||
private dataCb: ((chunk: Uint8Array) => void) | undefined = undefined;
|
||||
private closeCb: (() => void) | undefined = undefined;
|
||||
private closed: boolean = false;
|
||||
|
||||
constructor() {
|
||||
this.sock.on('message', (info: socket.SocketMessageInfo): void => {
|
||||
const chunk: Uint8Array = new Uint8Array(info.message);
|
||||
if (this.dataCb !== undefined) {
|
||||
this.dataCb(chunk);
|
||||
} else {
|
||||
this.pending.push(chunk);
|
||||
}
|
||||
});
|
||||
this.sock.on('close', (): void => {
|
||||
this.closed = true;
|
||||
if (this.closeCb !== undefined) {
|
||||
this.closeCb();
|
||||
}
|
||||
});
|
||||
this.sock.on('error', (err: BusinessError): void => {
|
||||
Log.error(TcpRawSocket.TAG, `socket error: ${err.message}`);
|
||||
});
|
||||
}
|
||||
|
||||
connect(host: string, port: number, timeoutMs: number): Promise<void> {
|
||||
const addr: socket.NetAddress = { address: host, port: port };
|
||||
const opts: socket.TCPConnectOptions = { address: addr, timeout: timeoutMs };
|
||||
return this.sock.connect(opts);
|
||||
}
|
||||
|
||||
send(bytes: Uint8Array): Promise<void> {
|
||||
return this.sock.send(u8ToArrayBuffer(bytes));
|
||||
}
|
||||
|
||||
onData(cb: (chunk: Uint8Array) => void): void {
|
||||
this.dataCb = cb;
|
||||
const queued: Uint8Array[] = this.pending;
|
||||
this.pending = [];
|
||||
for (let i = 0; i < queued.length; i++) {
|
||||
cb(queued[i]);
|
||||
}
|
||||
}
|
||||
|
||||
onClose(cb: () => void): void {
|
||||
this.closeCb = cb;
|
||||
if (this.closed) {
|
||||
cb();
|
||||
}
|
||||
}
|
||||
|
||||
close(): void {
|
||||
if (this.closed) {
|
||||
return;
|
||||
}
|
||||
this.closed = true;
|
||||
const done: Promise<void> = this.sock.close();
|
||||
done.catch((e: Object): void => {
|
||||
const err = e as BusinessError;
|
||||
Log.warn(TcpRawSocket.TAG, `close: ${err.message}`);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/** Copy a Uint8Array into a standalone ArrayBuffer (TCPSocket.send takes ArrayBuffer). */
|
||||
export function u8ToArrayBuffer(b: Uint8Array): ArrayBuffer {
|
||||
const out: ArrayBuffer = new ArrayBuffer(b.length);
|
||||
const view: Uint8Array = new Uint8Array(out);
|
||||
view.set(b);
|
||||
return out;
|
||||
}
|
||||
Reference in New Issue
Block a user