doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
multi-app.c
Go to the documentation of this file.
1 /*****************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * $Id: multi-app.c,v 1.4 2003/08/28 11:21:14 bagder Exp $
9  *
10  * This is an example application source code using the multi interface.
11  */
12 
13 #include <stdio.h>
14 #include <string.h>
15 
16 /* somewhat unix-specific */
17 #include <sys/time.h>
18 #include <unistd.h>
19 
20 /* curl stuff */
21 #include <curl/curl.h>
22 
23 /*
24  * Download a HTTP file and upload an FTP file simultaneously.
25  */
26 
27 #define HANDLECOUNT 2 /* Number of simultaneous transfers */
28 #define HTTP_HANDLE 0 /* Index for the HTTP transfer */
29 #define FTP_HANDLE 1 /* Index for the FTP transfer */
30 
31 int main(int argc, char **argv)
32 {
33  CURL *handles[HANDLECOUNT];
35 
36  int still_running; /* keep number of running handles */
37  int i;
38 
39  CURLMsg *msg; /* for picking up messages with the transfer status */
40  int msgs_left; /* how many messages are left */
41 
42  /* Allocate one CURL handle per transfer */
43  for (i=0; i<HANDLECOUNT; i++)
44  handles[i] = curl_easy_init();
45 
46  /* set the options (I left out a few, you'll get the point anyway) */
47  curl_easy_setopt(handles[HTTP_HANDLE], CURLOPT_URL, "http://website.com");
48 
49  curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_URL, "ftp://ftpsite.com");
50  curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_UPLOAD, TRUE);
51 
52  /* init a multi stack */
53  multi_handle = curl_multi_init();
54 
55  /* add the individual transfers */
56  for (i=0; i<HANDLECOUNT; i++)
57  curl_multi_add_handle(multi_handle, handles[i]);
58 
59  /* we start some action by calling perform right away */
61  curl_multi_perform(multi_handle, &still_running));
62 
63  while(still_running) {
64  struct timeval timeout;
65  int rc; /* select() return code */
66 
67  fd_set fdread;
68  fd_set fdwrite;
69  fd_set fdexcep;
70  int maxfd;
71 
72  FD_ZERO(&fdread);
73  FD_ZERO(&fdwrite);
74  FD_ZERO(&fdexcep);
75 
76  /* set a suitable timeout to play around with */
77  timeout.tv_sec = 1;
78  timeout.tv_usec = 0;
79 
80  /* get file descriptors from the transfers */
81  curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
82 
83  rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
84 
85  switch(rc) {
86  case -1:
87  /* select error */
88  break;
89  case 0:
90  /* timeout, do something else */
91  break;
92  default:
93  /* one or more of curl's file descriptors say there's data to read
94  or write */
96  curl_multi_perform(multi_handle, &still_running));
97  break;
98  }
99  }
100 
101  curl_multi_cleanup(multi_handle);
102 
103  /* See how the transfers went */
104  while ((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
105  if (msg->msg == CURLMSG_DONE) {
106 
107  int idx, found = 0;
108 
109  /* Find out which handle this message is about */
110  for (idx=0; (!found && (idx<HANDLECOUNT)); idx++) found = (msg->easy_handle == handles[idx]);
111 
112  switch (idx) {
113  case HTTP_HANDLE:
114  printf("HTTP transfer completed with status %d\n", msg->data.result);
115  break;
116  case FTP_HANDLE:
117  printf("FTP transfer completed with status %d\n", msg->data.result);
118  break;
119  }
120  }
121  }
122 
123  /* Free the CURL handles */
124  for (i=0; i<HANDLECOUNT; i++)
125  curl_easy_cleanup(handles[i]);
126 
127  return 0;
128 }
CURLMcode curl_multi_fdset(CURLM *multi_handle, fd_set *read_fd_set, fd_set *write_fd_set, fd_set *exc_fd_set, int *max_fd)
Definition: multi.c:231
int main(int argc, char **argv)
Definition: multi-app.c:31
union CURLMsg::@3 data
long tv_sec
Definition: timeval.h:37
CURLcode result
Definition: multi.h:100
CURLcode curl_easy_setopt(CURL *curl, CURLoption option,...)
Definition: easy.c:217
int i
Definition: process.py:33
long tv_usec
Definition: timeval.h:38
CURLM * multi_handle
Definition: fopen.c:81
#define select(args...)
Definition: amigaos.h:39
#define FTP_HANDLE
Definition: multi-app.c:29
CURLM * curl_multi_init(void)
Definition: multi.c:114
CURLMcode curl_multi_cleanup(CURLM *multi_handle)
Definition: multi.c:583
#define HTTP_HANDLE
Definition: multi-app.c:28
CURL * easy_handle
Definition: multi.h:97
CURLMSG msg
Definition: multi.h:96
CURLMcode curl_multi_perform(CURLM *multi_handle, int *running_handles)
Definition: multi.c:306
void CURLM
Definition: multi.h:76
void CURL
Definition: types.h:25
#define TRUE
Definition: mprintf.c:69
CURLMcode curl_multi_add_handle(CURLM *multi_handle, CURL *curl_handle)
Definition: multi.c:134
Definition: multi.h:95
void curl_easy_cleanup(CURL *curl)
Definition: easy.c:288
#define HANDLECOUNT
Definition: multi-app.c:27
CURL * curl_easy_init(void)
Definition: easy.c:195
CURLMsg * curl_multi_info_read(CURLM *multi_handle, int *msgs_in_queue)
Definition: multi.c:614