]> 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 8a3d820e21c801af23add160d7339f378452339a..4c42a55efedb0c6137dff4b4709cc8b53823f75b 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2006, 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
 #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)
-#define WIN32
-#include <winsock2.h>
-#undef NS_ALL
-#endif
-
+#include <unistd.h>
+#include "empio.h"
+#include "empthread.h"
+#include "ioqueue.h"
 #include "misc.h"
 #include "queue.h"
-#include "ioqueue.h"
-#include "empio.h"
-#include "gen.h"               /* getfdtablesize */
 #include "server.h"
 
-#include "empthread.h"
-
 struct iop {
     int fd;
     struct ioqueue *input;
     struct ioqueue *output;
     int flags;
-    s_char *assoc;
     int bufsize;
-    int (*notify)(void);
 };
 
 void
@@ -84,8 +69,7 @@ io_init(void)
 }
 
 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;
 
@@ -107,8 +91,6 @@ io_open(int fd, int flags, int bufsize, int (*notify)(void),
     if (flags & IO_NBLOCK)
        io_noblocking(iop, 1);  /* FIXME check success */
     iop->flags = flags;
-    iop->assoc = assoc;
-    iop->notify = notify;
     return iop;
 }
 
@@ -120,18 +102,14 @@ io_close(struct iop *iop)
        ioq_destroy(iop->input);
     if (iop->output != 0)
        ioq_destroy(iop->output);
-#if !defined(_WIN32)
     (void)close(iop->fd);
-#else
-    closesocket(iop->fd);
-#endif
     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 */
@@ -144,7 +122,6 @@ 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) {
@@ -156,19 +133,6 @@ io_input(struct iop *iop, int waitforinput)
        iop->flags |= IO_ERROR;
        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;
-       return -1;
-    }
-#endif
 
     /* We eof'd */
     if (cc == 0) {
@@ -196,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;
@@ -217,15 +177,10 @@ io_output(struct iop *iop, int waitforoutput)
     if (iop->flags & IO_ERROR)
        return -1;
 
-#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) {
        return 0;
@@ -239,7 +194,6 @@ io_output(struct iop *iop, int waitforoutput)
     }
 
     /* Do the actual write. */
-#if !defined(_WIN32)
     cc = writev(iop->fd, iov, n);
 
     /* if it failed.... */
@@ -253,22 +207,6 @@ io_output(struct iop *iop, int waitforoutput)
        iop->flags |= IO_ERROR;
        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 there are remaining bytes, set the IO as remaining.. */
-           remain = ioq_qsize(iop->output);
-           return remain;
-       }
-       iop->flags |= IO_ERROR;
-       return -1;
-    }
-#endif
 
     /* If no bytes were written, something happened..  Like an EOF. */
     if (cc == 0) {
@@ -283,7 +221,7 @@ io_output(struct iop *iop, int waitforoutput)
 }
 
 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;
@@ -291,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;
 
@@ -304,7 +242,7 @@ 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;
 
@@ -333,14 +271,14 @@ io_output_all(struct iop *iop)
      * 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) {
+    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;
@@ -348,7 +286,7 @@ 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;
@@ -375,7 +313,6 @@ 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);
@@ -387,10 +324,6 @@ io_noblocking(struct iop *iop, int value)
        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