let workerPort : ThreadWorkerGlobalScope = worker.workerPort;
/**
* Defines the event handler to be called when the worker thread receives a message sent by the host thread.
* The event handler is executed in the worker thread.
*
* @param
const bufferCache: buffer.Buffer = buffer.from([])
workerPort.onmessage = async (e: MessageEvents) => {
let byteArray = e.data as Uint8Array;
let byteBuffer = buffer.from(byteArray);
bufferCache = buffer.concat([bufferCache, byteBuffer]);
try {
let byteArrayBuffer = bufferCache.buffer;
if (byteArrayBuffer.byteLength == 0) {
return;
}
// 解析报文头
let protoResponse: ProtoResponse = await parser.decodePackage(byteArrayBuffer);
// 如果出现拆包问题,则先缓存数据,接收到剩余数据后再做解析
if (protoManager.packageHeadLength + protoResponse.bodyLength > protoResponse.packageBuffer.length) {
return;
}
// 将完整报文从缓存数据中移除
let sliceByteBuffer = bufferCache.subarray(protoManager.packageHeadLength + protoResponse.bodyLength);
bufferCache = sliceByteBuffer
// 解析完整报文
if (protoResponse.bodyArrayBuffer) {
let task = new taskpool.Task(decodePackageBody, protoResponse.bodyArrayBuffer);
let decodePackageBodyResult = await taskpool.execute(task) as ProtoResponse;
workerPort.postMessage(decodePackageBodyResult);
}
} catch (error) {
hilog.error(0x0000, TAG, error.message)
}
}
/**
* Defines the event handler to be called when the worker receives a message that cannot be deserialized.
* The event handler is executed in the worker thread.
*
* @param
workerPort.onmessageerror = (e: MessageEvents) => {
}
/**
* Defines the event handler to be called when an exception occurs during worker execution.
* The event handler is executed in the worker thread.
*
* @param
workerPort.onerror = (e: ErrorEvent) => {
}
@Concurrent
async function decodePackageBody(arrayBuffer: ArrayBuffer) {
// 对报文体进行解析
return response
}