Created
October 16, 2019 10:18
-
-
Save TvdW/b07e146eb648fd5ba05f70e87dee0a3b 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> | |
#include <assert.h> | |
#include <unistd.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; } | |
return 0; | |
} | |
int timer_callback(CURLM *multi, /* multi handle */ | |
long timeout_ms, /* see above */ | |
void *userp) /* private callback pointer */ | |
{ | |
fprintf(stderr, "SET TIMEOUT to %ld\n", timeout_ms); | |
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; | |
} | |
return 0; | |
} | |
static size_t write_cb(char *d, size_t n, size_t l, void *p) | |
{ | |
/* take care of the data here, ignored in this example */ | |
(void)d; | |
(void)p; | |
return n*l; | |
} | |
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_SOCKETFUNCTION, socket_callback); | |
curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, timer_callback); | |
curl_multi_setopt(multi, CURLMOPT_MAX_HOST_CONNECTIONS, 4); | |
curl_multi_setopt(multi, CURLMOPT_MAXCONNECTS, 128); | |
curl_multi_setopt(multi, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX); | |
int remaining = 10000; | |
while (remaining >= 0) { | |
int i; | |
for (i = 0; i < 100; i++) { | |
remaining--; | |
CURL *easy = curl_easy_init(); | |
curl_easy_setopt(easy, CURLOPT_URL, "https://domisc-01.server.tvdw.eu/"); | |
curl_easy_setopt(easy, CURLOPT_VERBOSE, 1L); | |
curl_easy_setopt(easy, CURLOPT_WRITEFUNCTION, write_cb); | |
curl_easy_setopt(easy, CURLOPT_TIMEOUT, 5); | |
curl_easy_setopt(easy, CURLOPT_PIPEWAIT, 1L); | |
curl_multi_add_handle(multi, easy); | |
} | |
tv.tv_sec = 0; | |
tv.tv_usec = 0; | |
running = 1; | |
while (running) { | |
if (tv.tv_sec == 0 && tv.tv_usec == 0) { | |
curl_multi_socket_action(multi, CURL_SOCKET_TIMEOUT, 0, &running); | |
if(!running) | |
goto end; | |
} | |
fd_set crfds = rfds; | |
fd_set cwfds = wfds; | |
int now = 0; | |
if(!tv.tv_sec && !tv.tv_usec) { | |
now = 1; | |
fprintf(stderr, "=== run now!\n"); | |
} | |
for (i = 0; i <= maxfd; i++) { | |
if (FD_ISSET(i, &crfds) || | |
FD_ISSET(i, &cwfds)) { | |
fprintf(stderr, "select() on socket %d\n", i); | |
} | |
} | |
int count = select(maxfd+1, &crfds, &cwfds, NULL, &tv); | |
if (!count) { | |
curl_multi_socket_action(multi, CURL_SOCKET_TIMEOUT, 0, &running); | |
if(!running) | |
goto end; | |
} | |
for (i = 0; i <= maxfd; i++) { | |
if (FD_ISSET(i, &crfds)) { | |
curl_multi_socket_action(multi, i, CURL_CSELECT_IN, &running); | |
} | |
if (FD_ISSET(i, &cwfds)) { | |
curl_multi_socket_action(multi, i, CURL_CSELECT_OUT, &running); | |
} | |
} | |
if(now && !tv.tv_sec && !tv.tv_usec) { | |
fprintf(stderr, "=== still NOW?\n"); | |
usleep(100000); | |
} | |
end: | |
while (1) { | |
int msgq; | |
struct CURLMsg *msg = curl_multi_info_read(multi, &msgq); | |
if (msg && (msg->msg == CURLMSG_DONE)) { | |
CURL *easy = msg->easy_handle; | |
fprintf(stderr, "Client %p ended, code: %d\n", easy, msg->data.result); | |
if (msg->data.result != CURLE_OK) { | |
return 1; | |
} | |
curl_multi_remove_handle(multi, easy); | |
curl_easy_cleanup(easy); | |
} | |
if (!msg) { | |
break; | |
} | |
} | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment