Imported Upstream version 7.59.0
[platform/upstream/curl.git] / docs / examples / ghiper.c
index a1af4c5..fd643fc 100644 (file)
@@ -5,7 +5,7 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
@@ -99,14 +99,14 @@ static void mcode_or_die(const char *where, CURLMcode code)
   if(CURLM_OK != code) {
     const char *s;
     switch(code) {
-    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_BAD_SOCKET:         s="CURLM_BAD_SOCKET";         break;
-    case     CURLM_UNKNOWN_OPTION:     s="CURLM_UNKNOWN_OPTION";     break;
-    case     CURLM_LAST:               s="CURLM_LAST";               break;
-    default: s="CURLM_unknown";
+    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_BAD_SOCKET:         s = "CURLM_BAD_SOCKET";         break;
+    case     CURLM_UNKNOWN_OPTION:     s = "CURLM_UNKNOWN_OPTION";     break;
+    case     CURLM_LAST:               s = "CURLM_LAST";               break;
+    default: s = "CURLM_unknown";
     }
     MSG_OUT("ERROR: %s returns %s\n", where, s);
     exit(code);
@@ -156,13 +156,22 @@ static gboolean timer_cb(gpointer data)
 static int update_timeout_cb(CURLM *multi, long timeout_ms, void *userp)
 {
   struct timeval timeout;
-  GlobalInfo *g=(GlobalInfo *)userp;
+  GlobalInfo *g = (GlobalInfo *)userp;
   timeout.tv_sec = timeout_ms/1000;
   timeout.tv_usec = (timeout_ms%1000)*1000;
 
   MSG_OUT("*** update_timeout_cb %ld => %ld:%ld ***\n",
           timeout_ms, timeout.tv_sec, timeout.tv_usec);
 
+  /* TODO
+   *
+   * if timeout_ms is 0, call curl_multi_socket_action() at once!
+   *
+   * if timeout_ms is -1, just delete the timer
+   *
+   * for all other values of timeout_ms, this should set or *update*
+   * the timer to the new value
+   */
   g->timer_event = g_timeout_add(timeout_ms, timer_cb, g);
   return 0;
 }
@@ -172,7 +181,7 @@ static gboolean event_cb(GIOChannel *ch, GIOCondition condition, gpointer data)
 {
   GlobalInfo *g = (GlobalInfo*) data;
   CURLMcode rc;
-  int fd=g_io_channel_unix_get_fd(ch);
+  int fd = g_io_channel_unix_get_fd(ch);
 
   int action =
     (condition & G_IO_IN ? CURL_CSELECT_IN : 0) |
@@ -219,7 +228,7 @@ static void setsock(SockInfo *f, curl_socket_t s, CURL *e, int act,
   if(f->ev) {
     g_source_remove(f->ev);
   }
-  f->ev=g_io_add_watch(f->ch, kind, event_cb, g);
+  f->ev = g_io_add_watch(f->ch, kind, event_cb, g);
 }
 
 /* Initialize a new SockInfo structure */
@@ -228,7 +237,7 @@ static void addsock(curl_socket_t s, CURL *easy, int action, GlobalInfo *g)
   SockInfo *fdp = g_malloc0(sizeof(SockInfo));
 
   fdp->global = g;
-  fdp->ch=g_io_channel_unix_new(s);
+  fdp->ch = g_io_channel_unix_new(s);
   setsock(fdp, s, easy, action, g);
   curl_multi_assign(g->multi, s, fdp);
 }
@@ -310,7 +319,7 @@ static void new_conn(char *url, GlobalInfo *g)
   curl_easy_setopt(conn->easy, CURLOPT_LOW_SPEED_TIME, 30L);
 
   MSG_OUT("Adding easy %p to multi %p (%s)\n", conn->easy, g->multi, url);
-  rc =curl_multi_add_handle(g->multi, conn->easy);
+  rc = curl_multi_add_handle(g->multi, conn->easy);
   mcode_or_die("new_conn: curl_multi_add_handle", rc);
 
   /* note that the add_handle() will set a time-out to trigger very soon so
@@ -322,11 +331,11 @@ static gboolean fifo_cb(GIOChannel *ch, GIOCondition condition, gpointer data)
 {
 #define BUF_SIZE 1024
   gsize len, tp;
-  gchar *buf, *tmp, *all=NULL;
+  gchar *buf, *tmp, *all = NULL;
   GIOStatus rv;
 
   do {
-    GError *err=NULL;
+    GError *err = NULL;
     rv = g_io_channel_read_line(ch, &buf, &len, &tp, &err);
     if(buf) {
       if(tp) {
@@ -336,15 +345,15 @@ static gboolean fifo_cb(GIOChannel *ch, GIOCondition condition, gpointer data)
       g_free(buf);
     }
     else {
-      buf = g_malloc(BUF_SIZE+1);
+      buf = g_malloc(BUF_SIZE + 1);
       while(TRUE) {
         buf[BUF_SIZE]='\0';
         g_io_channel_read_chars(ch, buf, BUF_SIZE, &len, &err);
         if(len) {
           buf[len]='\0';
           if(all) {
-            tmp=all;
-            all=g_strdup_printf("%s%s", tmp, buf);
+            tmp = all;
+            all = g_strdup_printf("%s%s", tmp, buf);
             g_free(tmp);
           }
           else {
@@ -408,12 +417,12 @@ int main(int argc, char **argv)
   GMainLoop*gmain;
   int fd;
   GIOChannel* ch;
-  g=g_malloc0(sizeof(GlobalInfo));
+  g = g_malloc0(sizeof(GlobalInfo));
 
-  fd=init_fifo();
-  ch=g_io_channel_unix_new(fd);
+  fd = init_fifo();
+  ch = g_io_channel_unix_new(fd);
   g_io_add_watch(ch, G_IO_IN, fifo_cb, g);
-  gmain=g_main_loop_new(NULL, FALSE);
+  gmain = g_main_loop_new(NULL, FALSE);
   g->multi = curl_multi_init();
   curl_multi_setopt(g->multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
   curl_multi_setopt(g->multi, CURLMOPT_SOCKETDATA, g);