一个二进制网络数据解析模块(与C++通信)
因项目需要,实现一个nodejs模块** node-cppMsg**,用于与c/c++进行二进制数据通信:
- 解析 C/C++传来的二进制数据为json对象;
- 编码 json对象为c/c++能够解析二进制数据;
模块安装: npm install cppmsg 使用,参见 test.js
/*
* Created by sdb on 2/25/16.
*/
var cppMsg = require('cppmsg');
var msg_def = {
msgHead:[
['mainType','int32'],
['subType', 'int32']
]
};
var msg = new cppMsg.msg(
[
['reg','int32'],
['chkCode','int32'],
['iType','int32'],
['bMonitor', 'bool'],
['workPath','string',10],
['processID','uint32'],
['testObj','object', msg_def.msgHead],
['testint64','int64']
]
);
var buff = msg.encodeMsg( {
reg : 2,
chkCode : 0,
iType : 2,
bMonitor : false,
workPath : 'no 你 work',
processID : 1234,
testObj :{
mainType : 0x01020304,
subType : 0x0A0B0C0D
},
testint64 : 0xCDEF
} );
console.log( buff );
var data = msg.decodeMsg( buff );
console.log( data );