从TS到C ++的flatbuffer无法正常工作

时间:2018-08-12 14:51:26

标签: c++ typescript flatbuffers

我有一个message的平面缓冲区架构:

rm -rf node_modules && npm install
我在TypeScript中构造(写入)并在C ++中接收(读取)的

table NodeConstructionInfo {
    type:string (id: 0, required);          
    name:string (id: 1, required);         
}

table AddNodeRequest {
    graphId:string (id:0, required);            
    node:NodeConstructionInfo (id:1, required);
}

在C ++中,我收到356个字节(let builder = new flatbuffers.Builder(356); let offGraphId = builder.createString("2992ebff-c950-4184-8876-5fe6ac029aa5"); let offType = builder.createString("MySuperDuperNode"); let offName = builder.createString("DummyNode"); sz.NodeConstructionInfo.startNodeConstructionInfo(builder); sz.NodeConstructionInfo.addName(builder, offName); sz.NodeConstructionInfo.addType(builder, offType); let off = sz.NodeConstructionInfo.endNodeConstructionInfo(builder); sz.AddNodeRequest.startAddNodeRequest(builder); sz.AddNodeRequest.addGraphId(builder, offGraphId); sz.AddNodeRequest.addNode(builder, off); off = sz.AddNodeRequest.endAddNodeRequest(builder); builder.finish(off); let requestPayload = builder.asUint8Array(); ),并尝试通过以下操作对其进行验证

requestPayload

flatbuffers::Verifier v(buffer.getData(), buffer.getSize()); v.VerifyBuffer<AddNodeRequest>(); 中始终失败的位置:

<flatbuffers/flatbuffers.h>

我缺少一些重要的细节吗? 缓冲区看起来像

template<typename T>
bool VerifyBufferFromStart(const char *identifier, const uint8_t *start) 
{
   ...
   // Call T::Verify, which must be in the generated code for this type.
   auto o = VerifyOffset(start); <--- HERE (the first read flatbuffers::uoffset_t should not be 0 (don't know why)?)
   ...
}

使用C ++编写的TypeScript读取消息可以工作...(?) flatbuffer版本1.9.0

2 个答案:

答案 0 :(得分:1)

正如您的缓冲区转储所示,问题在于它包含很多前导零。 FlatBuffer在构造时实际上可能包含前导零(因为它是在较大的缓冲区中向后构造的),但是asUint8Array通常负责将其修整为仅所需的数组。因此,要么您实际上没有在实际代码中使用asUint8Array,要么零被其他一些代码前置。

答案 1 :(得分:0)

问题在于职位的位置以及如何发送数据。必须将缓冲区切片到ByteBuffer中指定的位置,并且帖子必须像Blob一样发送。

builder.finish(end);
var buffer: flatbuffers.ByteBuffer = builder.dataBuffer();
var data: Uint8Array = buffer.bytes().slice(buffer.position());
this.http.post(environment.apiRoot + "hello", new Blob([data])).subscribe(
      () => {},
      (error) => {}
    );