诸暨麻将添加redis
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

453 lines
12 KiB

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