Commit c9437270 authored by neverlord's avatar neverlord

optimized outgoing messaging

parent d1f86cf2
......@@ -72,12 +72,6 @@ class binary_serializer : public serializer
void write_tuple(size_t size, primitive_variant const* values);
/**
* @brief Takes the internal buffer and returns it.
*
*/
std::pair<size_t, char*> take_buffer();
/**
* @brief Returns the number of written bytes.
*/
......@@ -88,6 +82,10 @@ class binary_serializer : public serializer
*/
char const* data() const;
size_t sendable_size() const;
char const* sendable_data();
/**
* @brief Resets the internal buffer.
*/
......
......@@ -41,6 +41,7 @@ using std::enable_if;
namespace {
constexpr size_t chunk_size = 512;
constepxr size_t ui32_size = sizeof(std::uint32_t);
} // namespace <anonymous>
......@@ -133,6 +134,7 @@ void binary_serializer::acquire(size_t num_bytes)
{
if (!m_begin)
{
num_bytes += ui32_size;
size_t new_size = chunk_size;
while (new_size <= num_bytes)
{
......@@ -140,7 +142,7 @@ void binary_serializer::acquire(size_t num_bytes)
}
m_begin = new char[new_size];
m_end = m_begin + new_size;
m_wr_pos = m_begin;
m_wr_pos = m_begin + ui32_size;
}
else
{
......@@ -203,22 +205,26 @@ void binary_serializer::write_tuple(size_t size,
}
}
std::pair<size_t, char*> binary_serializer::take_buffer()
size_t binary_serializer::sendable_size() const
{
char* begin = m_begin;
char* end = m_end;
m_begin = m_end = m_wr_pos = nullptr;
return { static_cast<size_t>(end - begin), begin };
return static_cast<size_t>(m_wr_pos - m_begin);
}
size_t binary_serializer::size() const
{
return static_cast<size_t>(m_wr_pos - m_begin);
return sendable_size() - ui32_size;
}
char const* binary_serializer::data() const
{
return m_begin + ui32_size;
}
const char* binary_serializer::data() const
char const* binary_serializer::sendable_data()
{
return m_begin;
auto s = static_cast<std::uint32_t>(size());
memcpy(m_begin, &s, ui32_size);
return m_begin();
}
void binary_serializer::reset()
......
......@@ -73,10 +73,8 @@ void mailman_loop()
bs << msg;
auto size32 = static_cast<std::uint32_t>(bs.size());
DEBUG("--> " << to_string(msg));
// write size of serialized message
auto sent = ::send(peer_fd, &size32, sizeof(std::uint32_t), 0);
if ( sent != static_cast<int>(sizeof(std::uint32_t))
|| static_cast<int>(bs.size()) != ::send(peer_fd, bs.data(), bs.size(), 0))
auto sent = ::send(peer_fd, bs.sendable_data(), bs.sendable_size());
if (sent != bs.sendable_size())
{
disconnect_peer = true;
DEBUG("too few bytes written");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment