[patch 2/4] io: Add timeout parameter to io_read()

Simon Horman <[email protected]>
Newsgroups gmane.mail.perdition.user
Message-ID <[email protected]>
io_read-timeout.patch (text/plain, 7.3 KB)
Cc: Christian Balzer <[email protected]>
Signed-off-by: Simon Horman <[email protected]>

Index: perdition/perdition/io.c
===================================================================
--- perdition.orig/perdition/io.c	2009-09-05 15:47:51.000000000 +1000
+++ perdition/perdition/io.c	2009-09-05 15:56:14.000000000 +1000
@@ -290,12 +290,14 @@ err:
  * pre: io: io_t to read from
  *      buf: buffer to read
  *      count: maximum number of bytes to read
+ *      timeout: idle timeout in seconds, 0 for no timeout
  * post: up to count bytes are read from io into buf
  * return: Number of bytes read
  *         -1 on error
  **********************************************************************/
 
-ssize_t io_read(io_t *io, void *buf, size_t count){
+ssize_t __io_read(io_t *io, void *buf, size_t count)
+{
   ssize_t bytes=0;
 
   switch(io->type){
@@ -328,6 +330,98 @@ err:
 }
 
 
+ssize_t io_read(io_t *io, void *buf, size_t count, long timeout)
+{
+	io_select_t *s = NULL;
+	struct timeval tv;
+	fd_set except_template;
+	fd_set read_template;
+	int bytes_read;
+	int status;
+	int return_status = -1;
+        int rfd;
+
+        io->err = io_err_none;
+
+        rfd = io_get_rfd(io);
+        if (rfd < 0) {
+		VANESSA_LOGGER_DEBUG("io_get_rfd");
+		goto err;
+	}
+
+	s = io_select_create();
+	if (s == NULL) {
+		VANESSA_LOGGER_DEBUG("io_select_create");
+		goto err;
+	}
+
+	if (!io_select_add(s, io)) {
+		VANESSA_LOGGER_DEBUG("io_select_add");
+		goto err_free;
+	}
+
+	while (1) {
+		FD_ZERO(&read_template);
+		FD_SET(rfd, &read_template);
+		FD_ZERO(&except_template);
+		FD_SET(rfd, &except_template);
+		tv.tv_sec = timeout;
+		tv.tv_usec = 0;
+
+		status = io_select(FD_SETSIZE, &read_template, NULL,
+                                   &except_template, timeout ? &tv : NULL, s);
+		if (status < 0) {
+			if (errno != EINTR) {
+				VANESSA_LOGGER_DEBUG_ERRNO("select");
+	                        goto err_free;
+			}
+			continue;	/* Ignore EINTR */
+		}
+		else if (FD_ISSET(rfd, &except_template)){
+			VANESSA_LOGGER_DEBUG("error on file descriptor");
+			goto err_free;
+		}
+		else if (!status) {
+			VANESSA_LOGGER_DEBUG("idle timeout");
+                        io->err = io_err_timeout;
+                        goto err_free;
+		}
+
+		/* If we get this far fd must be ready for reading */
+		bytes_read = __io_read(io, buf, count);
+                if (bytes_read < 0) {
+			if (errno != EINTR) {
+			        VANESSA_LOGGER_DEBUG_ERRNO("error reading "
+                                                           "input");
+	                        goto err_free;
+			}
+			continue;	/* Ignore EINTR */
+		}
+
+                break;
+	}
+
+        return_status = bytes_read;
+err_free:
+        io_select_destroy(s);
+err:
+        if (return_status < 0 && io->err == io_err_none)
+                io->err = io_err_other;
+        return return_status;
+}
+
+
+/**********************************************************************
+ * io_get_err
+ * Get error status of an io
+ * pre: io: io_t to get the err of
+ * post: none
+ * return: error status of the io
+ **********************************************************************/
+
+void io_set_err(io_t *io, enum io_err err);
+
+
 /**********************************************************************
  * io_get_rfd
  * Get the file descriptor that is being used for reading
@@ -549,7 +643,7 @@ static int __io_pipe_read(int fd, void *
 
   io->err = io_err_none;
 
-  bytes = io_read(io, buf, count);
+  bytes = __io_read(io, buf, count);
 
   if(opt.connection_logging && bytes > 0) {
     char *dump_str;
Index: perdition/perdition/io.h
===================================================================
--- perdition.orig/perdition/io.h	2009-09-05 15:47:51.000000000 +1000
+++ perdition/perdition/io.h	2009-09-05 15:56:04.000000000 +1000
@@ -117,12 +117,13 @@ ssize_t io_write(io_t *io, const void *b
  * pre: io: io_t to read from
  *      buf: buffer to read
  *      count: maximum number of bytes to read
+ *      timeout: idle timeout in seconds, 0 for no timeout
  * post: up to count bytes are read from io into buf
  * return: Number of bytes read
  *         -1 on error
  **********************************************************************/
 
-ssize_t io_read(io_t *io, void *buf, size_t count);
+ssize_t io_read(io_t *io, void *buf, size_t count, long timeout);
 
 
 /**********************************************************************
Index: perdition/perdition/token.c
===================================================================
--- perdition.orig/perdition/token.c	2009-09-05 15:47:51.000000000 +1000
+++ perdition/perdition/token.c	2009-09-05 15:56:04.000000000 +1000
@@ -215,88 +215,30 @@ static int token_fill_buffer(io_t *io, c
 
 static int __token_fill_buffer(io_t *io, const options_t *opt,
 		const char *log_str){
-  io_select_t *s = NULL;
-  struct timeval timeout;
-  fd_set except_template;
-  fd_set read_template;
-  int bytes_read;
-  int status;
-  int return_status = -1;
-  int fd;
-
-  if((fd=io_get_rfd(io))<0){
-    VANESSA_LOGGER_DEBUG_UNSAFE("io_get_rfd %d", fd);
-    return(-1);
-  }
-
-  s = io_select_create();
-  if(s == NULL) {
-	  VANESSA_LOGGER_DEBUG("io_select_create");
-	  goto leave;
-  }
-  
-  if(io_select_add(s, io) == NULL) {
-	  VANESSA_LOGGER_DEBUG("io_select_add");
-	  goto leave;
-  }
-
-  while(1){
-    FD_ZERO(&read_template);
-    FD_SET(fd, &read_template);
-    FD_ZERO(&except_template);
-    FD_SET(fd, &except_template);
-    timeout.tv_sec=opt->timeout;
-    timeout.tv_usec=0;
-
-    status=io_select(
-      FD_SETSIZE, 
-      &read_template, 
-      NULL, 
-      &except_template,
-      opt->timeout?&timeout:NULL,
-      s
-    );
-    if(status<0){
-      if(errno!=EINTR){
-        VANESSA_LOGGER_DEBUG_ERRNO("select");
-	goto leave;
-      }
-      continue;  /* Ignore EINTR */
-    }
-    else if(FD_ISSET(fd, &except_template)){
-      VANESSA_LOGGER_DEBUG("error on file descriptor");
-      goto leave;
-    }
-    else if(status==0){
-      VANESSA_LOGGER_DEBUG("idle timeout");
-      goto leave;
-    }
+	int bytes_read;
+	char *dump_buf;
 
-    /*If we get this far fd must be ready for reading*/
-    if((bytes_read=io_read(
-      io, 
-      token_read_buffer, 
-      MAX_LINE_LENGTH-1
-    ))<0){
-      VANESSA_LOGGER_DEBUG_ERRNO("error reading input");
-      goto leave;
-    }
+	bytes_read = io_read(io, token_read_buffer, MAX_LINE_LENGTH-1,
+			     opt->timeout);
+	if (bytes_read < 0) {
+		VANESSA_LOGGER_DEBUG_ERRNO("error reading input");
+		return -1;
+	}
 
-    token_read_offset=0;
-    token_read_bytes=bytes_read;
-    if(opt->connection_logging){
-      char *dump_buf;
-      dump_buf = VANESSA_LOGGER_DUMP(token_read_buffer, bytes_read, 0);
-      VANESSA_LOGGER_DEBUG_RAW_UNSAFE("%s \"%s\"", log_str, dump_buf);
-      free(dump_buf);
-    }
-    break;
-  }
+	token_read_offset = 0;
+	token_read_bytes = bytes_read;
+	if (opt->connection_logging) {
+		dump_buf = VANESSA_LOGGER_DUMP(token_read_buffer,
+					       bytes_read, 0);
+		if (!dump_buf) {
+			VANESSA_LOGGER_DEBUG("VANESSA_LOGGER_DUMP");
+			return -1;
+		}
+		VANESSA_LOGGER_DEBUG_RAW_UNSAFE("%s \"%s\"", log_str, dump_buf);
+		free(dump_buf);
+	}
 
-  return_status = bytes_read;
-leave:
-  io_select_destroy(s);
-  return(return_status);
+	return bytes_read;
 }
 
 

______________________________________________
Perdition-users mailing list
[email protected]
http://lists.vergenet.net/listinfo/perdition-users
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.