xref: /curl/docs/examples/multi-event.c (revision f540e43b)
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 
25 /* <DESC>
26  * multi_socket API using libevent
27  * </DESC>
28  */
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <event2/event.h>
33 #include <curl/curl.h>
34 
35 struct event_base *base;
36 CURLM *curl_handle;
37 struct event *timeout;
38 
39 typedef struct curl_context_s {
40   struct event *event;
41   curl_socket_t sockfd;
42 } curl_context_t;
43 
44 static void curl_perform(int fd, short event, void *arg);
45 
create_curl_context(curl_socket_t sockfd)46 static curl_context_t *create_curl_context(curl_socket_t sockfd)
47 {
48   curl_context_t *context;
49 
50   context = (curl_context_t *) malloc(sizeof(*context));
51 
52   context->sockfd = sockfd;
53 
54   context->event = event_new(base, sockfd, 0, curl_perform, context);
55 
56   return context;
57 }
58 
destroy_curl_context(curl_context_t * context)59 static void destroy_curl_context(curl_context_t *context)
60 {
61   event_del(context->event);
62   event_free(context->event);
63   free(context);
64 }
65 
add_download(const char * url,int num)66 static void add_download(const char *url, int num)
67 {
68   char filename[50];
69   FILE *file;
70   CURL *handle;
71 
72   snprintf(filename, 50, "%d.download", num);
73 
74   file = fopen(filename, "wb");
75   if(!file) {
76     fprintf(stderr, "Error opening %s\n", filename);
77     return;
78   }
79 
80   handle = curl_easy_init();
81   curl_easy_setopt(handle, CURLOPT_WRITEDATA, file);
82   curl_easy_setopt(handle, CURLOPT_PRIVATE, file);
83   curl_easy_setopt(handle, CURLOPT_URL, url);
84   curl_multi_add_handle(curl_handle, handle);
85   fprintf(stderr, "Added download %s -> %s\n", url, filename);
86 }
87 
check_multi_info(void)88 static void check_multi_info(void)
89 {
90   char *done_url;
91   CURLMsg *message;
92   int pending;
93   CURL *easy_handle;
94   FILE *file;
95 
96   while((message = curl_multi_info_read(curl_handle, &pending))) {
97     switch(message->msg) {
98     case CURLMSG_DONE:
99       /* Do not use message data after calling curl_multi_remove_handle() and
100          curl_easy_cleanup(). As per curl_multi_info_read() docs:
101          "WARNING: The data the returned pointer points to does not survive
102          calling curl_multi_cleanup, curl_multi_remove_handle or
103          curl_easy_cleanup." */
104       easy_handle = message->easy_handle;
105 
106       curl_easy_getinfo(easy_handle, CURLINFO_EFFECTIVE_URL, &done_url);
107       curl_easy_getinfo(easy_handle, CURLINFO_PRIVATE, &file);
108       printf("%s DONE\n", done_url);
109 
110       curl_multi_remove_handle(curl_handle, easy_handle);
111       curl_easy_cleanup(easy_handle);
112       if(file) {
113         fclose(file);
114       }
115       break;
116 
117     default:
118       fprintf(stderr, "CURLMSG default\n");
119       break;
120     }
121   }
122 }
123 
curl_perform(int fd,short event,void * arg)124 static void curl_perform(int fd, short event, void *arg)
125 {
126   int running_handles;
127   int flags = 0;
128   curl_context_t *context;
129 
130   if(event & EV_READ)
131     flags |= CURL_CSELECT_IN;
132   if(event & EV_WRITE)
133     flags |= CURL_CSELECT_OUT;
134 
135   context = (curl_context_t *) arg;
136 
137   curl_multi_socket_action(curl_handle, context->sockfd, flags,
138                            &running_handles);
139 
140   check_multi_info();
141 }
142 
on_timeout(evutil_socket_t fd,short events,void * arg)143 static void on_timeout(evutil_socket_t fd, short events, void *arg)
144 {
145   int running_handles;
146   curl_multi_socket_action(curl_handle, CURL_SOCKET_TIMEOUT, 0,
147                            &running_handles);
148   check_multi_info();
149 }
150 
start_timeout(CURLM * multi,long timeout_ms,void * userp)151 static int start_timeout(CURLM *multi, long timeout_ms, void *userp)
152 {
153   if(timeout_ms < 0) {
154     evtimer_del(timeout);
155   }
156   else {
157     if(timeout_ms == 0)
158       timeout_ms = 1; /* 0 means call socket_action asap */
159     struct timeval tv;
160     tv.tv_sec = timeout_ms / 1000;
161     tv.tv_usec = (timeout_ms % 1000) * 1000;
162     evtimer_del(timeout);
163     evtimer_add(timeout, &tv);
164   }
165   return 0;
166 }
167 
handle_socket(CURL * easy,curl_socket_t s,int action,void * userp,void * socketp)168 static int handle_socket(CURL *easy, curl_socket_t s, int action, void *userp,
169                   void *socketp)
170 {
171   curl_context_t *curl_context;
172   int events = 0;
173 
174   switch(action) {
175   case CURL_POLL_IN:
176   case CURL_POLL_OUT:
177   case CURL_POLL_INOUT:
178     curl_context = socketp ?
179       (curl_context_t *) socketp : create_curl_context(s);
180 
181     curl_multi_assign(curl_handle, s, (void *) curl_context);
182 
183     if(action != CURL_POLL_IN)
184       events |= EV_WRITE;
185     if(action != CURL_POLL_OUT)
186       events |= EV_READ;
187 
188     events |= EV_PERSIST;
189 
190     event_del(curl_context->event);
191     event_assign(curl_context->event, base, curl_context->sockfd, events,
192       curl_perform, curl_context);
193     event_add(curl_context->event, NULL);
194 
195     break;
196   case CURL_POLL_REMOVE:
197     if(socketp) {
198       event_del(((curl_context_t*) socketp)->event);
199       destroy_curl_context((curl_context_t*) socketp);
200       curl_multi_assign(curl_handle, s, NULL);
201     }
202     break;
203   default:
204     abort();
205   }
206 
207   return 0;
208 }
209 
main(int argc,char ** argv)210 int main(int argc, char **argv)
211 {
212   if(argc <= 1)
213     return 0;
214 
215   if(curl_global_init(CURL_GLOBAL_ALL)) {
216     fprintf(stderr, "Could not init curl\n");
217     return 1;
218   }
219 
220   base = event_base_new();
221   timeout = evtimer_new(base, on_timeout, NULL);
222 
223   curl_handle = curl_multi_init();
224   curl_multi_setopt(curl_handle, CURLMOPT_SOCKETFUNCTION, handle_socket);
225   curl_multi_setopt(curl_handle, CURLMOPT_TIMERFUNCTION, start_timeout);
226 
227   while(argc-- > 1) {
228     add_download(argv[argc], argc);
229   }
230 
231   event_base_dispatch(base);
232 
233   curl_multi_cleanup(curl_handle);
234   event_free(timeout);
235   event_base_free(base);
236 
237   libevent_global_shutdown();
238   curl_global_cleanup();
239 
240   return 0;
241 }
242