quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

multi-app.c (3528B)


      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
      9  *
     10  * This software is licensed as described in the file COPYING, which
     11  * you should have received as part of this distribution. The terms
     12  * are also available at https://curl.se/docs/copyright.html.
     13  *
     14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
     15  * copies of the Software, and permit persons to whom the Software is
     16  * furnished to do so, under the terms of the COPYING file.
     17  *
     18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
     19  * KIND, either express or implied.
     20  *
     21  * SPDX-License-Identifier: curl
     22  *
     23  ***************************************************************************/
     24 /* <DESC>
     25  * A basic application source code using the multi interface doing two
     26  * transfers in parallel.
     27  * </DESC>
     28  */
     29 
     30 #include <stdio.h>
     31 #include <string.h>
     32 
     33 /* curl stuff */
     34 #include <curl/curl.h>
     35 
     36 /*
     37  * Download an HTTP file and upload an FTP file simultaneously.
     38  */
     39 
     40 #define HANDLECOUNT 2   /* Number of simultaneous transfers */
     41 #define HTTP_HANDLE 0   /* Index for the HTTP transfer */
     42 #define FTP_HANDLE 1    /* Index for the FTP transfer */
     43 
     44 int main(void)
     45 {
     46   CURL *handles[HANDLECOUNT];
     47   CURLM *multi_handle;
     48 
     49   int still_running = 1; /* keep number of running handles */
     50   int i;
     51 
     52   CURLMsg *msg; /* for picking up messages with the transfer status */
     53   int msgs_left; /* how many messages are left */
     54 
     55   /* Allocate one curl handle per transfer */
     56   for(i = 0; i < HANDLECOUNT; i++)
     57     handles[i] = curl_easy_init();
     58 
     59   /* set the options (I left out a few, you get the point anyway) */
     60   curl_easy_setopt(handles[HTTP_HANDLE], CURLOPT_URL, "https://example.com");
     61 
     62   curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_URL, "ftp://example.com");
     63   curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_UPLOAD, 1L);
     64 
     65   /* init a multi stack */
     66   multi_handle = curl_multi_init();
     67 
     68   /* add the individual transfers */
     69   for(i = 0; i < HANDLECOUNT; i++)
     70     curl_multi_add_handle(multi_handle, handles[i]);
     71 
     72   while(still_running) {
     73     CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
     74 
     75     if(still_running)
     76       /* wait for activity, timeout or "nothing" */
     77       mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
     78 
     79     if(mc)
     80       break;
     81   }
     82   /* See how the transfers went */
     83   /* !checksrc! disable EQUALSNULL 1 */
     84   while((msg = curl_multi_info_read(multi_handle, &msgs_left)) != NULL) {
     85     if(msg->msg == CURLMSG_DONE) {
     86       int idx;
     87 
     88       /* Find out which handle this message is about */
     89       for(idx = 0; idx < HANDLECOUNT; idx++) {
     90         int found = (msg->easy_handle == handles[idx]);
     91         if(found)
     92           break;
     93       }
     94 
     95       switch(idx) {
     96       case HTTP_HANDLE:
     97         printf("HTTP transfer completed with status %d\n", msg->data.result);
     98         break;
     99       case FTP_HANDLE:
    100         printf("FTP transfer completed with status %d\n", msg->data.result);
    101         break;
    102       }
    103     }
    104   }
    105 
    106   /* remove the transfers and cleanup the handles */
    107   for(i = 0; i < HANDLECOUNT; i++) {
    108     curl_multi_remove_handle(multi_handle, handles[i]);
    109     curl_easy_cleanup(handles[i]);
    110   }
    111 
    112   curl_multi_cleanup(multi_handle);
    113 
    114   return 0;
    115 }