/*
* 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;
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 */
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) {
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) {
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;
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;
}
/* Do the actual write. */
-#if !defined(_WIN32)
cc = writev(iop->fd, iov, n);
/* if it failed.... */
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) {
}
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;
}
int
-io_read(struct iop *iop, s_char *buf, int nbytes)
+io_read(struct iop *iop, char *buf, int nbytes)
{
int cc;
}
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;
* 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;
}
int
-io_puts(struct iop *iop, s_char *buf)
+io_puts(struct iop *iop, char *buf)
{
if ((iop->flags & IO_WRITE) == 0)
return -1;
int
io_noblocking(struct iop *iop, int value)
{
-#if !defined(_WIN32)
int flags;
flags = fcntl(iop->fd, F_GETFL, 0);
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