KLBuffer
KLBuffer
是一个用于Nodejs中处理与C++交互过程中的数字数组与Buffer互相转化的工具类;支持数据类型有:char、uchar、short、ushort、int、uint、float、double等
安装
npm install kl-buffer
使用
1. 读写值
let klBuffer = KLBuffer.alloc(2);
console.log(klBuffer.ucharArray);
klBuffer.ucharArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log(klBuffer.ucharArray);
console.log(klBuffer.uchar[5]);
klBuffer.uchar[5] = 50;
console.log(klBuffer.uchar[5]);
klBuffer.doubleArray = [1.5, 2.5];
console.log(klBuffer.doubleArray);
console.log(klBuffer.double[0]);
klBuffer.double[0] = 3.5;
console.log(klBuffer.double[0]);
2. 从已有buffer中拷贝数据
2.1 API
type NumberArray = Int8Array | Uint8Array | Uint8ClampedArray |
Int16Array | Uint16Array |
Int32Array | Uint32Array |
BigInt64Array | BigUint64Array |
Float32Array | Float64Array;
type DataSource = Buffer | NumberArray | DataView | number;
KLBuffer.from(
data: DataSource,
size: number,
deepCopy?: boolean,
) => Buffer|null
2.2 深拷贝
let klBuffer1 = KLBuffer.alloc(10);
let klBuffer2 = KLBuffer.alloc(10);
klBuffer1.ucharArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log(klBuffer2.ucharArray);
klBuffer2.from(klBuffer1.ptr, 10, true);
console.log(klBuffer2.ucharArray);
klBuffer2.uchar[2] = 30;
console.log(klBuffer2.ucharArray);
console.log(klBuffer1.ucharArray);
console.log(klBuffer1.ptrVal, klBuffer2.ptrVal);
2.3 浅拷贝
let klBuffer1 = KLBuffer.alloc(10);
let klBuffer2 = KLBuffer.alloc(10);
klBuffer1.ucharArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log(klBuffer2.ucharArray);
klBuffer2.from(klBuffer1.ptr, 10, false);
console.log(klBuffer2.ucharArray);
klBuffer2.uchar[2] = 30;
console.log(klBuffer2.ucharArray);
console.log(klBuffer1.ucharArray);
console.log(klBuffer1.ptrVal, klBuffer2.ptrVal);
2.4 跨线程传递数据
使用过程中需保证buffer数据没有被回收!
const { KLBuffer } = require('kl-buffer');
const {
Worker,
isMainThread,
setEnvironmentData,
getEnvironmentData,
} = require('worker_threads');
if (isMainThread) {
const klBuffer = KLBuffer.alloc(2);
setEnvironmentData('klBuffer', [klBuffer.ptrVal, klBuffer.size]);
new Worker(__filename);
setInterval(() => {
klBuffer.uchar[0] = klBuffer.uchar[0] + 1;
console.log(klBuffer.ptrVal, klBuffer.ucharArray, 'main');
}, 1000)
} else {
const [ptrVal, size] = getEnvironmentData('klBuffer');
const klBuffer = KLBuffer.alloc(size, ptrVal);
setInterval(() => {
klBuffer.uchar[1] = klBuffer.uchar[1] + 1;
console.log(klBuffer.ptrVal, klBuffer.ucharArray, 'child');
}, 1000)
}