cURL / Mailing Lists / curl-library / Single Mail

curl-library

how to use poll with libcurl

From: jianqiu lv <realeasy82_at_gmail.com>
Date: Thu, 14 Mar 2013 23:24:39 +0800

  Recently I meet a problem the when using select after muti_curl_fdset,
the maxfd is over 1024 in my process, and it is necessary to have such a
value. I search in libcurl's example(
http://curl.haxx.se/libcurl/c/hiperfifo.html) , they have an example for
how to use libevent with curl_multi_socket_action. I am not familiar with
libevent nor have the plan to use libevent, since the socket fd in my
process is not too much (less than 50,using poll which is O(n) is almost
the same with using epoll O(1)), most all the fds are local files.
  This project is a maintenance project ,so I don't want to change a lot of
code to cause any side effect. So I think can I just use curl_multi_setopt
set the callback function before curl_multi_add_handle and when the call
back happen , I add the fd into pullfd array. Then I poll on the pullfd
array to replace the select I used to call.But when my code execute it will
stuck when multi curl happen. Does anyone have any idea why my code not
working?
  Here is part of my code.(the code look almost the same as
http://curl.haxx.se/libcurl/c/multi-post.html)

    static void setsock(curl_socket_t s, CURL*e, int act, CurlMultiEngine*g)
    {
      int kind =

 (act&CURL_POLL_IN?POLLIN:0)|(act&CURL_POLL_OUT?POLLOUT:0)|(act&CURL_POLL_INOUT?POLLIN|POLLOUT:0);

      struct pollfd ufd;
      ufd.fd = s;
      ufd.events = kind;
      g->setPollFd(ufd);
    }

    /*call back for curl_multi_setopt CURLMOPT_SOCKETFUNCTION */
    static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void
*nouse)
    {
    CurlMultiEngine *mapleCMERI = (CurlMultiEngine*) cbp;

      if (what != CURL_POLL_REMOVE) {
        setsock(s, e, what, mapleCMERI);
      }
      return 0;
    }

    CurlMultiEngine::setPollFd(pollfd ufd)
    {
    int i = 0;
    for(; i<m_ufdsPosition;i++)
    {
    if(ufds[i].fd == ufd.fd)
    {
     ufds[i] = ufd;
    }
    }
    if(i == m_ufdsPosition)
    {
     ufds[m_ufdsPosition++] = ufd;
    }
    }

    int
    CurlMultiEngine::multiSelect(int a_waitTime)
     {
//replace select with poll to avoid fd 1024 limit
        int result = poll(ufds,m_ufdsPosition,a_waitTime);
        return result;
    }

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2013-03-14