Skip to content

Instantly share code, notes, and snippets.

@TvdW
Created April 8, 2019 19:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TvdW/a8bf6eabc99bf812d2fd0be1d32f9313 to your computer and use it in GitHub Desktop.
Save TvdW/a8bf6eabc99bf812d2fd0be1d32f9313 to your computer and use it in GitHub Desktop.
#include <string.h>
#include <assert.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; }
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;
}
return 0;
}
size_t no_write(char *ptr, size_t size, size_t nmemb, void *userdata) {
return size * nmemb;
}
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);
int j;
for (j = 0; j < 11; j++) {
CURL *easy = curl_easy_init();
curl_easy_setopt(easy, CURLOPT_POST, 1);
curl_easy_setopt(easy, CURLOPT_POSTFIELDS, "0");
curl_easy_setopt(easy, CURLOPT_URL, "https://home.xifon.eu/sleep/100");
curl_easy_setopt(easy, CURLOPT_TIMEOUT_MS, 5000);
curl_easy_setopt(easy, CURLOPT_WRITEFUNCTION, no_write);
curl_easy_setopt(easy, CURLOPT_VERBOSE, 1);
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);
}
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);
}
int i;
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);
}
}
}
int msgq;
CURLMsg *message = curl_multi_info_read(multi, &msgq);
assert(msgq == 0);
if (message->data.result != CURLE_OK) {
fprintf(stderr, "ERROR %s\n", curl_easy_strerror(message->data.result));
return 1;
}
curl_multi_remove_handle(multi, easy);
curl_easy_cleanup(easy);
}
curl_multi_cleanup(multi);
fprintf(stderr, "OK\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment