cURL / Mailing Lists / curl-library / Single Mail

curl-library

Problem when I use libcurl to write and libevent to listen on a socket

From: vmon <vmonmoonshine_at_gmail.com>
Date: Mon, 27 Aug 2012 01:18:36 -0600

Hello There,

I have a client program that manages the socket connection on its own
using libevent's bufferevent. Now, I'd like to use libcurl to produce
and send HTML GET requests on that socket, but let libevent read
the reply.

I can't let the bufferevent listener go and stay with libcurl,
because it's a multi-protocol program, and I'm implementing the http
part of it. Other protocols don't have the luxury of using libcurl, so I
need to keep the bufferevent structure.

So here what I did:

  //now we are using curl to send the request
  curl_easy_setopt(_curl_easy_handle, CURLOPT_URL, uri_to_send.c_str());

  CURLMcode res = curl_multi_add_handle(_apache_config->_curl_multi_handle, _curl_easy_handle);

  if (res != CURLM_OK) {
    log_debug(conn,"error in adding curl handle. CURL Error %s", curl_multi_strerror(res));
  }

  bufferevent_disable(conn->buffer, EV_WRITE);
  event* write_url_event = event_new(bufferevent_get_base(conn->buffer), conn->socket(), EV_WRITE, curl_socket_event_cb, this);
  event_add(write_url_event, NULL);

Then I have the callback as follow:

/* Called by libevent when we get action on a multi socket */
void
http_apache_steg_t::curl_socket_event_cb(int fd, short kind, void *userp)
{
  http_apache_steg_t* steg_mod = (http_apache_steg_t*) userp;
  CURLMcode rc;
  
  if (kind & EV_WRITE)
  {
     rc = curl_multi_socket_action(steg_mod->_apache_config->_curl_multi_handle, fd, CURL_CSELECT_OUT, &steg_mod->_apache_config->_curl_running_handle);

     if (rc == CURLM_OK) {
      curl_multi_remove_handle(steg_mod->_apache_config->_curl_multi_handle, steg_mod->_curl_easy_handle);

      bufferevent_enable(steg_mod->conn->buffer, EV_READ|EV_WRITE);

      steg_mod->conn->cease_transmission();
    }
  else
    {
      log_abort(steg_mod->conn, "error in requesting the uri. CURL Error %s", curl_multi_strerror(rc));
    }

  }

}

However, unfortunately I lose lots of packets and they never reach my
libevent socket_read_cb. I suspect that curl is receiving them instead
(I have no problem when curl is not listening on the socket, i.e. I'm
generating the GET requests from scratch manually).

Does libcurl still listen to the socket for read events, even though I
never ask it to do so? If yes, is there a way to tell libcurl to only
write into the socket and then let it go?

Thank you for your help.

Cheers,
vmon
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2012-08-27