]> git.pond.sub.org Git - empserver/blobdiff - src/lib/gen/io.c
Get rid of src/lib/gen/copy.c
[empserver] / src / lib / gen / io.c
index 00f971f1216fbc465bae808abb98896f7b5ef145..4c42a55efedb0c6137dff4b4709cc8b53823f75b 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2008, 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.
  *
  *  ---
  *
  * reading or writing when appropriate.
  */
 
+#include <config.h>
+
 #include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <sys/socket.h>
 #include <sys/types.h>
-#if !defined(_WIN32)
 #include <sys/uio.h>
-#include <sys/file.h>
-#include <unistd.h>            /* close read shutdown select */
-#include <sys/socket.h>
-#endif
-#include <time.h>
-#include <fcntl.h>
-#include <stdlib.h>            /* malloc calloc free */
-
-#if defined(_WIN32)
-#include <winsock.h>
-#endif
-
-#include "misc.h"
-#include "queue.h"
-#include "ioqueue.h"
-#include "io_mask.h"
+#include <unistd.h>
 #include "empio.h"
-#include "gen.h"               /* getfdtablesize */
-
 #include "empthread.h"
-
-extern struct player *player;  /* XXX */
-
-static struct iop **io_list;
-static struct io_mask *iom;
-static int fdmax;              /* largest file descriptor seen */
-static fd_set newoutput;
+#include "ioqueue.h"
+#include "misc.h"
+#include "queue.h"
+#include "server.h"
 
 struct iop {
     int fd;
     struct ioqueue *input;
     struct ioqueue *output;
     int flags;
-    s_char *assoc;
     int bufsize;
-    int (*notify) ();
 };
 
 void
 io_init(void)
 {
-    iom = iom_create(IO_READ | IO_WRITE);
-    io_list = (struct iop **)calloc(getfdtablesize(), sizeof(*io_list));
-    fdmax = 0;
-    FD_ZERO(&newoutput);
 }
 
 struct iop *
-io_open(int fd, int flags, int bufsize, int (*notify) (void),
-       s_char *assoc)
+io_open(int fd, int flags, int bufsize)
 {
     struct iop *iop;
 
-    if (io_list[fd] != 0) {
-       /* already exists */
-       return 0;
-    }
     flags = flags & (IO_READ | IO_WRITE | IO_NBLOCK | IO_NEWSOCK);
     if ((flags & (IO_READ | IO_WRITE)) == 0)
-       return 0;
-    iop = (struct iop *)malloc(sizeof(struct iop));
+       return NULL;
+    iop = malloc(sizeof(struct iop));
+    if (!iop)
+       return NULL;
     iop->fd = fd;
     iop->input = 0;
     iop->output = 0;
@@ -114,13 +89,8 @@ 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;
-    io_list[fd] = iop;
-    iom_set(iom, flags, fd);
-    if (fd > fdmax) fdmax = fd;
     return iop;
 }
 
@@ -132,21 +102,14 @@ io_close(struct iop *iop)
        ioq_destroy(iop->input);
     if (iop->output != 0)
        ioq_destroy(iop->output);
-    iom_clear(iom, iop->flags, iop->fd);
-    FD_CLR(iop->fd, &newoutput);
-    io_list[iop->fd] = 0;
-#if !defined(_WIN32)
     (void)close(iop->fd);
-#else
-    closesocket(iop->fd);
-#endif
-    free((s_char *)iop);
+    free(iop);
 }
 
 int
 io_input(struct iop *iop, int waitforinput)
 {
-    s_char buf[IO_BUFSIZE];
+    char buf[IO_BUFSIZE];
     int cc;
 
     /* Not a read IOP */
@@ -159,33 +122,17 @@ io_input(struct iop *iop, int waitforinput)
     if (waitforinput) {
        empth_select(iop->fd, EMPTH_FD_READ);
     }
-#if !defined(_WIN32)
     /* Do the actual read. */
     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... */
        iop->flags |= IO_ERROR;
-       iom_clear(iom, IO_READ, iop->fd);
        return -1;
     }
-#else
-    cc = recv(iop->fd, buf, sizeof(buf), 0);
-    if (cc == SOCKET_ERROR) {
-       int err = WSAGetLastError();
-       /* Hmm, it would block.  file is opened noblock, soooooo.. */
-       if (err == WSAEWOULDBLOCK)
-           return 0;
-
-       /* Some form of file error occurred... */
-       iop->flags |= IO_ERROR;
-       iom_clear(iom, IO_READ, iop->fd);
-       return -1;
-    }
-#endif
 
     /* We eof'd */
     if (cc == 0) {
@@ -213,11 +160,7 @@ io_outputwaiting(struct iop *iop)
 int
 io_output(struct iop *iop, int waitforoutput)
 {
-#if !defined(_WIN32)
     struct iovec iov[16];
-#else
-    s_char buf[IO_BUFSIZE];
-#endif
     int cc;
     int n;
     int remain;
@@ -226,9 +169,6 @@ io_output(struct iop *iop, int waitforoutput)
     if (!io_outputwaiting(iop))
        return 0;
 
-    /* bit clear */
-    FD_CLR(iop->fd, &newoutput);
-
     /* If the iop is not write enabled. */
     if ((iop->flags & IO_WRITE) == 0)
        return -1;
@@ -237,23 +177,12 @@ 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. */
     /* returns the number of elements in the iov. */
     n = ioq_makeiov(iop->output, iov, IO_BUFSIZE);
-#else
-    /* Make a buffer containing the output to write. */
-    n = ioq_makebuf(iop->output, buf, sizeof(buf));
-#endif
 
     if (n <= 0) {
-       /* If we got no elements, we have no output.... */
-       iom_clear(iom, IO_WRITE, iop->fd);
        return 0;
     }
 
@@ -265,124 +194,34 @@ io_output(struct iop *iop, int waitforoutput)
     }
 
     /* Do the actual write. */
-#if !defined(_WIN32)
     cc = writev(iop->fd, iov, n);
 
     /* if it failed.... */
     if (cc < 0) {
        /* Hmm, it would block.  file is opened noblock, soooooo.. */
-       if (errno == EWOULDBLOCK) {
-           /* If there are remaining bytes, set the IO as remaining.. */
-           remain = ioq_qsize(iop->output);
-           if (remain > 0)
-               iom_set(iom, IO_WRITE, iop->fd);
-           return remain;
-       }
-       iop->flags |= IO_ERROR;
-       iom_clear(iom, IO_WRITE, iop->fd);
-       return -1;
-    }
-#else
-    cc = send(iop->fd, buf, n, 0);
-
-    /* if it failed.... */
-    if (cc == SOCKET_ERROR) {
-       int err = WSAGetLastError();
-       /* Hmm, it would block.  file is opened noblock, soooooo.. */
-       if (err == WSAEWOULDBLOCK) {
+       if (errno == EAGAIN || errno == EWOULDBLOCK) {
            /* If there are remaining bytes, set the IO as remaining.. */
            remain = ioq_qsize(iop->output);
-           if (remain > 0)
-               iom_set(iom, IO_WRITE, iop->fd);
            return remain;
        }
        iop->flags |= IO_ERROR;
-       iom_clear(iom, IO_WRITE, iop->fd);
        return -1;
     }
-#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);
-       if (remain > 0)
-           iom_set(iom, IO_WRITE, iop->fd);
-       return remain;
-    }
-#endif /* hpux */
 
     /* Remove the number of written bytes from the queue. */
     ioq_dequeue(iop->output, cc);
 
-    /* If the queue has stuff remaining, set it still needing output. */
-    remain = ioq_qsize(iop->output);
-    if (remain == 0) {
-       iom_clear(iom, IO_WRITE, iop->fd);
-    } else {
-       iom_set(iom, IO_WRITE, iop->fd);
-    }
     return cc;
 }
 
 int
-io_select(struct timeval *tv)
-{
-    fd_set *readmask;
-    fd_set *writemask;
-    int n;
-    int maxfd;
-    int fd;
-    struct iop *iop;
-
-    iom_getmask(iom, &maxfd, &readmask, &writemask);
-    n = select(maxfd + 1, readmask, writemask, NULL, tv);
-    if (n <= 0) {
-       if (errno == EINTR)
-           return 0;
-       return -1;
-    }
-    for (fd = 0; fd <= maxfd; ++fd) {
-       if (!FD_ISSET(fd, readmask)) continue;
-       iop = io_list[fd];
-       if ((iop->flags & IO_NEWSOCK) == 0)
-           (void)io_input(iop, IO_NOWAIT);
-       if (iop->notify != 0)
-           iop->notify(iop, IO_READ, iop->assoc);
-       FD_CLR(fd, readmask);
-    }
-    for (fd = 0; fd <= maxfd; ++fd) {
-       if (!FD_ISSET(fd, writemask)) continue;
-       iop = io_list[fd];
-       if (io_output(iop, IO_NOWAIT) < 0 && iop->notify != 0)
-           iop->notify(iop, IO_WRITE, iop->assoc);
-       FD_CLR(fd, writemask);
-    }
-    return n;
-}
-
-void
-io_flush(int doWait)
-{
-    int fd;
-    struct iop *iop;
-
-    for (fd = 0; fd <= fdmax; ++fd) {
-       if (!FD_ISSET(fd, &newoutput)) continue;
-       iop = io_list[fd];
-       if (io_output(iop, doWait) < 0 && iop->notify != 0)
-           iop->notify(iop, IO_WRITE, iop->assoc);
-    }
-}
-
-int
-io_peek(struct iop *iop, s_char *buf, int nbytes)
+io_peek(struct iop *iop, char *buf, int nbytes)
 {
     if ((iop->flags & IO_READ) == 0)
        return -1;
@@ -390,7 +229,7 @@ io_peek(struct iop *iop, s_char *buf, int nbytes)
 }
 
 int
-io_read(struct iop *iop, s_char *buf, int nbytes)
+io_read(struct iop *iop, char *buf, int nbytes)
 {
     int cc;
 
@@ -403,14 +242,13 @@ io_read(struct iop *iop, s_char *buf, int nbytes)
 }
 
 int
-io_write(struct iop *iop, s_char *buf, int nbytes, int doWait)
+io_write(struct iop *iop, char *buf, int nbytes, int doWait)
 {
     int len;
 
     if ((iop->flags & IO_WRITE) == 0)
        return -1;
     ioq_append(iop->output, buf, nbytes);
-    FD_SET(iop->fd, &newoutput);
     len = ioq_qsize(iop->output);
     if (len > iop->bufsize) {
        if (doWait) {
@@ -418,7 +256,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;
@@ -429,14 +267,18 @@ 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 && !play_wrlock_wanted)
        empth_select(iop->fd, EMPTH_FD_WRITE);
-    }
+
     return n;
 }
 
 int
-io_gets(struct iop *iop, s_char *buf, int nbytes)
+io_gets(struct iop *iop, char *buf, int nbytes)
 {
     if ((iop->flags & IO_READ) == 0)
        return -1;
@@ -444,11 +286,10 @@ io_gets(struct iop *iop, s_char *buf, int nbytes)
 }
 
 int
-io_puts(struct iop *iop, s_char *buf)
+io_puts(struct iop *iop, char *buf)
 {
     if ((iop->flags & IO_WRITE) == 0)
        return -1;
-    FD_SET(iop->fd, &newoutput);
     return ioq_puts(iop->output, buf);
 }
 
@@ -472,22 +313,17 @@ io_shutdown(struct iop *iop, int flags)
 int
 io_noblocking(struct iop *iop, int value)
 {
-#if !defined(_WIN32)
     int flags;
 
     flags = fcntl(iop->fd, F_GETFL, 0);
     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
-    u_long arg = value;
-    ioctlsocket(iop->fd, FIONBIO, &arg);
-#endif
     if (value == 0)
        iop->flags &= ~IO_NBLOCK;
     else
@@ -495,22 +331,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
@@ -518,9 +348,3 @@ io_fileno(struct iop *iop)
 {
     return iop->fd;
 }
-
-struct iop *
-io_iopfromfd(int fd)
-{
-    return io_list[fd];
-}