I write a client-server app which uses asynchronous boost asio networking (boost::asio::async_write
and boost::asio::async_read
) on server side and synchronous calls (boost::asio::write
and boost::asio::read
) on the client end. Because underneath I use protocol buffers, if I want to send a buffer from the client, first I send the payload size, then in the second call the payload body. Pseudocode for the client end:
void WriteProtobuf( std::string && body )
{
boost::system::error_code ec;
std::size_t dataSize = body.size();
// send the size
boost::asio::write( socket, boost::asio::buffer( reinterpret_cast<const char *>( &dataSize ), sizeof( dataSize ) ), ec );
// send the body
boost::asio::write( socket, boost::asio::buffer( body.data(), body.size() ), ec );
}
Pseudocode for the server end:
void ReadProtobuf()
{
std::size_t requestSize;
std::string body;
// read the size
boost::asio::async_read( socket, boost::asio::buffer( &requestSize, sizeof( requestSize ) ), [&requestSize, &body]() { // read the size
body.resize( requestSize );
// read the body
boost::asio::async_read( socket, boost::asio::buffer( body.data(), body.size() ), []() {
/* ... */
});
});
}
Now, it works just fine, but I observe a ~40ms latency in the second boost::asio:write call. I found an easy but not clean solution to work it around. I added the "confirmation" byte send from the server between the calls of write from client:
Pseudocode for the client end:
void WriteProtobuf( std::string && body )
{
boost::system::error_code ec;
std::size_t dataSize = body.size();
// send the size
boost::asio::write( socket, boost::asio::buffer( reinterpret_cast<const
char *>( &dataSize ), sizeof( dataSize ) ), ec );
char ackByte;
// read the ack byte
boost::asio::read( socket, boost::asio::buffer( ackByte, sizeof( ackByte ) ), ec );
// send the body
boost::asio::write( socket, boost::asio::buffer( body.data(), body.size() ), ec );
}
Pseudocode for the server end:
void ReadProtobuf()
{
std::size_t requestSize;
std::string body;
// read the size
boost::asio::async_read( socket, boost::asio::buffer( &requestSize, sizeof( requestSize ) ), [&requestSize, &body]() { // read the size
body.resize( requestSize );
char ackByte = 0;
// write the ack byte
boost::asio::async_write( socket, boost::asio::buffer( &ackByte, sizeof( ackByte ), []() {
// read the body
boost::asio::async_read( socket, boost::asio::buffer( body.data(), body.size() ), []() {
/* ... */
});
});
});
}
This removes the latency but still I would get rid of unnecessary communication and understand better why is it happening this way.