Files
dougal-software/lib/www/server/lib/binary/info.js
D. Berge 4db6d8dd7a Add custom binary format packing / unpacking.
This series of custom binary messages are an alternative to JSON /
GeoJSON when huge amounts of data needs to be transferred to and
processed by the client, such as a GPU-based map view showing all
the points for a prospect, or QC graphs, etc.
2025-07-26 19:05:00 +02:00

83 lines
1.9 KiB
JavaScript

const { MSGTYPE, PKTTYPE, HEADER_OFFSET, PACKET_OFFSET } = require('./constants');
function headerSize (msgType) {
switch (msgType) {
case MSGTYPE.BUNDLE:
return 4;
case MSGTYPE.PREPLOT:
// 1 * Uint8 + 1 * Uint32 + 1 * Uint16 + 1 * Uint16 + 1 * Int16
// 1 * 1 + 1 * 4 + 1 * 2 + 1 * 2 + 1 * 2 = 11
return 11;
case MSGTYPE.PREPLOT_RAWERROR_OPT:
return 12;
case MSGTYPE.RAW:
case MSGTYPE.FINAL:
case MSGTYPE.RAW_OPT:
case MSGTYPE.FINAL_OPT:
// 1 * Uint8 + 1 * Uint32 + 1 * Uint16 + 1 * Uint16 + 1 * Int16 + 1 * BigInt64
// 1 * 1 + 1 * 4 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 8 = 19
return 20;
default:
return; // undefined
}
}
function packetSize (msgType) {
switch (msgType) {
case MSGTYPE.BUNDLE:
return; // Doesn't apply to this msgType
case MSGTYPE.PREPLOT:
// 2 * Float32
return 8;
case MSGTYPE.RAW:
case MSGTYPE.FINAL:
// 2 * Float32 + 2 * Float32 + 1 * Int32
return 20;
case MSGTYPE.PREPLOT_RAWERROR_OPT:
// 2 * Float32 + 1 * Float32
return 12;
case MSGTYPE.RAW_OPT:
case MSGTYPE.FINAL_OPT:
// 2 * Float32 + 2 * Float32 + 1 * Int32
return 20;
default:
return; // undefined
}
}
function packetFormatType (msgType) {
switch (msgType) {
case MSGTYPE.BUNDLE:
return PKTTYPE.A;
case MSGTYPE.PREPLOT:
return PKTTYPE.B;
case MSGTYPE.RAW:
case MSGTYPE.FINAL:
return PKTTYPE.C;
case MSGTYPE.PREPLOT_RAWERROR_OPT:
return PKTTYPE.D;
case MSGTYPE.RAW_OPT:
case MSGTYPE.FINAL_OPT:
return PKTTYPE.E;
default:
return; // undefined
}
}
function isLittleEndian () {
const buffer = new ArrayBuffer(2);
const asUint8 = new Uint8Array(buffer);
const asUint16 = new Uint16Array(buffer);
asUint8[1] = 0xFF;
return asUint16[0] == 0xFF00;
}
module.exports = {
headerSize,
packetSize,
packetFormatType,
isLittleEndian
};