Last active
October 30, 2015 15:33
-
-
Save falconair/625c354fb4b92dd6ef81 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//DISCLAIMER: If something looks trivially wrong, it probably is. I'm a java developer, not yet a C++ dev. | |
//====================Schema==================== | |
union FinalEventUnion | |
{ | |
CharactersLoadedEvent, | |
SomeOtherEvent, | |
AnotherEvent, | |
... | |
} | |
table FinalEvent | |
{ | |
id: uint; | |
input_time: ulong; | |
final_event: FinalEventUnion; | |
} | |
table CharactersLoadedEvent | |
{ | |
characters: [string];//max string size 16, max array size 256 | |
} | |
//====================C++ code (encoder)==================== | |
std::tuple<size_t, uint8_t *> convert_characters_loaded_event(internal_event_format::characters_loaded_event &ple, uint64_t time){ | |
flatbuffers::FlatBufferBuilder fbb;//Looks like the problem may be here! fbb goes out of scope at the end of this method, but is still used in the main function! | |
std::vector<flatbuffers::Offset<flatbuffers::String>> characters_; | |
for (auto & element : ple.characters_)//there is a single entry in characters_ {"AOL"} | |
{ | |
auto raw_str = element.c_str(); //element contains "AOL" | |
auto fb_str = fbb.CreateString(raw_str);//after this, fbb.buf_.buf_ is [...,'\x3','\0','\0','\0','A','O','L','\0'] (the end of 1024 byte array) | |
characters_.push_back(fb_str); | |
} | |
auto characters = fbb.CreateVector(characters_);//after this, fbb.buf_.buf_ is [...,'\x1','\0','\0','\0','\x4','\0','\0','\0','\x3','\0','\0','\0','A','O','L','\0'] (the end of 1024 byte array) | |
external_event_format::CharactersLoadedEventBuilder e(fbb); | |
e.add_characters(characters); | |
auto inner_event = e.Finish(); | |
//after this, fbb.buf_.buf_ is [...,'\x6','\0','\b','\0','\x4','\0','\x6','\0','\0','\0','\x4','\0','\0','\0',\x1','\0','\0','\0','\x4','\0','\0','\0','\x3','\0','\0','\0','A','O','L','\0'] (the end of 1024 byte array) | |
auto inner_event_type = external_event_format::FinalEventUnion_CharactersLoadedEvent; | |
std::tuple<size_t, uint8_t *> buffer_tuple = build_output_event(fbb, inner_event_type, inner_event.Union(), time); | |
return buffer_tuple; | |
} | |
std::tuple<size_t, uint8_t *> build_output_event(flatbuffers::FlatBufferBuilder &fbb, | |
const FinalEventUnion output_event_type, | |
const flatbuffers::Offset<void> &inner_event, | |
uint64_t ns_time) | |
{ | |
FinalEventBuilder u(fbb); | |
u.add_input_time(ns_time); | |
u.add_final_event_type(final_event_type); | |
u.add_final_event(inner_event); | |
//u.Finish();//Removed after Wouter's comment regarding fbb.Finish() | |
FinishFinalEventBuffer(fbb, u.Finish());//Added after Wouter's comment regarding fbb.Finish() | |
uint64_t buffer_size = fbb.GetSize(); | |
auto buffer = fbb.GetBufferPointer(); | |
//all previous operations add to fbb's buffer, while "AOL" remains at bottom (of 1024 byte buffer fbb.buf_.buf_) | |
//buffer seems to have same contents, but shifted, "AOL\0" now shows up at byte 68 in buffer, while it was at byte 1020 in fbb.buf_.buf_ | |
return std::make_tuple(buffer_size, buffer); | |
} | |
struct data_chunk{ char data[5000]; } | |
void decode(data_chunk &data){ | |
auto event = GetFinalEvent(data.data); | |
switch(event->final_event_type()){ | |
case FinalEventUnion::FinalEventUnion_CharactersLoadedEvent: | |
process(reinterpret_cast<const CharactersLoadedEvent*>(event->final_event()); | |
break; | |
... | |
} | |
} | |
void process(const CharactersLoadedEvent* ev){ | |
auto v = convert_to_std_vector(ev->characters()); | |
... | |
} | |
void convert_to_std_vector(const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *e){ | |
std::vector<std::string> v; | |
for (flatbuffers::uoffset_t i = 0; i < e->size(); i++) | |
{ | |
auto element = e->Get(i); | |
v.push_back(element->str()); | |
} | |
return v; | |
} | |
//====================C++ code (main)==================== | |
//main | |
//Put on the wire | |
... | |
std::tuple<size_t, uint8_t*> buffer_tuple = convert_characters_loaded_event(ple, time_stamp); | |
std::get<0>(buffer_tuple);//size of the byte buffer | |
//by this time, the contents of the byte buffer seem to have completely changed and I don't see "AOL" anywhere! <==============PROBLEM! | |
std::get<1>(buffer_tuple);//byte buffer to put on the wire | |
//Take off the wire | |
... | |
data_chunk data = ... | |
decode(data); | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment