Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <curl/curl.h>
00031
00032
00033
00034
00035
00036
00037
00038
00039 #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
00040
00041 static struct timeval tvnow(void)
00042 {
00043 struct timeval now;
00044
00045
00046 now.tv_sec = (long)time(NULL);
00047 now.tv_usec = 0;
00048
00049 return now;
00050 }
00051
00052 static long tvdiff(struct timeval newer, struct timeval older)
00053 {
00054 return (newer.tv_sec - older.tv_sec) * 1000 +
00055 (newer.tv_usec - older.tv_usec) / 1000;
00056 }
00057
00058 int main(void)
00059 {
00060 CURL *curl;
00061 CURLM *mcurl;
00062 int still_running = 1;
00063 struct timeval mp_start;
00064
00065 curl_global_init(CURL_GLOBAL_DEFAULT);
00066
00067 curl = curl_easy_init();
00068 if(!curl)
00069 return 1;
00070
00071 mcurl = curl_multi_init();
00072 if(!mcurl)
00073 return 2;
00074
00075
00076 curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
00077 curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
00078
00079
00080 curl_easy_setopt(curl, CURLOPT_URL, "pop3://pop.example.com/1");
00081
00082
00083 curl_multi_add_handle(mcurl, curl);
00084
00085
00086 mp_start = tvnow();
00087
00088
00089 curl_multi_perform(mcurl, &still_running);
00090
00091 while(still_running) {
00092 struct timeval timeout;
00093 fd_set fdread;
00094 fd_set fdwrite;
00095 fd_set fdexcep;
00096 int maxfd = -1;
00097 int rc;
00098 CURLMcode mc;
00099
00100 long curl_timeo = -1;
00101
00102
00103 FD_ZERO(&fdread);
00104 FD_ZERO(&fdwrite);
00105 FD_ZERO(&fdexcep);
00106
00107
00108 timeout.tv_sec = 1;
00109 timeout.tv_usec = 0;
00110
00111 curl_multi_timeout(mcurl, &curl_timeo);
00112 if(curl_timeo >= 0) {
00113 timeout.tv_sec = curl_timeo / 1000;
00114 if(timeout.tv_sec > 1)
00115 timeout.tv_sec = 1;
00116 else
00117 timeout.tv_usec = (curl_timeo % 1000) * 1000;
00118 }
00119
00120
00121 mc = curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
00122
00123 if(mc != CURLM_OK) {
00124 fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
00125 break;
00126 }
00127
00128
00129
00130
00131
00132
00133
00134 if(maxfd == -1) {
00135 #ifdef _WIN32
00136 Sleep(100);
00137 rc = 0;
00138 #else
00139
00140 struct timeval wait = { 0, 100 * 1000 };
00141 rc = select(0, NULL, NULL, NULL, &wait);
00142 #endif
00143 }
00144 else {
00145
00146
00147 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
00148 }
00149
00150 if(tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
00151 fprintf(stderr,
00152 "ABORTING: Since it seems that we would have run forever.\n");
00153 break;
00154 }
00155
00156 switch(rc) {
00157 case -1:
00158 break;
00159 case 0:
00160 default:
00161 curl_multi_perform(mcurl, &still_running);
00162 break;
00163 }
00164 }
00165
00166
00167 curl_multi_remove_handle(mcurl, curl);
00168 curl_multi_cleanup(mcurl);
00169 curl_easy_cleanup(curl);
00170 curl_global_cleanup();
00171
00172 return 0;
00173 }