[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[no subject]



Hi,

I have tried to embed libevent as a static library with my C++ application and would like to get http requests in my c++ application. Seems it works fine normally.

I have use the following code..

______________________________________________

void WebServer::ProcessHandler(struct evhttp_request *req, void *arg)
{
   
    struct evbuffer *buf;
    buf = evbuffer_new();

    if (buf == NULL)
        cout << "failed to create response buffer";

    char* uriInput = NULL;
    if (req->type == EVHTTP_REQ_GET)
    {
        uriInput = (char*)evhttp_request_uri(req);
    }
    else if (req->type == EVHTTP_REQ_POST)
    {
        struct evbuffer *input_headers = evbuffer_new();
        input_headers = req->input_buffer;
        uriInput = (char*)input_headers->buffer;
    }
   
    evbuffer_add_printf(buf, "Requested: %s\n", evhttp_request_uri(req));
    evhttp_send_reply(req, HTTP_OK, "OK", buf);

    evhttp_connection_free(req->evcon);
    free(uriInput);
    evbuffer_free(buf);

    return;
};

void webserver::ThreadProcess()
{
    struct evhttp *httpd;

    event_init();
    httpd = evhttp_start("0.0.0.0", 8080);   

    evhttp_set_cb(httpd, "/", &WebServer::ProcessHandler, NULL);

    event_dispatch();

    evhttp_free(httpd);
}

__________________________________________________

But seems, the above code does not work as a threaded function. ie, call the same functions from different thread , when a first call is in progress I want to try a second call synchronously.

Is there a way to enable the function as a multithread callback function..

Thanks in Advance for your reply.