In libcurl, The curlmulti_perform () function is used to execute the subscription type url.

Source: Internet
Author: User

In libcurl, The curlmulti_perform () function is used to execute the subscription type url.
Prerequisites

When you need to process multiple URLs at the same time, you can use curl_multi_perform to execute the following code 1:

1 // initialize a multi curl object 2 CURLM * curl_m = curl_multi_init (); 3 CURL * my_curl [CURL_NUM]; 4 char rcvbuf [CURL_NUM] [MAXHEADLEN] = {0 }; 5 // other initialization code skipped... 6 7 // execute multiple URLs 8 while (running_handles) 9 {10 if (-1 = curl_multi_select (curl_m) 11 {12 printf ("curl_multi_select error! \ N "); 13 break; 14} 15 else {16 // select listens to the event, call curl_multi_perform to notify curl to perform the corresponding operation // 17 while (CURLM_CALL_MULTI_PERFORM = curl_multi_perform (curl_m, & running_handles); 18} 19} 20 21 // parse data 22 int msgs_left; 23 CURLMsg * curl_msg; 24 while (curl_msg = curl_multi_info_read (curl_m, & msgs_left) 25 {26 if (CURLMSG_DONE = curl_msg-> msg) 27 {28 int idx; 29 for (idx = 0; idx <CURL_NUM; ++ idx) 30 {31 if (curl_ms G-> easy_handle = my_curl [idx]) break; 32} 33 34 if (idx = CURL_NUM) 35 {36 printf ("curl not found! \ N "); 37} 38 else39 {40 printf (" \ ncurl [% d] rcvbuf: \ n % s \ n ", idx, rcvbuf [idx]); 41 // data processing... 42} 43} 44}
Problem Summary

Now my url is the subscription method, and each curl will keep receiving data (even if there is no data, it will receive a heartbeat message every 10 s), and will never exit, that is, the above loop is always executed, So that I cannot run to the "parse data" step. So I need to judge whether a curl has new data in a loop.

Method 1 (NO)

The first method is to directly move the curl_multi_info_read () function to the loop to see if it can receive data, as shown in Code 2 below:

1 // execute multiple URLs and parse the data 2 while (running_handles) 3 {4 if (-1 = curl_multi_select (curl_m) 5 {6 printf ("curl_multi_select error! \ N "); 7 break; 8} 9 else {10 // select listens to the event, call curl_multi_perform to notify curl to perform the corresponding operation // 11 while (CURLM_CALL_MULTI_PERFORM = curl_multi_perform (curl_m, & running_handles )); 12 // check which curl [idx] comes with data 13 while (curl_msg = curl_multi_info_read (curl_m, & msgs_left ))) 14 {15 if (CURLMSG_DONE = curl_msg-> msg) 16 {17 int idx; 18 for (idx = 0; idx <CURL_NUM; ++ idx) 19 {20 if (curl_msg-> easy_handle = my_curl [idx]) bre Ak; 21} 22 if (idx = CURL_NUM) 23 {24 printf ("curl not found! \ N "); 25} 26 else27 {28 printf (" \ ncurl [% d] rcvbuf: \ n % s \ n ", idx, rcvbuf [idx]); 29 // data processing... 30 memset (rcvbuf [idx], 0, sizeof (rcvbuf [idx]); // clear the next round of the buf loop and use 31} 32} 33} 34} 35}

Obviously, I think too much. The only difference after this process is that, which curl prints the data after it is executed (Code 1 can only print rcvbuf [idx] After all curls are executed and exited). however, my curl is subscribed and cannot be printed unless rcvbuf [idx] overflows...

 

Method 2 (temporarily unavailable)

Rcvbuf [idx] overflow? This is definitely not possible, but it reminds me of the curl_easy_setopt () function, which can be used to configure curl functions. Maybe one of them can satisfy me:

CURLOPT_TIMEOUT_MS configure the timeout time?
No. This is because the curl has timed out;
CURLOPT_RANGE configure resumable upload?
It seems we can; through the test, we found that the curl will exit after the specified XX bytes are full, even if there is still data in the back, this is not what we want to see.
Is there a configuration for receiving timeout?
Same as above, even if there is still data after the number of s, it will not work.
...

I believe that configuring the curl_easy_setopt () function should be the most official practice. However, the younger brother did not find any relevant articles and did not make any research on his own.

 

Method 3 (feasible and defective)

In desperation, I suddenly thought that since the data received by curl [idx] is in rcvbuf [idx], why not directly check whether there is data in rcvbuf [idx]? Code 3:

1 // execute multiple URLs and parse the data 2 while (running_handles) 3 {4 if (-1 = curl_multi_select (curl_m) 5 {6 printf ("curl_multi_select error! \ N "); 7 break; 8} 9 else {10 // select listens to the event, call curl_multi_perform to notify curl to perform the corresponding operation // 11 while (CURLM_CALL_MULTI_PERFORM = curl_multi_perform (curl_m, & running_handles )); 12 // check which curl [idx] has 13 int idx = 0; 14 for (idx = 0; idx <CURL_NUM; ++ idx) 15 {16 if (rcvbuf [idx] [0] = NULL) // curl [idx] does not receive data. 17 continue; 18 printf ("curl [% d] rcvbuf: \ n % s \ n", idx, rcvbuf [idx]); 19 // data processing... 20 memset (rcvbuf [idx], 0, sizeof (rcvbuf [idx]); 21} 22} 23}

The above processing can indeed meet the requirements, but the method is a bit stupid, there are two obvious defects:
1. Check all curls at a time, with low efficiency;
2. Once a curl dies for some reason, how can I determine which curl is down?


So is there an official solution to this problem? It remains to be answered by senior personnel and remains updated.

 




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.