Created
January 4, 2019 15:27
-
-
Save TvdW/c41cb1e74feb0c5f79be83f9d330a74b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
#include <curl/curl.h> | |
fd_set wfds; | |
fd_set rfds; | |
struct timeval tv; | |
int maxfd = 0; | |
int socket_callback(CURL *easy, /* easy handle */ | |
curl_socket_t s, /* socket */ | |
int what, /* describes the socket */ | |
void *userp, /* private callback pointer */ | |
void *socketp) /* private socket pointer */ | |
{ | |
FD_CLR(s, &wfds); | |
FD_CLR(s, &rfds); | |
if (what == CURL_POLL_IN || what == CURL_POLL_INOUT) { | |
FD_SET(s, &rfds); | |
} | |
if (what == CURL_POLL_OUT || what == CURL_POLL_INOUT) { | |
FD_SET(s, &wfds); | |
} | |
if (s > maxfd) { maxfd = s; } | |
fprintf(stderr, "set %d %d\n", s, what); | |
return 0; | |
} | |
int timer_callback(CURLM *multi, /* multi handle */ | |
long timeout_ms, /* see above */ | |
void *userp) /* private callback pointer */ | |
{ | |
if (timeout_ms == -1) { | |
tv.tv_sec = 10; | |
tv.tv_usec = 0; | |
} else { | |
tv.tv_sec = timeout_ms / 1000; | |
tv.tv_usec = (timeout_ms % 1000) * 1000; | |
} | |
fprintf(stderr, "timer %ld\n", timeout_ms); | |
return 0; | |
} | |
int main(int argc, char** argv) | |
{ | |
int running = 1; | |
curl_global_init(CURL_GLOBAL_ALL); | |
FD_ZERO(&wfds); | |
FD_ZERO(&rfds); | |
CURLM *multi = curl_multi_init(); | |
curl_multi_setopt(multi, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX); | |
curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, socket_callback); | |
curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, timer_callback); | |
char poststuff[100000]; | |
memset(poststuff, 0, 100000); | |
int i; | |
for (i = 0; i < 10; i++) { | |
CURL *easy = curl_easy_init(); | |
curl_easy_setopt(easy, CURLOPT_URL, argv[1]); | |
curl_easy_setopt(easy, CURLOPT_PIPEWAIT, 1); | |
curl_easy_setopt(easy, CURLOPT_VERBOSE, 1); | |
curl_easy_setopt(easy, CURLOPT_POST, 1); | |
curl_easy_setopt(easy, CURLOPT_POSTFIELDSIZE_LARGE, 100000L); | |
curl_easy_setopt(easy, CURLOPT_POSTFIELDS, poststuff); | |
curl_multi_add_handle(multi, easy); | |
} | |
tv.tv_sec = 0; | |
tv.tv_usec = 0; | |
running = 1; | |
/* BAD: will effectively process requests one at a time */ | |
while (running) { | |
if (tv.tv_sec == 0 && tv.tv_usec == 0) { | |
curl_multi_socket_action(multi, CURL_SOCKET_TIMEOUT, 0, &running); | |
} | |
fd_set crfds = rfds; | |
fd_set cwfds = wfds; | |
int count = select(maxfd+1, &crfds, &cwfds, NULL, &tv); | |
if (!count) { | |
curl_multi_socket_action(multi, CURL_SOCKET_TIMEOUT, 0, &running); | |
} | |
for (i = 0; i <= maxfd; i++) { | |
if (FD_ISSET(i, &crfds)) { | |
fprintf(stderr, "action read %d\n", i); | |
curl_multi_socket_action(multi, i, CURL_CSELECT_IN, &running); | |
} | |
if (FD_ISSET(i, &cwfds)) { | |
fprintf(stderr, "action write %d\n", i); | |
curl_multi_socket_action(multi, i, CURL_CSELECT_OUT, &running); | |
} | |
} | |
} | |
/* GOOD: "Just Works" */ | |
while (running) { | |
curl_multi_perform(multi, &running); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment