Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

multi: add curl_multi_wait()

/*
 * Name:     curl_multi_wait()
 *
 * Desc:     Poll on all fds within a CURLM set as well as any
 *           additional fds passed to the function.
 *
 * Returns:  CURLMcode type, general multi error code.
 */
CURL_EXTERN CURLMcode curl_multi_wait(CURLM *multi_handle,
                                      struct curl_waitfd extra_fds[],
                                      unsigned int extra_nfds,
                                      int timeout_ms);
  • Loading branch information...
commit de24d7bd4c03ea3eeba928edc9ae9e7a826c67c8 1 parent 2f6e1a8
Sara Golemon authored August 27, 2012 bagder committed September 01, 2012
28  include/curl/multi.h
@@ -7,7 +7,7 @@
7 7
  *                            | (__| |_| |  _ <| |___
8 8
  *                             \___|\___/|_| \_\_____|
9 9
  *
10  
- * Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
  10
+ * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
11 11
  *
12 12
  * This software is licensed as described in the file COPYING, which
13 13
  * you should have received as part of this distribution. The terms
@@ -89,6 +89,19 @@ struct CURLMsg {
89 89
 };
90 90
 typedef struct CURLMsg CURLMsg;
91 91
 
  92
+/* Based on poll(2) structure and values.
  93
+ * We don't use pollfd and POLL* constants explicitly
  94
+ * to cover platforms without poll(). */
  95
+#define CURL_WAIT_POLLIN    0x0001
  96
+#define CURL_WAIT_POLLPRI   0x0002
  97
+#define CURL_WAIT_POLLOUT   0x0004
  98
+
  99
+struct curl_waitfd {
  100
+  curl_socket_t fd;
  101
+  short events;
  102
+  short revents; /* not supported yet */
  103
+};
  104
+
92 105
 /*
93 106
  * Name:    curl_multi_init()
94 107
  *
@@ -133,6 +146,19 @@ CURL_EXTERN CURLMcode curl_multi_fdset(CURLM *multi_handle,
133 146
                                        fd_set *exc_fd_set,
134 147
                                        int *max_fd);
135 148
 
  149
+/*
  150
+ * Name:     curl_multi_wait()
  151
+ *
  152
+ * Desc:     Poll on all fds within a CURLM set as well as any
  153
+ *           additional fds passed to the function.
  154
+ *
  155
+ * Returns:  CURLMcode type, general multi error code.
  156
+ */
  157
+CURL_EXTERN CURLMcode curl_multi_wait(CURLM *multi_handle,
  158
+                                      struct curl_waitfd extra_fds[],
  159
+                                      unsigned int extra_nfds,
  160
+                                      int timeout_ms);
  161
+
136 162
  /*
137 163
   * Name:    curl_multi_perform()
138 164
   *
87  lib/multi.c
@@ -941,6 +941,93 @@ CURLMcode curl_multi_fdset(CURLM *multi_handle,
941 941
   return CURLM_OK;
942 942
 }
943 943
 
  944
+CURLMcode curl_multi_wait(CURLM *multi_handle,
  945
+                          struct curl_waitfd extra_fds[],
  946
+                          unsigned int extra_nfds,
  947
+                          int timeout_ms)
  948
+{
  949
+  struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
  950
+  struct Curl_one_easy *easy;
  951
+  curl_socket_t sockbunch[MAX_SOCKSPEREASYHANDLE];
  952
+  int bitmap;
  953
+  unsigned int i;
  954
+  unsigned int nfds = extra_nfds;
  955
+  struct pollfd *ufds;
  956
+
  957
+  if(!GOOD_MULTI_HANDLE(multi))
  958
+    return CURLM_BAD_HANDLE;
  959
+
  960
+  /* Count up how many fds we have from the multi handle */
  961
+  easy=multi->easy.next;
  962
+  while(easy != &multi->easy) {
  963
+    bitmap = multi_getsock(easy, sockbunch, MAX_SOCKSPEREASYHANDLE);
  964
+
  965
+    for(i=0; i< MAX_SOCKSPEREASYHANDLE; i++) {
  966
+      curl_socket_t s = CURL_SOCKET_BAD;
  967
+
  968
+      if(bitmap & GETSOCK_READSOCK(i)) {
  969
+        ++nfds;
  970
+        s = sockbunch[i];
  971
+      }
  972
+      if(bitmap & GETSOCK_WRITESOCK(i)) {
  973
+        ++nfds;
  974
+        s = sockbunch[i];
  975
+      }
  976
+      if(s == CURL_SOCKET_BAD) {
  977
+        break;
  978
+      }
  979
+    }
  980
+
  981
+    easy = easy->next; /* check next handle */
  982
+  }
  983
+
  984
+  ufds = (struct pollfd *)malloc(nfds * sizeof(struct pollfd));
  985
+  nfds = 0;
  986
+
  987
+  /* Add the curl handles to our pollfds first */
  988
+  easy=multi->easy.next;
  989
+  while(easy != &multi->easy) {
  990
+    bitmap = multi_getsock(easy, sockbunch, MAX_SOCKSPEREASYHANDLE);
  991
+
  992
+    for(i=0; i< MAX_SOCKSPEREASYHANDLE; i++) {
  993
+      curl_socket_t s = CURL_SOCKET_BAD;
  994
+
  995
+      if(bitmap & GETSOCK_READSOCK(i)) {
  996
+        ufds[nfds].fd = sockbunch[i];
  997
+        ufds[nfds].events = POLLIN;
  998
+        ++nfds;
  999
+        s = sockbunch[i];
  1000
+      }
  1001
+      if(bitmap & GETSOCK_WRITESOCK(i)) {
  1002
+        ufds[nfds].fd = sockbunch[i];
  1003
+        ufds[nfds].events = POLLOUT;
  1004
+        ++nfds;
  1005
+        s = sockbunch[i];
  1006
+      }
  1007
+      if(s == CURL_SOCKET_BAD) {
  1008
+        break;
  1009
+      }
  1010
+    }
  1011
+
  1012
+    easy = easy->next; /* check next handle */
  1013
+  }
  1014
+
  1015
+  /* Add external file descriptions from poll-like struct curl_waitfd */
  1016
+  for(i = 0; i < extra_nfds; i++) {
  1017
+    ufds[nfds].fd = extra_fds[i].fd;
  1018
+    ufds[nfds].events = (short) (
  1019
+      ((extra_fds[i].events & CURL_WAIT_POLLIN)  ? POLLIN  : 0) |
  1020
+      ((extra_fds[i].events & CURL_WAIT_POLLPRI) ? POLLPRI : 0) |
  1021
+      ((extra_fds[i].events & CURL_WAIT_POLLOUT) ? POLLOUT : 0) );
  1022
+    ++nfds;
  1023
+  }
  1024
+
  1025
+  /* wait... */
  1026
+  Curl_poll(ufds, nfds, timeout_ms);
  1027
+  free(ufds);
  1028
+  return CURLM_OK;
  1029
+}
  1030
+
944 1031
 static CURLMcode multi_runsingle(struct Curl_multi *multi,
945 1032
                                  struct timeval now,
946 1033
                                  struct Curl_one_easy *easy)

0 notes on commit de24d7b

Please sign in to comment.
Something went wrong with that request. Please try again.