]> granicus.if.org Git - mutt/commitdiff
Add timeout parameter to mutt_socket_poll.
authorKevin McCarthy <kevin@8t8.us>
Sun, 23 Jul 2017 02:48:49 +0000 (19:48 -0700)
committerKevin McCarthy <kevin@8t8.us>
Sun, 23 Jul 2017 02:48:49 +0000 (19:48 -0700)
This will be used in the next commit to add a timeout when polling for
new mail.

imap/imap.c
mutt_sasl.c
mutt_sasl.h
mutt_socket.c
mutt_socket.h
mutt_tunnel.c

index 387801e60fd194eccff168ca3c9fc5be295da8a1..0260a730e138de1cd7e1372bbabd84c00df7565a 100644 (file)
@@ -1459,7 +1459,7 @@ int imap_check_mailbox (CONTEXT *ctx, int *index_hint, int force)
   }
   if (idata->state == IMAP_IDLE)
   {
-    while ((result = mutt_socket_poll (idata->conn)) > 0)
+    while ((result = mutt_socket_poll (idata->conn, 0)) > 0)
     {
       if (imap_cmd_step (idata) != IMAP_CMD_CONTINUE)
       {
index d99ba72601356748f6bd27ab6727f7992c194821..f0760acf1d07d099235ed9406b087f9f15b88e78 100644 (file)
@@ -101,7 +101,7 @@ static int mutt_sasl_conn_close (CONNECTION* conn);
 static int mutt_sasl_conn_read (CONNECTION* conn, char* buf, size_t len);
 static int mutt_sasl_conn_write (CONNECTION* conn, const char* buf,
   size_t count);
-static int mutt_sasl_conn_poll (CONNECTION* conn);
+static int mutt_sasl_conn_poll (CONNECTION* conn, time_t wait_secs);
 
 /* utility function, stolen from sasl2 sample code */
 static int iptostring(const struct sockaddr *addr, socklen_t addrlen,
@@ -613,13 +613,13 @@ static int mutt_sasl_conn_write (CONNECTION* conn, const char* buf,
   return -1;
 }
 
-static int mutt_sasl_conn_poll (CONNECTION* conn)
+static int mutt_sasl_conn_poll (CONNECTION* conn, time_t wait_secs)
 {
   SASL_DATA* sasldata = conn->sockdata;
   int rc;
 
   conn->sockdata = sasldata->sockdata;
-  rc = sasldata->msasl_poll (conn);
+  rc = sasldata->msasl_poll (conn, wait_secs);
   conn->sockdata = sasldata;
 
   return rc;
index 278117524ddff8acdf950451a6a145fa843f0255..f1a2c9be19c181019491b9f7584f63db70685140 100644 (file)
@@ -48,7 +48,7 @@ typedef struct
   int (*msasl_close) (CONNECTION* conn);
   int (*msasl_read) (CONNECTION* conn, char* buf, size_t len);
   int (*msasl_write) (CONNECTION* conn, const char* buf, size_t count);
-  int (*msasl_poll) (CONNECTION* conn);
+  int (*msasl_poll) (CONNECTION* conn, time_t wait_secs);
 }
 SASL_DATA;
 
index 08f21533b0232444af3afb45ec1ce98d8b26a9d3..d4b22973e4f14a96971fd5ccfd1592a78c7da6e9 100644 (file)
@@ -153,13 +153,13 @@ int mutt_socket_write_d (CONNECTION *conn, const char *buf, int len, int dbg)
  *   Returns: >0 if there is data to read,
  *            0 if a read would block,
  *            -1 if this connection doesn't support polling */
-int mutt_socket_poll (CONNECTION* conn)
+int mutt_socket_poll (CONNECTION* conn, time_t wait_secs)
 {
   if (conn->bufpos < conn->available)
     return conn->available - conn->bufpos;
 
   if (conn->conn_poll)
-    return conn->conn_poll (conn);
+    return conn->conn_poll (conn, wait_secs);
 
   return -1;
 }
@@ -431,18 +431,41 @@ int raw_socket_write (CONNECTION* conn, const char* buf, size_t count)
   return rc;
 }
 
-int raw_socket_poll (CONNECTION* conn)
+int raw_socket_poll (CONNECTION* conn, time_t wait_secs)
 {
   fd_set rfds;
-  struct timeval tv = { 0, 0 };
+  struct timeval tv;
+  struct timespec pre_t, post_t;
+  time_t sleep_secs;
+  int rv;
 
   if (conn->fd < 0)
     return -1;
 
-  FD_ZERO (&rfds);
-  FD_SET (conn->fd, &rfds);
-  
-  return select (conn->fd + 1, &rfds, NULL, NULL, &tv);
+  FOREVER
+  {
+    tv.tv_sec = wait_secs;
+    tv.tv_usec = 0;
+
+    FD_ZERO (&rfds);
+    FD_SET (conn->fd, &rfds);
+
+    clock_gettime (CLOCK_MONOTONIC, &pre_t);
+    rv = select (conn->fd + 1, &rfds, NULL, NULL, &tv);
+    clock_gettime (CLOCK_MONOTONIC, &post_t);
+
+    if (rv > 0 ||
+        (rv < 0 && errno != EINTR))
+      return rv;
+
+    if (SigInt)
+      mutt_query_exit ();
+
+    sleep_secs = post_t.tv_sec - pre_t.tv_sec;
+    if (wait_secs <= sleep_secs)
+      return 0;
+    wait_secs -= sleep_secs;
+  }
 }
 
 int raw_socket_open (CONNECTION* conn)
index b8c535ca711d9234d37af41e6ff6e9f5781633ab..e69b71b6b29f60a6556a9b62d364a33506053d7a 100644 (file)
@@ -48,13 +48,13 @@ typedef struct _connection
   int (*conn_write) (struct _connection *conn, const char *buf, size_t count);
   int (*conn_open) (struct _connection *conn);
   int (*conn_close) (struct _connection *conn);
-  int (*conn_poll) (struct _connection *conn);
+  int (*conn_poll) (struct _connection *conn, time_t wait_secs);
 } CONNECTION;
 
 int mutt_socket_open (CONNECTION* conn);
 int mutt_socket_close (CONNECTION* conn);
 int mutt_socket_read (CONNECTION* conn, char* buf, size_t len);
-int mutt_socket_poll (CONNECTION* conn);
+int mutt_socket_poll (CONNECTION* conn, time_t wait_secs);
 int mutt_socket_readchar (CONNECTION *conn, char *c);
 #define mutt_socket_readln(A,B,C) mutt_socket_readln_d(A,B,C,MUTT_SOCK_LOG_CMD)
 int mutt_socket_readln_d (char *buf, size_t buflen, CONNECTION *conn, int dbg);
@@ -71,6 +71,6 @@ int raw_socket_read (CONNECTION* conn, char* buf, size_t len);
 int raw_socket_write (CONNECTION* conn, const char* buf, size_t count);
 int raw_socket_open (CONNECTION *conn);
 int raw_socket_close (CONNECTION *conn);
-int raw_socket_poll (CONNECTION* conn);
+int raw_socket_poll (CONNECTION* conn, time_t wait_secs);
 
 #endif /* _MUTT_SOCKET_H_ */
index c1b5a346e1445de5f3cbd20ce0b3600c529e531e..e199e4d67072c6f8839ce8f8dcab392b7fbe952a 100644 (file)
@@ -45,7 +45,7 @@ static int tunnel_socket_open (CONNECTION*);
 static int tunnel_socket_close (CONNECTION*);
 static int tunnel_socket_read (CONNECTION* conn, char* buf, size_t len);
 static int tunnel_socket_write (CONNECTION* conn, const char* buf, size_t len);
-static int tunnel_socket_poll (CONNECTION* conn);
+static int tunnel_socket_poll (CONNECTION* conn, time_t wait_secs);
 
 /* -- public functions -- */
 int mutt_tunnel_socket_setup (CONNECTION *conn)
@@ -188,7 +188,7 @@ static int tunnel_socket_write (CONNECTION* conn, const char* buf, size_t len)
   return rc;
 }
 
-static int tunnel_socket_poll (CONNECTION* conn)
+static int tunnel_socket_poll (CONNECTION* conn, time_t wait_secs)
 {
   TUNNEL_DATA* tunnel = (TUNNEL_DATA*) conn->sockdata;
   int ofd;
@@ -196,7 +196,7 @@ static int tunnel_socket_poll (CONNECTION* conn)
 
   ofd = conn->fd;
   conn->fd = tunnel->readfd;
-  rc = raw_socket_poll (conn);
+  rc = raw_socket_poll (conn, wait_secs);
   conn->fd = ofd;
 
   return rc;