lerys
lerys

Reputation: 15

WriteHandler from boost::asio::async_write doesn't work properly when connection is dropped (firewall / manual disconecting the network)

I've been trying to write a client-server application using boost::asio, overall the application works great but I've stumbled over a problem regarding the data provided by 'WriteHandler' ( async_write function) when the connoction (between client <> server) is droped by a firewalll or by manualy disabling a newtwork card.

Here is the code snippet:

void write(const CommunicatorMessage & msg, std::function<void(bool)> writeCallback)
        {
            io_service.dispatch(
                [this, msg, writeCallback]()
            {
                boost::asio::async_write(socket, boost::asio::buffer(msg.data(), msg.length()), [this, msg, writeCallback](boost::system::error_code ec, std::size_t length)
                {
                    writeCallback(ec != 0);
                    if (!ec)
                    {
                        LOG_DEBUG("Number of bytes written into the buffer: " << length << " Error code: " << ec);
                        LOG_DEBUG("Successfully send msg: " << msg);
                    }
                    else
                    {
                        LOG_ERROR("Failed sending msg: " << msg);
                        throw std::exception(ec.message().c_str());
                    }
                });
            });
        }

The data from the Writehandler is valid (the error code is 0, bytes_transferred are correct), even if the data doesn't arrive on the server. I've tracked with Wireshark the whole flow( here's a link with the a screenshot Link)

Upvotes: 1

Views: 1401

Answers (1)

Galimov Albert
Galimov Albert

Reputation: 7357

When async_write send you success it means it sucessfully written to kernel's buffer. But, its not guaranteed packet was delivered.

Upvotes: 3

Related Questions