void UdpReporter::ReConnect()

in nodemanager/core/UdpReporter.cpp [24:105]


void UdpReporter::ReConnect()
{
    if (this->s)
    {
        close(this->s);
    }

    std::string uri;

    try
    {
        uri = this->getReportUri(this->cts.get_token());
    }
    catch (const http::http_exception& httpEx)
    {
        Logger::Warn("UdpReporter, HttpException occurred when {2} report to {0}, ex {1}", uri, httpEx.what(), this->name);
    }
    catch (const std::exception& ex)
    {
        Logger::Error("UdpReporter, Exception occurred when {2} report to {0}, ex {1}", uri, ex.what(), this->name);
    }
    catch (...)
    {
        Logger::Error("UdpReporter, Unknown error occurred when {1} report to {0}", uri, this->name);
    }

    if (!uri.empty())
    {
        auto tokens = String::Split(uri, '/');
        auto endpoint = String::Split(tokens[2], ':');
        auto server = endpoint[0];
        auto port = endpoint[1];

        addrinfo hints;
        memset(&hints, 0, sizeof(hints));
        hints.ai_family = AF_INET;
        hints.ai_socktype = SOCK_DGRAM;
        hints.ai_flags = 0;
        hints.ai_protocol = 0;

        addrinfo* siRemote, *current;
        Logger::Info("UdpReporter, getaddrinfo server {0}, port {1}", server, port);

        int ret = getaddrinfo(server.c_str(), port.c_str(), &hints, &siRemote);
        if (ret != 0)
        {
            Logger::Error("UdpReporter, getaddrinfo failed {0}", gai_strerror(ret));
            return;
        }

        bool success = false;
        for (current = siRemote;
            current != nullptr;
            current = current->ai_next)
        {
            if ((this->s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP)) == -1)
            {
                Logger::Warn("UdpReporter, create socket failed with errno {0}", errno);
                continue;
            }

            if (connect(this->s, current->ai_addr, current->ai_addrlen) != -1)
            {
                Logger::Info("UdpReporter, connect succeeds.");
                success = true;
                break;
            }

            Logger::Warn("UdpReporter, connect failed with errno {0}.", errno);
            close(this->s);
        }

        freeaddrinfo(siRemote);

        this->uri = uri;
        this->initialized = success;
    }
    else
    {
        this->initialized = false;
    }
}