void tcp_adapter_proxy::async_tcp_write_buffer_drain()

in src/TcpAdapterProxy.cpp [1383:1454]


        void tcp_adapter_proxy::async_tcp_write_buffer_drain(tcp_adapter_context &tac, string service_id)
        {
            tcp_connection::pointer connection = get_tcp_connection(tac, service_id);
            if (!connection->socket_.is_open())
            {
                throw proxy_exception((boost::format("TCP socket is not open service id: %1%") % service_id).str());
            }
            static std::function<void(boost::system::error_code const &, size_t)> write_done;
            write_done = [&, service_id](boost::system::error_code const &ec, size_t bytes_written)
            {
                BOOST_LOG_SEV(log, trace) << "write done service id " << service_id;
                tcp_connection::pointer socket_write_connection = get_tcp_connection(tac, service_id);
                socket_write_connection->is_tcp_socket_writing_ = false;
                if (ec)
                {
                    if (socket_write_connection->on_tcp_error)
                    {
                        socket_write_connection->on_tcp_error(ec);
                        socket_write_connection->on_tcp_error = nullptr;
                    }
                    else
                    {
                        tcp_socket_error(tac, ec, service_id);
                    }
                }
                else
                {
                    BOOST_LOG_SEV(log, trace) << "Wrote " << bytes_written << " bytes to tcp socket";
                    bool had_space_before = tcp_has_enough_write_buffer_space(socket_write_connection);
                    socket_write_connection->tcp_write_buffer_.consume(bytes_written);
                    bool has_space_after = tcp_has_enough_write_buffer_space(socket_write_connection);
                    if (!had_space_before && has_space_after)
                    {
    #ifdef DEBUG
                        BOOST_LOG_SEV(log, debug) << "Just cleared enough buffer space in tcp write buffer. Re-starting async web socket read loop";
    #endif
                        async_web_socket_read_loop(tac);
                    }
                    if (socket_write_connection->tcp_write_buffer_.size() > 0)
                    {
                        socket_write_connection->is_tcp_socket_writing_ = true;
                        BOOST_LOG_SEV(log, debug) << "Write to tcp socket";
                        socket_write_connection->socket_.async_write_some(socket_write_connection->tcp_write_buffer_.data(), write_done);
                    }
                    else
                    {
                        if (socket_write_connection->on_tcp_write_buffer_drain_complete)
                        {
                            invoke_and_clear_handler(socket_write_connection->on_tcp_write_buffer_drain_complete);
                        }
                        BOOST_LOG_SEV(log, trace) << "TCP write buffer drain complete";
    #ifdef DEBUG
                        BOOST_LOG_SEV(log, trace) << "TCP write buffer drain complete";
    #endif
                    }
                    BOOST_LOG_SEV(log, trace) << "Done writing for: " << service_id;
                }
            };
            if (connection->is_tcp_socket_writing_)
            {
                BOOST_LOG_SEV(log, debug) << "TCP write buffer drain cannot be started while already writing";
            }
            else if (connection->tcp_write_buffer_.size() == 0)
            {
                invoke_and_clear_handler(connection->on_tcp_write_buffer_drain_complete);
            }
            else
            {
                connection->is_tcp_socket_writing_ = true;
                connection->socket_.async_write_some(connection->tcp_write_buffer_.data(), write_done);
            }
        }