Example #1
0
void CurlConnect::WateForMulitCurl(CURLM* curl_m)
{
	//等待数据的填充
	/*
	* 调用curl_multi_perform函数执行curl请求
	* url_multi_perform返回CURLM_CALL_MULTI_PERFORM时,表示需要继续调用该函数直到返回值不是CURLM_CALL_MULTI_PERFORM为止
	* running_handles变量返回正在处理的easy curl数量,running_handles为0表示当前没有正在执行的curl请求
	*/
	int running_handles;
	while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(curl_m, &running_handles))
	{
		//cout << running_handles << endl;
	}

	/**
	* 为了避免循环调用curl_multi_perform产生的cpu持续占用的问题,采用select来监听文件描述符
	*/
	while (running_handles)
	{
		if (-1 == curl_multi_select(curl_m))
		{
			//cerr << "select error" << endl;
			break;
		} 
		else 
		{
			// select监听到事件,调用curl_multi_perform通知curl执行相应的操作 //
			while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(curl_m, &running_handles))
			{
				//cout << "select: " << running_handles << endl;
			}
		}
		//cout << "select: " << running_handles << endl;
	}
}
Example #2
0
int curl_multi_done(CURL* curl_e, int& reDirectCount)
{
	int iRetcode = -1;
	CURLM * curl_m = curl_multi_init();
	curl_multi_add_handle(curl_m, curl_e);

	/*
	* 调用curl_multi_perform函数执行curl请求
	* url_multi_perform返回CURLM_CALL_MULTI_PERFORM时,表示需要继续调用该函数直到返回值不是CURLM_CALL_MULTI_PERFORM为止
	* running_handles变量返回正在处理的easy curl数量,running_handles为0表示当前没有正在执行的curl请求
	*/
	int running_handles;
	while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(curl_m, &running_handles));

	/**
	* 为了避免循环调用curl_multi_perform产生的cpu持续占用的问题,采用select来监听文件描述符
	*/
	while (running_handles)
	{
		if (-1 == curl_multi_select(curl_m, &running_handles))
		{
			break;
		}
		else {
			// select监听到事件,调用curl_multi_perform通知curl执行相应的操作 //  
			while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(curl_m, &running_handles));
		}
	}

	// 输出执行结果 //  
	int         msgs_left;
	CURLMsg *   msg;
	CURLcode code = CURL_LAST;
	while ((msg = curl_multi_info_read(curl_m, &msgs_left)))
	{
		if ( CURLMSG_DONE == msg->msg )
		{
			code = msg->data.result;
			if ( code == CURLE_OK )
			{
				curl_easy_getinfo(curl_e, CURLINFO_HTTP_CODE, &iRetcode);
				curl_easy_getinfo(curl_e, CURLINFO_REDIRECT_COUNT, &reDirectCount);
			}
		}
	}
	curl_multi_remove_handle(curl_m, curl_e);

	curl_easy_cleanup(curl_e);

	curl_multi_cleanup(curl_m);

	return iRetcode;
}