]> git.pond.sub.org Git - empserver/blobdiff - src/lib/gen/io.c
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / src / lib / gen / io.c
index f7efadc710adc99df58880ddcf8244037b6da1f4..8a3d820e21c801af23add160d7339f378452339a 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -19,9 +19,9 @@
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
  *
  *  ---
  *
@@ -39,6 +39,8 @@
  * reading or writing when appropriate.
  */
 
+#include <config.h>
+
 #include <errno.h>
 #include <sys/types.h>
 #if !defined(_WIN32)
@@ -62,6 +64,7 @@
 #include "ioqueue.h"
 #include "empio.h"
 #include "gen.h"               /* getfdtablesize */
+#include "server.h"
 
 #include "empthread.h"
 
@@ -102,7 +105,7 @@ io_open(int fd, int flags, int bufsize, int (*notify)(void),
     if ((flags & IO_WRITE) && (flags & IO_NEWSOCK) == 0)
        iop->output = ioq_create(bufsize);
     if (flags & IO_NBLOCK)
-       io_noblocking(iop, 1);
+       io_noblocking(iop, 1);  /* FIXME check success */
     iop->flags = flags;
     iop->assoc = assoc;
     iop->notify = notify;
@@ -146,7 +149,7 @@ io_input(struct iop *iop, int waitforinput)
     cc = read(iop->fd, buf, sizeof(buf));
     if (cc < 0) {
        /* would block, so nothing to read. */
-       if (errno == EWOULDBLOCK)
+       if (errno == EAGAIN || errno == EWOULDBLOCK)
            return 0;
 
        /* Some form of file error occurred... */
@@ -214,10 +217,6 @@ io_output(struct iop *iop, int waitforoutput)
     if (iop->flags & IO_ERROR)
        return -1;
 
-    /* This is the same test as io_outputwaiting.... */
-    if (ioq_qsize(iop->output) == 0)
-       return 0;
-
 #if !defined(_WIN32)
     /* make the iov point to the data in the queue. */
     /* I.E., each of the elements in the queue. */
@@ -246,7 +245,7 @@ io_output(struct iop *iop, int waitforoutput)
     /* if it failed.... */
     if (cc < 0) {
        /* Hmm, it would block.  file is opened noblock, soooooo.. */
-       if (errno == EWOULDBLOCK) {
+       if (errno == EAGAIN || errno == EWOULDBLOCK) {
            /* If there are remaining bytes, set the IO as remaining.. */
            remain = ioq_qsize(iop->output);
            return remain;
@@ -271,19 +270,11 @@ io_output(struct iop *iop, int waitforoutput)
     }
 #endif
 
-
     /* If no bytes were written, something happened..  Like an EOF. */
-#ifndef        hpux
     if (cc == 0) {
        iop->flags |= IO_EOF;
        return 0;
     }
-#else
-    if (cc == 0) {
-       remain = ioq_qsize(iop->output);
-       return remain;
-    }
-#endif /* hpux */
 
     /* Remove the number of written bytes from the queue. */
     ioq_dequeue(iop->output, cc);
@@ -327,7 +318,7 @@ io_write(struct iop *iop, s_char *buf, int nbytes, int doWait)
        } else {
            /* only try a write every BUFSIZE characters */
            if (((len - nbytes) % iop->bufsize) < (len % iop->bufsize))
-               io_output(iop, 0);
+               io_output(iop, IO_NOWAIT);
        }
     }
     return nbytes;
@@ -338,7 +329,11 @@ io_output_all(struct iop *iop)
 {
     int n;
 
-    while ((n = io_output(iop, IO_NOWAIT)) > 0) {
+    /*
+     * Mustn't block a player thread while update is pending, or else
+     * a malicous player could delay the update indefinitely
+     */
+    while (((n = io_output(iop, IO_NOWAIT)) > 0) && !update_pending) {
        empth_select(iop->fd, EMPTH_FD_WRITE);
     }
     return n;
@@ -387,9 +382,9 @@ io_noblocking(struct iop *iop, int value)
     if (flags < 0)
        return -1;
     if (value == 0)
-       flags &= ~FNDELAY;
+       flags &= ~O_NONBLOCK;
     else
-       flags |= FNDELAY;
+       flags |= O_NONBLOCK;
     if (fcntl(iop->fd, F_SETFL, flags) < 0)
        return -1;
 #else
@@ -403,22 +398,16 @@ io_noblocking(struct iop *iop, int value)
     return 0;
 }
 
-int
-io_conn(struct iop *iop)
-{
-    return (iop->flags & IO_CONN);
-}
-
 int
 io_error(struct iop *iop)
 {
-    return (iop->flags & IO_ERROR);
+    return iop->flags & IO_ERROR;
 }
 
 int
 io_eof(struct iop *iop)
 {
-    return (iop->flags & IO_EOF);
+    return iop->flags & IO_EOF;
 }
 
 int