pop3-multi.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 
00023 /* <DESC>
00024  * POP3 example using the multi interface
00025  * </DESC>
00026  */
00027 
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <curl/curl.h>
00031 
00032 /* This is a simple example showing how to retrieve mail using libcurl's POP3
00033  * capabilities. It builds on the pop3-retr.c example to demonstrate how to use
00034  * libcurl's multi interface.
00035  *
00036  * Note that this example requires libcurl 7.20.0 or above.
00037  */
00038 
00039 #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
00040 
00041 static struct timeval tvnow(void)
00042 {
00043   struct timeval now;
00044 
00045   /* time() returns the value of time in seconds since the epoch */
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   /* Set username and password */
00076   curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
00077   curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
00078 
00079   /* This will retrieve message 1 from the user's mailbox */
00080   curl_easy_setopt(curl, CURLOPT_URL, "pop3://pop.example.com/1");
00081 
00082   /* Tell the multi stack about our easy handle */
00083   curl_multi_add_handle(mcurl, curl);
00084 
00085   /* Record the start time which we can use later */
00086   mp_start = tvnow();
00087 
00088   /* We start some action by calling perform right away */
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; /* curl_multi_fdset() return code */
00099 
00100     long curl_timeo = -1;
00101 
00102     /* Initialise the file descriptors */
00103     FD_ZERO(&fdread);
00104     FD_ZERO(&fdwrite);
00105     FD_ZERO(&fdexcep);
00106 
00107     /* Set a suitable timeout to play around with */
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     /* get file descriptors from the transfers */
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     /* On success the value of maxfd is guaranteed to be >= -1. We call
00129        select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
00130        no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
00131        to sleep 100ms, which is the minimum suggested value in the
00132        curl_multi_fdset() doc. */
00133 
00134     if(maxfd == -1) {
00135 #ifdef _WIN32
00136       Sleep(100);
00137       rc = 0;
00138 #else
00139       /* Portable sleep for platforms other than Windows. */
00140       struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
00141       rc = select(0, NULL, NULL, NULL, &wait);
00142 #endif
00143     }
00144     else {
00145       /* Note that on some platforms 'timeout' may be modified by select().
00146          If you need access to the original value save a copy beforehand. */
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:  /* select error */
00158       break;
00159     case 0:   /* timeout */
00160     default:  /* action */
00161       curl_multi_perform(mcurl, &still_running);
00162       break;
00163     }
00164   }
00165 
00166   /* Always cleanup */
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 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06