using c-ares and libcurl
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
只回答业务咨询
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
#include #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include #include <string.h> #include <assert.h> #define ERROR_EXIT(x...) do {fprintf(stderr, x);fprintf(stderr, ", %s:%d", __FILE__, __LINE__);exit(-1);}while(0) #define DEBUG(x...) do {fprintf(stdout, x);}while(0) #define ERROR(x...) do {fprintf(stderr, x);fprintf(stderr, ", %s:%d", __FILE__, __LINE__);}while(0) CURLM *multi_handle = NULL; int epoll_fd = -1; int still_running; static void mcode_or_die(const char *where, CURLMcode code) { if ( CURLM_OK != code ) { const char *s; switch (code) { case CURLM_CALL_MULTI_PERFORM: s="CURLM_CALL_MULTI_PERFORM"; break; case CURLM_BAD_HANDLE: s="CURLM_BAD_HANDLE"; break; case CURLM_BAD_EASY_HANDLE: s="CURLM_BAD_EASY_HANDLE"; break; case CURLM_OUT_OF_MEMORY: s="CURLM_OUT_OF_MEMORY"; break; case CURLM_INTERNAL_ERROR: s="CURLM_INTERNAL_ERROR"; break; case CURLM_UNKNOWN_OPTION: s="CURLM_UNKNOWN_OPTION"; break; case CURLM_LAST: s="CURLM_LAST"; break; default: s="CURLM_unknown"; break; case CURLM_BAD_SOCKET: s="CURLM_BAD_SOCKET"; ERROR("ERROR: %s returns %s\n", where, s); /* ignore this error */ return; } ERROR("ERROR: %s returns %s\n", where, s); exit(code); } else { // DEBUG("INFO: %s returns OK\n", where); } } static void check_multi_info() { char *eff_url; CURLcode res; int msgs_left; CURLMsg *msg; CURL *easy; while ((msg = curl_multi_info_read(multi_handle, &msgs_left))) { if (msg->msg == CURLMSG_DONE) { easy = msg->easy_handle; res = msg->data.result; curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &eff_url); DEBUG("DONE: %s => (%d)\n", eff_url, res); curl_multi_remove_handle(multi_handle, easy); curl_easy_cleanup(easy); } } } /* static int multi_timer_cb(CURLM *multi, long timeout_ms, void *g) { DEBUG("multi_timer_cb\n"); DEBUG("multi_timer_cb: Setting timeout to %ld ms\n", timeout_ms); CURLMcode rc = curl_multi_socket_action(multi_handle, CURL_SOCKET_TIMEOUT, 0, &still_running); mcode_or_die("multi_timer_cb", rc); check_multi_info(); return 0; } */ static int multi_socket_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp) { DEBUG("multi_socket_cb--------------------------------------------------------------%p\n", sockp); struct epoll_event ee; ee.data.fd = s; switch ( what ) { case CURL_POLL_REMOVE: epoll_ctl(epoll_fd, EPOLL_CTL_DEL, s, NULL); DEBUG("---->>>>finish%p\n", sockp); // clean return 0; case CURL_POLL_IN: ee.events = EPOLLIN; DEBUG("---->>>>EPOLLIN%p\n", sockp); epoll_ctl(epoll_fd, EPOLL_CTL_MOD, s, &ee); break; case CURL_POLL_OUT: ee.events = EPOLLOUT; epoll_ctl(epoll_fd, EPOLL_CTL_ADD, s, &ee); DEBUG("---->>>>EPOLLOUT%p\n", sockp); break; case CURL_POLL_INOUT: ee.events = EPOLLIN | EPOLLOUT; DEBUG("---->>>>EPOLLINOUT%p\n", sockp); break; } /* if ( sockp ) { epoll_ctl(epoll_fd, EPOLL_CTL_MOD, s, &ee); } else { epoll_ctl(epoll_fd, EPOLL_CTL_ADD, s, &ee); curl_multi_assign(multi_handle, s, (void *)"hahaha"); } */ return 0; } FILE *filep = NULL; static size_t easy_write_cb(void *ptr, size_t size, size_t nmemb, void *data) { if ( filep == NULL ) { filep = fopen("/tmp/a.html", "w"); assert(filep); } fwrite(ptr, size, nmemb, filep); size_t realsize = size * nmemb; DEBUG("======> size:%u\n",realsize); return realsize; } int main(int argc, char **argv) { if ( argc != 1 ) { ERROR_EXIT("get"); } curl_global_init(CURL_GLOBAL_ALL); multi_handle = curl_multi_init(); if ( NULL == multi_handle ) { ERROR_EXIT("curl_multi_init"); } curl_multi_setopt(multi_handle, CURLMOPT_SOCKETFUNCTION, multi_socket_cb); curl_multi_setopt(multi_handle, CURLMOPT_SOCKETDATA, NULL); // curl_multi_setopt(multi_handle, CURLMOPT_TIMERFUNCTION, multi_timer_cb); // curl_multi_setopt(multi_handle, CURLMOPT_TIMERDATA, NULL); epoll_fd = epoll_create(1024); if ( -1 == epoll_fd ) { ERROR_EXIT("epoll_create"); } struct epoll_event ees[10]; ees[0].data.fd = 0; ees[0].events = EPOLLIN; if ( 0 != epoll_ctl(epoll_fd, EPOLL_CTL_ADD, 0, ees) ) { ERROR_EXIT("epoll_ctl"); } int ret = 0; FILE *pFlieInput = fdopen(0, "r"); char line[1024]; while ( 1 ) { ret = epoll_wait(epoll_fd, ees, 10, 10); for ( int i = 0;i < ret;i++ ) { int fd = ees[i].data.fd; if ( ees[i].data.fd == 0 ) { fgets(line, sizeof(line), pFlieInput); int line_len = strlen(line); if ( line[line_len - 1] == '\n' ) { line[line_len - 1] = 0; } DEBUG("url:%s\n", line); CURL *easy_handle = curl_easy_init(); curl_easy_setopt(easy_handle, CURLOPT_URL, line); // curl_easy_setopt(easy_handle, CURLOPT_VERBOSE, 1L); curl_multi_add_handle(multi_handle, easy_handle); curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, easy_write_cb); struct timeval tv1, tv2; gettimeofday(&tv1, NULL); curl_multi_socket_action(multi_handle, CURL_SOCKET_TIMEOUT,0, &still_running); gettimeofday(&tv2, NULL); DEBUG("duration: %u\n", (tv2.tv_sec - tv1.tv_sec)*1000000+(tv2.tv_usec - tv1.tv_usec)); // curl_easy_setopt(easy_handle, CURLOPT_PRIVATE, NULL); } else { // curl connection; int action = (ees[i].events & EPOLLIN ? CURL_CSELECT_IN : 0) | (ees[i].events & EPOLLOUT ? CURL_CSELECT_OUT : 0); struct timeval tv1, tv2; gettimeofday(&tv1, NULL); CURLMcode rc = curl_multi_socket_action(multi_handle, fd, action, &still_running); //DEBUG("TIMES:%s\n", "action"); gettimeofday(&tv2, NULL); DEBUG("duration222222: %u\n", (tv2.tv_sec - tv1.tv_sec)*1000000+(tv2.tv_usec - tv1.tv_usec)); mcode_or_die("event_cb: curl_multi_socket_action", rc); check_multi_info(); } } } curl_multi_cleanup(multi_handle); curl_global_cleanup(); return 0; } /////////////////////////// |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=588