簡體   English   中英

Flatbuffers 如何在一條消息中發送 uint16_t 標頭、size_t 正文大小和 uint8_t 緩沖區

[英]Flatbuffers how to send in one message uint16_t header, size_t body size and uint8_t buffer

我正在使用FlatBuffers & Boost ASIO

我想用以下順序形成一條消息:

  1. uint16_t 標頭標識符。 (固定大小消息)
  2. size_t 正文大小 <- 這樣閱讀部分就可以知道它需要閱讀多長時間的消息。 (固定大小消息)
  3. Flatbuffers 對象。 (動態大小消息,大小在2處發送)

所以對於前兩個,我是如何將它們組合起來發送的:

size_t const header_length = 8;
size_t const body_size_b = 8;

ServerOpcode opc;
opc = ServerOpcode::SMSG_LOGIN_REQUEST_RESPONSE_TEST;
std::string header = std::to_string(opc);
flatbuffers::FlatBufferBuilder builder;
auto name = builder.CreateString("Orc MONSTER");
auto accountRole = Vibranium::CreateAccountRole_Packet(builder,1,name);
builder.Finish(accountRole);
size_t size = builder.GetSize();
uint8_t *buf = builder.GetBufferPointer();

std::array<char, header_length + body_size_b> buffer{};
std::copy(header.begin(), header.end(), buffer.begin());
std::copy(std::to_string(size).begin(), std::to_string(size).end(), buffer.begin() + header_length);
// How can I add builder here at third place ?
boost::asio::write(s, boost::asio::buffer(buffer,sizeof(buffer)));

我不知道如何在前兩個之后附加 flatbuffers 緩沖區。

我的問題是:

如何按順序統一所有三個並將它們發送到一個boost::asio::write

你不需要 1) 和 2)。 要獲得 1), file_identifier在您的架構中聲明一個file_identifier ,然后將該標識符傳遞給Finish 要獲得 2) 使用FinishSizePrefixed 所有這些都在您仍然不想閱讀的文檔中。

再說一次,這是一種二進制格式,所以我不確定你在用std::to_string做什么,但它不會工作。 你只需要寫buf的內容。 我不知道如何將它與 Boost 一起使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM