void tcp_adapter_proxy::async_setup_dest_tcp_socket_retry()

in src/TcpAdapterProxy.cpp [1720:1788]


    void tcp_adapter_proxy::async_setup_dest_tcp_socket_retry(tcp_adapter_context &tac, std::shared_ptr<basic_retry_config> retry_config, string const & service_id)
    {
        tcp_client::pointer client = tac.serviceId_to_tcp_client_map[service_id];
        tcp_socket_ensure_closed(client->connection_->socket());
        if (tac.adapter_config.serviceId_to_endpoint_map.find((service_id)) == tac.adapter_config.serviceId_to_endpoint_map.end())
        {
            throw std::runtime_error((boost::format("Receive invalid service id %1%") % service_id).str());
        }
        std::string endpoint = tac.adapter_config.serviceId_to_endpoint_map[service_id];

        BOOST_LOG_SEV(log, info) << "Attempting to establish tcp socket connection to: " << endpoint;

        if (tac.adapter_config.bind_address.has_value())
        {
            BOOST_LOG_SEV(log, debug) << "Resolving local address host: " << tac.adapter_config.bind_address.get();
            client->resolver_.async_resolve(tac.adapter_config.bind_address.get(), boost::lexical_cast<std::string>("0"),
                boost::asio::ip::resolver_base::passive,
                [=, &tac](boost::system::error_code const &ec, tcp::resolver::results_type results)
                {
                    if (ec)
                    {
                        BOOST_LOG_SEV(log, error) << (boost::format("Could not resolve bind address: %1% -- %2%") % tac.adapter_config.bind_address.get() % ec.message()).str();
                        basic_retry_execute(log, retry_config,
                            [this, &tac, service_id]()
                            {
                                tcp_connection::pointer socket_connection = get_tcp_connection(tac, service_id);
                                socket_connection->after_send_message = std::bind(&tcp_adapter_proxy::setup_tcp_socket, this, std::ref(tac), service_id);
                                async_send_stream_reset(tac, service_id);
                            });
                    }
                    else
                    {
                        BOOST_LOG_SEV(log, debug) << "Resolved bind IP: " << results->endpoint().address().to_string();
                        boost::system::error_code bind_ec;

                        client->connection_->socket().open(results->endpoint().protocol());
                        client->connection_->socket().bind({results->endpoint().address(), 0}, bind_ec);
                        if (bind_ec)
                        {
                            BOOST_LOG_SEV(log, error) << (boost::format("Could not bind to address: %1% -- %2%") % results->endpoint().address().to_string() % bind_ec.message()).str();
                            basic_retry_execute(log, retry_config,
                                [this, &tac, service_id]()
                                {
                                    tcp_connection::pointer socket_connection = get_tcp_connection(tac, service_id);
                                    socket_connection->after_send_message = std::bind(&tcp_adapter_proxy::setup_tcp_socket, this, std::ref(tac), service_id);
                                    async_send_stream_reset(tac, service_id);
                                });
                        }
                        else
                        {
                            tuple<string, string> endpoint_to_connect = tcp_adapter_proxy::get_host_and_port(endpoint, tac.adapter_config.bind_address.get());
                            std::string dst_host = std::get<0>(endpoint_to_connect);
                            std::string dst_port = std::get<1>(endpoint_to_connect);
                            client->resolver_.async_resolve(dst_host, dst_port,
                                std::bind(&tcp_adapter_proxy::async_resolve_destination_for_connect, this, std::ref(tac), retry_config, service_id, std::placeholders::_1, std::placeholders::_2));
                        }
                    }
                });
        }
        else
        {
            tuple<string, string> endpoint_to_connect = tcp_adapter_proxy::get_host_and_port(endpoint, LOCALHOST_IP);
            std::string dst_host = std::get<0>(endpoint_to_connect);
            std::string dst_port = std::get<1>(endpoint_to_connect);
            BOOST_LOG_SEV(log, trace) << "Resolving destination host: " << dst_host << " port: " << dst_port;
            client->resolver_.async_resolve(dst_host, dst_port,
                std::bind(&tcp_adapter_proxy::async_resolve_destination_for_connect, this, std::ref(tac), retry_config, service_id, std::placeholders::_1, std::placeholders::_2));
        }
    }