会mongoose的帮下忙


#include <string>
#include "ui_rotate_management.hpp"
#include "r2_http_api.hpp"

#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */
#include "mongoose.h"

#define LOG_TAG "r2_http_api"
#include <log/log.h>
#ifdef __cplusplus
}
#endif /* __cplusplus */

static int req_type = 0;
static r2_http_api *http_api = nullptr;



r2_http_api::r2_http_api()
{
    http_api = this;
    reqThread = new QThread;
    reqThread->start();
    this->moveToThread(reqThread);
    ALOGD("[%s]", __func__);
}

r2_http_api::~r2_http_api()
{
    http_api = nullptr;
    req_type = 0;
    reqThread->quit();
    reqThread->wait();
    ALOGD("[%s]", __func__);
}

void r2_http_api::sendHeartbeat()
{
    if (req_type != 0)
        return;
    req_type = 1;

    ALOGD("[%s]: req_start", __func__);
    struct mg_mgr mgr;
    mg_mgr_init(&mgr, NULL);
 
    if (mg_connect_http(&mgr, ev_handler, url.constData(), header.constData(), NULL)
    {
        while (req_type != 0)
        {
            mg_mgr_poll(&mgr, 1000);
        }
    }
    req_type = 0;
    mg_mgr_free(&mgr);
    ALOGD("[%s]: req_end", __func__);
}

static void ev_handler(struct mg_connection *nc, int ev, void *ev_data)
{
    struct http_message *hm = (struct http_message *)ev_data;
    switch (ev)
    {
    case MG_EV_CONNECT:
        if (*(int *)ev_data != 0)
        {
            ALOGD("[%s]: MG_EV_CONNECT ERROR: *s", __func__, (int *)ev_data);
            return;
        }
        ALOGD("[%s]: MG_EV_CONNECT", __func__);
        break;
    case MG_EV_HTTP_REPLY:
        nc->flags |= MG_F_CLOSE_IMMEDIATELY;
        ALOGD("[%s]: MG_EV_HTTP_REPLY", __func__);
        ALOGD("[%s]: Response: %s", __func__, hm->body.p);
        break;
    case MG_EV_CLOSE:
        ALOGD("[%s]: MG_EV_CLOSE", __func__);
        req_type = 0;
        break;
    }
}

用mongoose发起向192.168.1.122:8080/device/heartbeat
一次请求