chiark / gitweb /
adns_pollfds and struct pollfd support - on branch, because it's difficult
authorian <ian>
Sun, 11 Apr 1999 18:13:28 +0000 (18:13 +0000)
committerian <ian>
Sun, 11 Apr 1999 18:13:28 +0000 (18:13 +0000)
and I can't be bothered to finish and test it now.

src/adns.h
src/event.c
src/internal.h

index a234cda45951c4770bb0355704939b1a0f6d0972..d3ab7eda9e9b92eb7dccbeedda306dc356a9c649 100644 (file)
@@ -303,6 +303,35 @@ void adns_interest(adns_state, int *maxfd_io, fd_set *readfds_io,
  * readfds, writefds, exceptfds and maxfd may not be 0.
  */
 
+struct pollfd *adns_pollfds(adns_state, struct pollfd *buf, int *len_io, int *timeout);
+/* Like adns_interest, but for use with poll(2).  The return value
+ * will be a pointer to an array of struct pollfd, one for each of
+ * adns's fds, and *len_io will be set to the number of file
+ * descriptors.
+ *
+ * Each descriptor's entry will have a revents==0, and events will be
+ * a combination of one or more of POLLIN, POLLOUT and POLLERR.
+ *
+ * *timeout will be adjusted to ensure that it is no more than the
+ * desired timeout (with the usual rule for poll that <0 stands for
+ * infinity).  *timeout may be 0 in which case no timeout information
+ * will be returned.
+ *
+ * If *len_io is nonnegative then adns_pollfds will write to buffer
+ * provided by the caller in buf.  If it is too small then *len_io
+ * will be updated to say how large it would have needed to be and
+ * adns_pollfds will fail with ENOSPC.
+ *
+ * If *len_io is -1 then buf should be 0, and adns_pollfds will use some
+ * memory belonging to the adns_state, which may be overwritten on subsequent
+ * calls.
+ *
+ * When you come out of poll(2), you should probably call adns_callback
+ * with maxfd==-1.
+ *
+ * On systems without poll(2) this function will return ENOSYS.
+ */
+
 /* Example expected/legal calling sequences:
  *  adns_init
  *  adns_submit 1
index 5041f8ce02bc6cf23e69b52c214de682869330b1..983e266949ef600677db2021f3a822571a206cfc 100644 (file)
@@ -167,7 +167,79 @@ static void checktimeouts(adns_state ads, struct timeval now,
       inter_maxtoabs(tv_io,tvbuf,now,qu->timeout);
     }
   }
-}  
+}
+
+static void checkfds(adns_state ads, int *maxfd,
+                    fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
+                    int avail, struct pollfd *buf, int *count_r) {
+  int count;
+
+  count= 0;
+  inter_addfd(ads->udpsocket,maxfd,readfds);
+  poll_addfd(ads->udpsocket,avail,buf,&count,POLLIN);
+
+  switch (ads->tcpstate) {
+  case server_disconnected:
+    break;
+  case server_connecting:
+    inter_addfd(ads->tcpsocket,maxfd,writefds);
+    poll_addfd(ads->tcpsocket,avail,buf,&count,POLLOUT);
+    break;
+  case server_ok:
+    inter_addfd(ads->tcpsocket,maxfd,readfds);
+    inter_addfd(ads->tcpsocket,maxfd,exceptfds);
+    if (ads->tcpsend.used) inter_addfd(ads->tcpsocket,maxfd,writefds);
+    poll_addfd(ads->tcpsocket,avail,buf,&count,
+              ads->tcpsend.used ? POLLIN|POLLOUT : POLLIN);
+    break;
+  default:
+    abort();
+  }
+}
+
+struct pollfd *adns_pollfds(adns_state ads, struct pollfd *buf,
+                           int *len_io, int *timeout_io) {
+  struct timeval now, tvbuf, *tvp;
+  int timeout;
+  
+  r= gettimeofday(&now,0);
+  if (r) return 0;
+
+  timeout= *timeout_io;
+  if (timeout < 0) {
+    tvtop= 0;
+  } else {
+    tvbuf.tv_sec= now.tv_sec + (timeout/1000);
+    tvbuf.tv_usec= now.tv_usec + (timeout%1000)*1000;
+    if (tvbuf.tv_sec >= 1000000) {
+      tvbuf.tv_sec += 1;
+      tvbuf.tv_usec -= 1000000;
+    }
+    tvp= &tvbuf;
+  }
+  checktimouts(ads,now,&tvp,&tvbuf);
+
+  if (tvp) {
+    assert(tvbuf.tv_sec<INT_MAX/1000);
+    *timeout_io= (tvp->tv_sec - now.tv_sec)*1000 + (tvp->tv_usec - now.tv_usec)/1000;
+  } else {
+    *timeout_io= -1;
+  }
+
+  avail= *len_io;
+  if (avail == -1) {
+    buf= ads->pollfdsbuf;
+    avail= 2;
+    if (!buf) {
+      buf= ads->pollfdsbuf= malloc(sizeof(struct pollfd)*avail);
+      if (!buf) return 0;
+    }
+  }
+  checkfds(ads, 0,0,0,0, avail,buf,len_io);
+  if (*len_io > avail) { errno= ENOSPC; return 0; }
+
+  return buf;
+}
  
 void adns_interest(adns_state ads, int *maxfd,
                   fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
@@ -187,23 +259,8 @@ void adns_interest(adns_state ads, int *maxfd,
   } else {
     checktimeouts(ads,now,tv_io,tvbuf);
   }
-  
-  inter_addfd(maxfd,readfds,ads->udpsocket);
 
-  switch (ads->tcpstate) {
-  case server_disconnected:
-    break;
-  case server_connecting:
-    inter_addfd(maxfd,writefds,ads->tcpsocket);
-    break;
-  case server_ok:
-    inter_addfd(maxfd,readfds,ads->tcpsocket);
-    inter_addfd(maxfd,exceptfds,ads->tcpsocket);
-    if (ads->tcpsend.used) inter_addfd(maxfd,writefds,ads->tcpsocket);
-    break;
-  default:
-    abort();
-  }
+  checkfds(ads, maxfd,readfds,writefds,exceptfds, 0,0,0);
 }
 
 /* Callback procedures - these do the real work of reception and timeout, etc. */
index 5e59a12877b066eb47a199a2aae8f491bc52c7a3..94b8b20a8efe01be83584006d2ac19dbe8564cd3 100644 (file)
@@ -249,6 +249,7 @@ struct adns__state {
   int nservers, nsortlist, tcpserver;
   enum adns__tcpstate { server_disconnected, server_connecting, server_ok } tcpstate;
   struct timeval tcptimeout;
+  struct pollfd *pollfdsbuf; /* fixme: init and cleanup */
   struct server {
     struct in_addr addr;
   } servers[MAXSERVERS];