libcurl 异步调用curl_multi_fdset max_fd总返回-1

春暖花开哟 发布于 2015/05/20 18:23
阅读 1K+
收藏 0
CURLM *multi_handle = NULL;
	multi_handle = curl_multi_init();
	// 添加到multi stack
	curl_multi_add_handle(multi_handle, curl);
	// 
	int running_handle_count;
	while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(multi_handle, &running_handle_count))
	{
		int cccc = 0;
	}
	//
	while (running_handle_count)
	{
		struct timeval timeout_tv;
		timeout_tv.tv_sec = 1;
		timeout_tv.tv_usec = 0;
		long curl_timeo = -1;
		curl_multi_timeout(multi_handle, &curl_timeo);
		if(curl_timeo >= 0) {
			timeout_tv.tv_sec = curl_timeo / 1000;
			if(timeout_tv.tv_sec > 1)
				timeout_tv.tv_sec = 1;
			else
				timeout_tv.tv_usec = (curl_timeo % 1000) * 1000;
		}

		int max_fd = 0;
		fd_set fd_read;
		fd_set fd_write;
		fd_set fd_except;

		FD_ZERO(&fd_read);
		FD_ZERO(&fd_write);
		FD_ZERO(&fd_except);

		CURLMcode abc = curl_multi_fdset(multi_handle, &fd_read, &fd_write, &fd_except, &max_fd);//max_fd总返回-1,curl版本号是7.39
int return_code = select(max_fd + 1, &fd_read, &fd_write, &fd_except, &timeout_tv);
		if (SOCKET_ERROR == return_code)
		{
			//cerr << "select error." << endl;
			break;
		}
		else
		{
			while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(multi_handle, &running_handle_count))
			{
				//cout << running_handle_count << endl;
			}
		}
	}


max_fd总返回-1,curl版本号是7.39

加载中
0
krole
krole

这是我在GitHub上查到的。https://github.com/curl/curl/blob/master/docs/examples/fopen.c  第173行。

    /* On success the value of maxfd is guaranteed to be >= -1. We call
       select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
       no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
       to sleep 100ms, which is the minimum suggested value in the
       curl_multi_fdset() doc. */

翻译:

成功时,maxfd的值保证>=-1。我们调用select(maxfd + 1,...);特别是在(maxfd == -1)的情况下,还没有fds就绪,所以我们在Windows上调用select(0,...)或Sleep()睡眠100ms,这是最小的建议值。 curl_multi_fdset()doc。

当调用curl_multi_fdset,尽量延迟时间,等待句柄就绪。

注意:第一个curl_multi_perform放在循环体内。

返回顶部
顶部