诸暨麻将添加redis
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

437 строки
12 KiB

  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2013, 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 http://curl.haxx.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. ***************************************************************************/
  22. /* Example application source code using the multi socket interface to
  23. download many files at once.
  24. Written by Jeff Pohlmeyer
  25. Requires libevent version 2 and a (POSIX?) system that has mkfifo().
  26. This is an adaptation of libcurl's "hipev.c" and libevent's "event-test.c"
  27. sample programs.
  28. When running, the program creates the named pipe "hiper.fifo"
  29. Whenever there is input into the fifo, the program reads the input as a list
  30. of URL's and creates some new easy handles to fetch each URL via the
  31. curl_multi "hiper" API.
  32. Thus, you can try a single URL:
  33. % echo http://www.yahoo.com > hiper.fifo
  34. Or a whole bunch of them:
  35. % cat my-url-list > hiper.fifo
  36. The fifo buffer is handled almost instantly, so you can even add more URL's
  37. while the previous requests are still being downloaded.
  38. Note:
  39. For the sake of simplicity, URL length is limited to 1023 char's !
  40. This is purely a demo app, all retrieved data is simply discarded by the write
  41. callback.
  42. */
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <stdlib.h>
  46. #include <sys/time.h>
  47. #include <time.h>
  48. #include <unistd.h>
  49. #include <sys/poll.h>
  50. #include <curl/curl.h>
  51. #include <event2/event.h>
  52. #include <fcntl.h>
  53. #include <sys/stat.h>
  54. #include <errno.h>
  55. #define MSG_OUT stdout /* Send info to stdout, change to stderr if you want */
  56. /* Global information, common to all connections */
  57. typedef struct _GlobalInfo
  58. {
  59. struct event_base *evbase;
  60. struct event *fifo_event;
  61. struct event *timer_event;
  62. CURLM *multi;
  63. int still_running;
  64. FILE* input;
  65. } GlobalInfo;
  66. /* Information associated with a specific easy handle */
  67. typedef struct _ConnInfo
  68. {
  69. CURL *easy;
  70. char *url;
  71. GlobalInfo *global;
  72. char error[CURL_ERROR_SIZE];
  73. } ConnInfo;
  74. /* Information associated with a specific socket */
  75. typedef struct _SockInfo
  76. {
  77. curl_socket_t sockfd;
  78. CURL *easy;
  79. int action;
  80. long timeout;
  81. struct event *ev;
  82. int evset;
  83. GlobalInfo *global;
  84. } SockInfo;
  85. /* Update the event timer after curl_multi library calls */
  86. static int multi_timer_cb(CURLM *multi, long timeout_ms, GlobalInfo *g)
  87. {
  88. struct timeval timeout;
  89. (void)multi; /* unused */
  90. timeout.tv_sec = timeout_ms/1000;
  91. timeout.tv_usec = (timeout_ms%1000)*1000;
  92. fprintf(MSG_OUT, "multi_timer_cb: Setting timeout to %ld ms\n", timeout_ms);
  93. evtimer_add(g->timer_event, &timeout);
  94. return 0;
  95. }
  96. /* Die if we get a bad CURLMcode somewhere */
  97. static void mcode_or_die(const char *where, CURLMcode code)
  98. {
  99. if ( CURLM_OK != code ) {
  100. const char *s;
  101. switch (code) {
  102. case CURLM_CALL_MULTI_PERFORM: s="CURLM_CALL_MULTI_PERFORM"; break;
  103. case CURLM_BAD_HANDLE: s="CURLM_BAD_HANDLE"; break;
  104. case CURLM_BAD_EASY_HANDLE: s="CURLM_BAD_EASY_HANDLE"; break;
  105. case CURLM_OUT_OF_MEMORY: s="CURLM_OUT_OF_MEMORY"; break;
  106. case CURLM_INTERNAL_ERROR: s="CURLM_INTERNAL_ERROR"; break;
  107. case CURLM_UNKNOWN_OPTION: s="CURLM_UNKNOWN_OPTION"; break;
  108. case CURLM_LAST: s="CURLM_LAST"; break;
  109. default: s="CURLM_unknown";
  110. break;
  111. case CURLM_BAD_SOCKET: s="CURLM_BAD_SOCKET";
  112. fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s);
  113. /* ignore this error */
  114. return;
  115. }
  116. fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s);
  117. exit(code);
  118. }
  119. }
  120. /* Check for completed transfers, and remove their easy handles */
  121. static void check_multi_info(GlobalInfo *g)
  122. {
  123. char *eff_url;
  124. CURLMsg *msg;
  125. int msgs_left;
  126. ConnInfo *conn;
  127. CURL *easy;
  128. CURLcode res;
  129. fprintf(MSG_OUT, "REMAINING: %d\n", g->still_running);
  130. while ((msg = curl_multi_info_read(g->multi, &msgs_left))) {
  131. if (msg->msg == CURLMSG_DONE) {
  132. easy = msg->easy_handle;
  133. res = msg->data.result;
  134. curl_easy_getinfo(easy, CURLINFO_PRIVATE, &conn);
  135. curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &eff_url);
  136. fprintf(MSG_OUT, "DONE: %s => (%d) %s\n", eff_url, res, conn->error);
  137. curl_multi_remove_handle(g->multi, easy);
  138. free(conn->url);
  139. curl_easy_cleanup(easy);
  140. free(conn);
  141. }
  142. }
  143. }
  144. /* Called by libevent when we get action on a multi socket */
  145. static void event_cb(int fd, short kind, void *userp)
  146. {
  147. GlobalInfo *g = (GlobalInfo*) userp;
  148. CURLMcode rc;
  149. int action =
  150. (kind & EV_READ ? CURL_CSELECT_IN : 0) |
  151. (kind & EV_WRITE ? CURL_CSELECT_OUT : 0);
  152. rc = curl_multi_socket_action(g->multi, fd, action, &g->still_running);
  153. mcode_or_die("event_cb: curl_multi_socket_action", rc);
  154. check_multi_info(g);
  155. if ( g->still_running <= 0 ) {
  156. fprintf(MSG_OUT, "last transfer done, kill timeout\n");
  157. if (evtimer_pending(g->timer_event, NULL)) {
  158. evtimer_del(g->timer_event);
  159. }
  160. }
  161. }
  162. /* Called by libevent when our timeout expires */
  163. static void timer_cb(int fd, short kind, void *userp)
  164. {
  165. GlobalInfo *g = (GlobalInfo *)userp;
  166. CURLMcode rc;
  167. (void)fd;
  168. (void)kind;
  169. rc = curl_multi_socket_action(g->multi,
  170. CURL_SOCKET_TIMEOUT, 0, &g->still_running);
  171. mcode_or_die("timer_cb: curl_multi_socket_action", rc);
  172. check_multi_info(g);
  173. }
  174. /* Clean up the SockInfo structure */
  175. static void remsock(SockInfo *f)
  176. {
  177. if (f) {
  178. if (f->evset)
  179. event_free(f->ev);
  180. free(f);
  181. }
  182. }
  183. /* Assign information to a SockInfo structure */
  184. static void setsock(SockInfo*f, curl_socket_t s, CURL*e, int act, GlobalInfo*g)
  185. {
  186. int kind =
  187. (act&CURL_POLL_IN?EV_READ:0)|(act&CURL_POLL_OUT?EV_WRITE:0)|EV_PERSIST;
  188. f->sockfd = s;
  189. f->action = act;
  190. f->easy = e;
  191. if (f->evset)
  192. event_free(f->ev);
  193. f->ev = event_new(g->evbase, f->sockfd, kind, event_cb, g);
  194. f->evset = 1;
  195. event_add(f->ev, NULL);
  196. }
  197. /* Initialize a new SockInfo structure */
  198. static void addsock(curl_socket_t s, CURL *easy, int action, GlobalInfo *g)
  199. {
  200. SockInfo *fdp = calloc(sizeof(SockInfo), 1);
  201. fdp->global = g;
  202. setsock(fdp, s, easy, action, g);
  203. curl_multi_assign(g->multi, s, fdp);
  204. }
  205. /* CURLMOPT_SOCKETFUNCTION */
  206. static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
  207. {
  208. GlobalInfo *g = (GlobalInfo*) cbp;
  209. SockInfo *fdp = (SockInfo*) sockp;
  210. const char *whatstr[]={ "none", "IN", "OUT", "INOUT", "REMOVE" };
  211. fprintf(MSG_OUT,
  212. "socket callback: s=%d e=%p what=%s ", s, e, whatstr[what]);
  213. if (what == CURL_POLL_REMOVE) {
  214. fprintf(MSG_OUT, "\n");
  215. remsock(fdp);
  216. }
  217. else {
  218. if (!fdp) {
  219. fprintf(MSG_OUT, "Adding data: %s\n", whatstr[what]);
  220. addsock(s, e, what, g);
  221. }
  222. else {
  223. fprintf(MSG_OUT,
  224. "Changing action from %s to %s\n",
  225. whatstr[fdp->action], whatstr[what]);
  226. setsock(fdp, s, e, what, g);
  227. }
  228. }
  229. return 0;
  230. }
  231. /* CURLOPT_WRITEFUNCTION */
  232. static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data)
  233. {
  234. size_t realsize = size * nmemb;
  235. ConnInfo *conn = (ConnInfo*) data;
  236. (void)ptr;
  237. (void)conn;
  238. return realsize;
  239. }
  240. /* CURLOPT_PROGRESSFUNCTION */
  241. static int prog_cb (void *p, double dltotal, double dlnow, double ult,
  242. double uln)
  243. {
  244. ConnInfo *conn = (ConnInfo *)p;
  245. (void)ult;
  246. (void)uln;
  247. fprintf(MSG_OUT, "Progress: %s (%g/%g)\n", conn->url, dlnow, dltotal);
  248. return 0;
  249. }
  250. /* Create a new easy handle, and add it to the global curl_multi */
  251. static void new_conn(char *url, GlobalInfo *g )
  252. {
  253. ConnInfo *conn;
  254. CURLMcode rc;
  255. conn = calloc(1, sizeof(ConnInfo));
  256. memset(conn, 0, sizeof(ConnInfo));
  257. conn->error[0]='\0';
  258. conn->easy = curl_easy_init();
  259. if (!conn->easy) {
  260. fprintf(MSG_OUT, "curl_easy_init() failed, exiting!\n");
  261. exit(2);
  262. }
  263. conn->global = g;
  264. conn->url = strdup(url);
  265. curl_easy_setopt(conn->easy, CURLOPT_URL, conn->url);
  266. curl_easy_setopt(conn->easy, CURLOPT_WRITEFUNCTION, write_cb);
  267. curl_easy_setopt(conn->easy, CURLOPT_WRITEDATA, &conn);
  268. curl_easy_setopt(conn->easy, CURLOPT_VERBOSE, 1L);
  269. curl_easy_setopt(conn->easy, CURLOPT_ERRORBUFFER, conn->error);
  270. curl_easy_setopt(conn->easy, CURLOPT_PRIVATE, conn);
  271. curl_easy_setopt(conn->easy, CURLOPT_NOPROGRESS, 0L);
  272. curl_easy_setopt(conn->easy, CURLOPT_PROGRESSFUNCTION, prog_cb);
  273. curl_easy_setopt(conn->easy, CURLOPT_PROGRESSDATA, conn);
  274. fprintf(MSG_OUT,
  275. "Adding easy %p to multi %p (%s)\n", conn->easy, g->multi, url);
  276. rc = curl_multi_add_handle(g->multi, conn->easy);
  277. mcode_or_die("new_conn: curl_multi_add_handle", rc);
  278. /* note that the add_handle() will set a time-out to trigger very soon so
  279. that the necessary socket_action() call will be called by this app */
  280. }
  281. /* This gets called whenever data is received from the fifo */
  282. static void fifo_cb(int fd, short event, void *arg)
  283. {
  284. char s[1024];
  285. long int rv=0;
  286. int n=0;
  287. GlobalInfo *g = (GlobalInfo *)arg;
  288. (void)fd; /* unused */
  289. (void)event; /* unused */
  290. do {
  291. s[0]='\0';
  292. rv=fscanf(g->input, "%1023s%n", s, &n);
  293. s[n]='\0';
  294. if ( n && s[0] ) {
  295. new_conn(s,arg); /* if we read a URL, go get it! */
  296. } else break;
  297. } while ( rv != EOF);
  298. }
  299. /* Create a named pipe and tell libevent to monitor it */
  300. static const char *fifo = "hiper.fifo";
  301. static int init_fifo (GlobalInfo *g)
  302. {
  303. struct stat st;
  304. curl_socket_t sockfd;
  305. fprintf(MSG_OUT, "Creating named pipe \"%s\"\n", fifo);
  306. if (lstat (fifo, &st) == 0) {
  307. if ((st.st_mode & S_IFMT) == S_IFREG) {
  308. errno = EEXIST;
  309. perror("lstat");
  310. exit (1);
  311. }
  312. }
  313. unlink(fifo);
  314. if (mkfifo (fifo, 0600) == -1) {
  315. perror("mkfifo");
  316. exit (1);
  317. }
  318. sockfd = open(fifo, O_RDWR | O_NONBLOCK, 0);
  319. if (sockfd == -1) {
  320. perror("open");
  321. exit (1);
  322. }
  323. g->input = fdopen(sockfd, "r");
  324. fprintf(MSG_OUT, "Now, pipe some URL's into > %s\n", fifo);
  325. g->fifo_event = event_new(g->evbase, sockfd, EV_READ|EV_PERSIST, fifo_cb, g);
  326. event_add(g->fifo_event, NULL);
  327. return (0);
  328. }
  329. static void clean_fifo(GlobalInfo *g)
  330. {
  331. event_free(g->fifo_event);
  332. fclose(g->input);
  333. unlink(fifo);
  334. }
  335. int main(int argc, char **argv)
  336. {
  337. GlobalInfo g;
  338. (void)argc;
  339. (void)argv;
  340. memset(&g, 0, sizeof(GlobalInfo));
  341. g.evbase = event_base_new();
  342. init_fifo(&g);
  343. g.multi = curl_multi_init();
  344. g.timer_event = evtimer_new(g.evbase, timer_cb, &g);
  345. /* setup the generic multi interface options we want */
  346. curl_multi_setopt(g.multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
  347. curl_multi_setopt(g.multi, CURLMOPT_SOCKETDATA, &g);
  348. curl_multi_setopt(g.multi, CURLMOPT_TIMERFUNCTION, multi_timer_cb);
  349. curl_multi_setopt(g.multi, CURLMOPT_TIMERDATA, &g);
  350. /* we don't call any curl_multi_socket*() function yet as we have no handles
  351. added! */
  352. event_base_dispatch(g.evbase);
  353. /* this, of course, won't get called since only way to stop this program is
  354. via ctrl-C, but it is here to show how cleanup /would/ be done. */
  355. clean_fifo(&g);
  356. event_free(g.timer_event);
  357. event_base_free(g.evbase);
  358. curl_multi_cleanup(g.multi);
  359. return 0;
  360. }