Here is how I create my message with FlatBuffers and how I send it over TCP with Boost ASIO.
flatbuffers::FlatBufferBuilder builder;
auto email = builder.CreateString("test@asd.dg");
auto password = builder.CreateString("test");
auto loginRequest = Vibranium::CreateLoginRequest(builder,email,password);
builder.FinishSizePrefixed(loginRequest);
size_t size = builder.GetSize();
uint8_t *buf = builder.GetBufferPointer();
//And here I am sending it with Boost ASIO.
boost::asio::write(s, boost::asio::buffer(buf,size));
What I would like to do is get a value of this enum:
enum ServerOpcode : uint16_t{
SMSG_AUTH_CONNECTION_RESPONSE = 0x001,
SMSG_LOGIN_REQUEST = 0x002,
SMSG_LOGIN_REQUEST_RESPONSE_TEST = 0xA99,
};
Let's say like so:
ServerOpcode opc;
opc = ServerOpcode::SMSG_LOGIN_REQUEST;
And prepend opc
in front of buf*
.
As uint16_t
is fixed size than on the receiving end i'll know that the ServerOpcdode
is always kept in the first two bytes.
So how can I prepend opc
infron of buf
is it even possible and how?
PS Note that the file_indetifier
of FlatBuffers is not a solution here.
You can allocate an intermediate buffer.
flatbuffers::FlatBufferBuilder builder;
auto email = builder.CreateString("test@asd.dg");
auto password = builder.CreateString("test");
auto loginRequest = Vibranium::CreateLoginRequest(builder,email,password);
builder.FinishSizePrefixed(loginRequest);
size_t size = builder.GetSize();
uint8_t *buf = builder.GetBufferPointer();
uint8_t *actualBuffer = new uint8_t[size + 2];
actualBuffer[1] = (code >> 8);
actualBuffer[0] = (code&0xFF);
memcpy(actualBuffer + 2, buf, size);
//And here I am sending it with Boost ASIO.
boost::asio::write(s, boost::asio::buffer(actualBuffer, size + 2));
delete[] actualBuffer;
I've chosen to encode the uint16_t code as Big Endian, as is good form for any sort of network communication I am familiar with, however you can of course also just do a memcpy(actualBuffer, &code, 2);
, at the risk of not knowing the endianess.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.