[ds6-devel]
nc6/src io_stream.c,1.15,1.16 io_stream.h,1.9,1.10 readwrite.c,1.27,1.28
chris at deepspace6.net
chris@deepspace6.net
Fri Jan 3 17:07:05 CET 2003
Update of /cvs/nc6/src
Modified Files:
io_stream.c io_stream.h readwrite.c
Log Message:
Moved much of the handling of logging and read/write results into the
io_stream. This fixes up the logging I broke before, and simplifies the
readwrite loops further.
Mauro: review this and give me your thoughts...
Index: io_stream.c
===================================================================
RCS file: /cvs/nc6/src/io_stream.c,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -d -r1.15 -r1.16
--- io_stream.c 3 Jan 2003 14:38:27 -0000 1.15
+++ io_stream.c 3 Jan 2003 17:07:02 -0000 1.16
@@ -22,6 +22,7 @@
#include "config.h"
#include "io_stream.h"
#include "misc.h"
+#include "parser.h"
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
@@ -154,6 +155,13 @@
if (tv->tv_sec < 0) {
/* timeout has expired */
timerclear(tv);
+#ifndef NDEBUG
+ if (is_flag_set(VERY_VERBOSE_MODE) == TRUE)
+ warn("%s hold timed out", ios->name);
+ } else if (is_flag_set(VERY_VERBOSE_MODE) == TRUE) {
+ warn("%s timer expires in %d.%06d",
+ ios->name, tv->tv_sec, tv->tv_usec);
+#endif
}
return tv;
@@ -177,10 +185,33 @@
else
rr = cb_read(ios->buf_in, ios->fd_in, 0);
- if (rr > 0)
+ if (rr > 0) {
ios->rcvd += rr;
-
- return rr;
+#ifndef NDEBUG
+ if (is_flag_set(VERY_VERBOSE_MODE) == TRUE)
+ warn("read %d bytes from %s", rr, ios->name);
+#endif
+ return rr;
+ } else if (rr == 0) {
+ /* read eof - close read stream */
+#ifndef NDEBUG
+ if (is_flag_set(VERY_VERBOSE_MODE) == TRUE)
+ warn("read eof from %s", ios->name);
+#endif
+ ios_shutdown(ios, SHUT_RD);
+ return IOS_EOF;
+ } else if (errno == EAGAIN) {
+ /* not ready? */
+ return 0;
+ } else {
+ /* weird error */
+#ifndef NDEBUG
+ if (is_flag_set(VERY_VERBOSE_MODE) == TRUE)
+ warn("error reading from %s: %s",
+ ios->name, strerror(errno));
+#endif
+ return IOS_FAILED;
+ }
}
@@ -203,13 +234,32 @@
if (rr > 0) {
ios->sent += rr;
-
+#ifndef NDEBUG
+ if (is_flag_set(VERY_VERBOSE_MODE) == TRUE)
+ warn("wrote %d bytes to %s", rr, ios->name);
+#endif
/* shutdown the write if buf_out is empty and out_eof is set */
if (ios->out_eof && cb_is_empty(ios->buf_out))
ios_shutdown(ios, SHUT_WR);
+ return rr;
+ } else if (rr == 0) {
+ /* shouldn't happen? */
+ return 0;
+ } else if (errno == EAGAIN) {
+ /* not ready? */
+ return 0;
+ } else {
+#ifndef NDEBUG
+ if (is_flag_set(VERY_VERBOSE_MODE) == TRUE) {
+ if (errno == EPIPE)
+ warn("received SIGPIPE on %s", ios->name);
+ else
+ warn("error writing to %s: %s",
+ ios->name, strerror(errno));
+ }
+#endif
+ return IOS_FAILED;
}
-
- return rr;
[...89 lines suppressed...]
Index: io_stream.h
===================================================================
RCS file: /cvs/nc6/src/io_stream.h,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- io_stream.h 3 Jan 2003 14:38:27 -0000 1.9
+++ io_stream.h 3 Jan 2003 17:07:02 -0000 1.10
@@ -84,11 +84,17 @@
/* read into the input buffer.
- * should only be called if ios_schedule_read returned a true value */
+ * should only be called if ios_schedule_read returned a true value.
+ * returns the total bytes read, or a negative error code */
ssize_t ios_read(io_stream *ios);
/* write from the output buffer.
- * should only be called if ios_schedule_write returned a true value */
+ * should only be called if ios_schedule_write returned a true value
+ * returns the total bytes read, or a negative error code */
ssize_t ios_write(io_stream *ios);
+
+/* error return values from ios_read/ios_write */
+#define IOS_FAILED -1
+#define IOS_EOF -2
/* signal that no more data will be added to the output buffer. Once the
Index: readwrite.c
===================================================================
RCS file: /cvs/nc6/src/readwrite.c,v
retrieving revision 1.27
retrieving revision 1.28
diff -u -d -r1.27 -r1.28
--- readwrite.c 3 Jan 2003 14:38:27 -0000 1.27
+++ readwrite.c 3 Jan 2003 17:07:02 -0000 1.28
@@ -48,9 +48,6 @@
struct timeval tv1, tv2;
struct timeval *tvp1, *tvp2, *tvp;
int retval = 0;
-#ifndef NDEBUG
- bool very_verbose_mode = is_flag_set(VERY_VERBOSE_MODE);
-#endif
/* check function arguments */
assert(ios1 != NULL);
@@ -108,17 +105,6 @@
tvp1 = ios_next_timeout(ios1, &tv1);
tvp2 = ios_next_timeout(ios2, &tv2);
-#ifndef NDEBUG
- if (very_verbose_mode == TRUE) {
- if (tvp1 != NULL)
- warn("%s timer expires in %d.%06d",
- ios_name(ios1), tv1.tv_sec, tv1.tv_usec);
- if (tvp2 != NULL)
- warn("%s timer expires in %d.%06d",
- ios_name(ios2), tv2.tv_sec, tv2.tv_usec);
- }
-#endif
-
/* stop loop if either ios has timed out */
if ((tvp1 != NULL && istimerexpired(tvp1) == TRUE) ||
(tvp2 != NULL && istimerexpired(tvp2) == TRUE)) {
@@ -149,26 +135,15 @@
/* ios1 is ready to read */
rr = ios_read(ios1);
- if (rr > 0) {
-#ifndef NDEBUG
- if (very_verbose_mode == TRUE)
- warn("read %d bytes from %s",
- rr, ios_name(ios1));
-#endif
- } else if (rr == 0) {
-#ifndef NDEBUG
- if (very_verbose_mode == TRUE) {
- warn("read eof from %s",
- ios_name(ios1));
+ if (rr < 0) {
+ if (rr == IOS_EOF)
+ ios_write_eof(ios2);
+ else {
+ /* something bad happened -
+ * exit the main loop */
+ retval = -1;
+ break;
}
-#endif
- ios_shutdown(ios1, SHUT_RD);
- ios_write_eof(ios2);
- } else if (rr < 0 && errno != EAGAIN) {
- /* error while reading ios1:
- * print an error message and exit. */
- fatal("error reading from %s: %s",
- ios_name(ios1), strerror(errno));
}
}
@@ -176,26 +151,15 @@
/* ios2 is ready to read */
rr = ios_read(ios2);
- if (rr > 0) {
-#ifndef NDEBUG
- if (very_verbose_mode == TRUE)
- warn("read %d bytes from %s",
- rr, ios_name(ios2));
-#endif
- } else if (rr == 0) {
-#ifndef NDEBUG
- if (very_verbose_mode == TRUE) {
- warn("read eof from %s",
- ios_name(ios2));
+ if (rr < 0) {
+ if (rr == IOS_EOF)
+ ios_write_eof(ios1);
+ else {
+ /* something bad happened -
+ * exit the main loop */
+ retval = -1;
+ break;
}
-#endif
- ios_shutdown(ios2, SHUT_RD);
- ios_write_eof(ios1);
- } else if (rr < 0 && errno != EAGAIN) {
- /* error while reading ios2:
[...82 lines suppressed...]
More information about the ds6-devel
mailing list