Created
September 21, 2017 23:45
-
-
Save nlyan/ae2fc5f0b3f0bb88e5e9fadc293ea4a4 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
template <typename T> | |
std::enable_if_t<std::is_base_of<flatbuffers::NativeTable, T>::value, | |
boost::system::error_code> | |
operator() (tcp::socket& socket, Message::Header const& header, T& body, | |
asio::yield_context ctx) const { | |
boost::system::error_code ec; | |
/* TODO: Inefficient and dangerous, huzzah! */ | |
std::vector<unsigned char> buffer; | |
buffer.resize (header.size); | |
asio::async_read (socket, | |
asio::buffer (buffer.data (), buffer.size ()), | |
asio::transfer_exactly (buffer.size ()), | |
ctx[ec]); | |
if (ec) { | |
return ec; | |
} | |
flatbuffers::Verifier fbv (buffer.data (), buffer.size ()); | |
if (!fbv.VerifyBuffer<typename T::TableType> ()) { | |
throw std::runtime_error ("Couldn't verify flatbuffer"); | |
} | |
auto root = | |
flatbuffers::GetRoot<typename T::TableType> (buffer.data ()); | |
root->UnPackTo (&body); | |
return ec; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment