I am writing an application using boost.asio. I've an object of type boost::asio::ip::tcp::socket
and (of course) I've boost::asio::io_context
which run
's function was called from only one thread. For writing data to the socket there are a couple of ways but currently I use socket's function async_write_some
, something like the code below:
void tcp_connection::write(packet_ptr packet)
{
m_socket.async_write_some(boost::asio::buffer(packet->data(), packet->size()),
std::bind(&tcp_connection::on_write, this, std::placeholders::_1, std::placeholders::_2, packet));
}
There is another function in boost::asio
namespace - async_write
. And the documentation of async_write
says:
This operation is implemented in terms of zero or more calls to the stream's async_write_some function, and is known as a composed operation. The program must ensure that the stream performs no other write operations (such as async_write, the stream's async_write_some function, or any other composed operations that perform writes) until this operation completes.
In async_write_some
's documentation there is no such kind of 'caution'.
That's a little bit confusing to me and here I've got the following questions:
- Is it safe to call
async_write_some
without waiting for the previous call to be finished? As far as I understood from boost's documentation I shouldn't do that withasync_write
, but what aboutasync_write_some
? - If yes, is the order in which the data is written to the socket the same as the functions were called? I mean if I called
async_write_some(packet1)
andasync_write_some(packet2)
- are the packets going to be written to the socket in the same order? - Which function I should use? What is the difference between them?
- What is the reason that it's not safe to call
async_write
while the previous one hasn't finished yet?