cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re:Re: in curl multi socket,in what condition the callback function of CURLMOPT_TIMERFUNCTION will be called?

From: À¼Ìì <lantian0811_at_163.com>
Date: Wed, 14 Jan 2015 13:16:16 +0800 (CST)

the code like this:

int main()
{
    .....
    g.evbase = event_base_new();
    g.timer_event = evtimer_new(g.evbase, timer_cb, &g);

    curl_multi_setopt(m_MultiHandle, CURLMOPT_SOCKETFUNCTION, sock_cb); <- sock_cb is callback function
    curl_multi_setopt(m_MultiHandle, CURLMOPT_SOCKETDATA, &g);

    curl_multi_setopt(m_MultiHandle, CURLMOPT_TIMERFUNCTION, multi_timer_cb); <- multi_timer_cb is another time callback function
    curl_multi_setopt(m_MultiHandle, CURLMOPT_TIMERDATA, &g);
}
In callback function multi_timer_cb,we just print the timeout value and add this timeout value to libevent ,and after this time ,the callback function of libevent called

static int multi_timer_cb(CURLM *multi, long timeout_ms, GlobalInfo *g)
{
  struct timeval timeout;
  (void)multi; /* unused */
  timeout.tv_sec = timeout_ms/1000;
  timeout.tv_usec = (timeout_ms%1000)*1000;
  fprintf(stdout, "multi_timer_cb: Setting timeout to %ld ms\n", timeout_ms);
  evtimer_add(g->timer_event, &timeout);
  return 0;
}

static void timer_cb(int fd, short kind, void *userp)
{
  GlobalInfo *g = (GlobalInfo *)userp;
  CURLMcode rc;
  (void)fd;
  (void)kind;
  rc = curl_multi_socket_action(g->multi,CURL_SOCKET_TIMEOUT, 0, &g->still_running);
  mcode_or_die("timer_cb: curl_multi_socket_action", rc);
  check_multi_info(g);
}
when I use one client to test this ,and the get the correct result,follow is the log:
[2015-01-14 11:59:21.148596] DBG|CImgMultiDownload.cpp|392|multi_timer_cb|multi_timer_cb: Setting timeout to 1 ms <-callback function called and timeout value is 1ms
[2015-01-14 11:59:21.148615] DBG|CImgMultiDownload.cpp|681|AddTask|Adding easy 0x15657a0 to multi 0x1531eb0 <- add easy handle to multi handle
[2015-01-14 11:59:21.152696] DBG|CImgMultiDownload.cpp|374|timer_cb|before curl_multi_socket_action <- after 1 ms ,we call libevent callback function timer_cb, and call curl_multi_socket_action to monitor socket fd status
[2015-01-14 11:59:21.152869] DBG|CImgMultiDownload.cpp|443|sock_cb|socket callback: s=113 e=0x15657a0 what=OUT -> now sent http request to server
[2015-01-14 11:59:21.152882] DBG|CImgMultiDownload.cpp|456|sock_cb|Adding data: OUT
[2015-01-14 11:59:21.152908] DBG|CImgMultiDownload.cpp|392|multi_timer_cb|multi_timer_cb: Setting timeout to 1000 ms -> set timeout again ,this value is choosed by curl kernel
[2015-01-14 11:59:21.152920] DBG|CImgMultiDownload.cpp|376|timer_cb|after curl_multi_socket_action
[2015-01-14 11:59:21.152931] DBG|CImgMultiDownload.cpp|226|check_multi_info|REMAINING: 1
[2015-01-14 11:59:21.156391] DBG|CImgMultiDownload.cpp|443|sock_cb|socket callback: s=113 e=0x15657a0 what=IN -> socket fd status changed ,and prepared to received data
[2015-01-14 11:59:21.156411] DBG|CImgMultiDownload.cpp|462|sock_cb|Changing action from OUT to IN
however,when I use a large amount connections in the client, the program is running incorrectly, and the log likes that:
[2015-01-13 23:39:55.199002] DBG|CImgMultiDownload.cpp|392|multi_timer_cb|multi_timer_cb: Setting timeout to 1 ms
[2015-01-13 23:39:55.199021] DBG|CImgMultiDownload.cpp|681|AddTask|Adding easy 0x1ae8520 to multi 0x1ab3eb0
[2015-01-13 23:39:55.203107] DBG|CImgMultiDownload.cpp|374|timer_cb|before curl_multi_socket_action
[2015-01-13 23:39:55.207158] DBG|CImgMultiDownload.cpp|443|sock_cb|socket callback: s=112 e=0x1ae8520 what=OUT
[2015-01-13 23:39:55.207187] DBG|CImgMultiDownload.cpp|456|sock_cb|Adding data: OUT
[2015-01-13 23:39:55.207214] DBG|CImgMultiDownload.cpp|392|multi_timer_cb|multi_timer_cb: Setting timeout to 996 ms <- the multi_timer_cb called first time
[2015-01-13 23:39:55.207231] DBG|CImgMultiDownload.cpp|376|timer_cb|after curl_multi_socket_action
[2015-01-13 23:39:55.207244] DBG|CImgMultiDownload.cpp|226|check_multi_info|REMAINING: 1
[2015-01-13 23:39:55.208146] DBG|CImgMultiDownload.cpp|392|multi_timer_cb|multi_timer_cb: Setting timeout to 996 ms <- the multi_timer_cb called second time ,confused!!!!!
[2015-01-13 23:39:55.208162] DBG|CImgMultiDownload.cpp|226|check_multi_info|REMAINING: 1
[2015-01-13 23:39:56.205182] DBG|CImgMultiDownload.cpp|374|timer_cb|before curl_multi_socket_action
[2015-01-13 23:39:56.205212] DBG|CImgMultiDownload.cpp|443|sock_cb|socket callback: s=112 e=0x1ae8520 what=REMOVE <- here libcurl remove the socket fd, I am so confused here
[2015-01-13 23:39:56.205224] DBG|CImgMultiDownload.cpp|447|sock_cb|remove socket fd!
[2015-01-13 23:39:56.205242] DBG|CImgMultiDownload.cpp|376|timer_cb|after curl_multi_socket_action
[2015-01-13 23:39:56.205252] DBG|CImgMultiDownload.cpp|226|check_multi_info|REMAINING: 0 <- because we remove the socket fd,so we can not receive the picture from server?

At 2015-01-14 06:18:24, "Daniel Stenberg" <daniel_at_haxx.se> wrote:
>On Wed, 14 Jan 2015, À¼Ìì wrote:
>
>> 50 [2015-01-13 23:39:57.211287]
>> DBG|CImgMultiDownload.cpp|443|sock_cb|socket callback: s=115 e=0x1af6c90
>> what=REMOVE <- here remove the socket,why? I thought here shou be sent http
>> header to server
>
>I know far too little about your code and libcurl setup to be able to tell
>why. Does the transfer fail in a strange way?
>
>> why the callback function of multi_timer_cb called twice??
>
>I can't tell based on this little info.
>
>> and my second question is :In what condition ,the callback function
>> multi_timer_cb will be called ?
>
>When libcurl has deemed that there's an updated timeout period to the nearest
>timeout at which time it wants to get called.
>
>--
>
> / daniel.haxx.se

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