c++ - node.js 到 c++ 协议缓冲区
问题描述
我在 node.js 中创建了一个协议缓冲区,我想在 node c++ 插件中对其进行解码。
在节点中,我创建缓冲区并将其发送到 c++ 插件:
var Message = root.lookupType(tutorial.AddressBook);
var payload = { people: [{name: "name", id: 23, email: "sdfsdf", phones: [{ number: "12", type: 1}], last_updated: 3}]};
var errMsg = Message.verify(payload);
if (errMsg)
throw Error(errMsg);
var message = Message.create(payload);
var buffer = Message.encode(message).finish();
const cppResult = addons.myNanMethod(buffer);
那么我应该如何在 C++ 中解码呢?ParseFromString 没有帮助。我收到运行时错误“节点:符号查找错误:path/to/my/addon。”
char* data = (char*) node::Buffer::Data(info[0]->ToObject());
std::string datastring data, node::Buffer::Length(info[0])););
// Verify that the version of the library that we linked against is
// compatible with the version of the headers we compiled against.
GOOGLE_PROTOBUF_VERIFY_VERSION;
tutorial::AddressBook address_book;
{
// Read the existing address book.
if (!address_book.ParseFromString(&datastring)) {
cerr << "Failed to parse address book." << endl;
errorCode = 1;
}
}
解决方案
推荐阅读
- c++ - 从 C++ 中的地址引用返回值
- node.js - PythonBridge:如何正确设置子进程的当前工作目录?
- python - 陷入无限循环。Python
- elasticsearch - 使用数组项作为键的 ElasticSearch 聚合
- vue.js - 如何在Vuetify组件中将光标默认更改为指针
- json - 使用 JOLT 从给定的 json 值列表中创建“键 - 值”对 json
- flutter - Flutter Riverpod:如何实现 FutureProvider?
- apache-camel - Apache Camel - 如何使整个路由中的值都可以访问?
- numpy - 如何在嵌套列表中选择“列”?
- sql-server - 删除具有非法名称的默认约束?