* empio.h: Describes io pointers used in Empire
*
* Known contributors to this file:
- * Markus Armbruster, 2004-2010
+ * Markus Armbruster, 2004-2012
*/
#ifndef EMPIO_H
#define IO_BUFSIZE 4096
-extern struct iop *io_open(int, int, int, struct timeval);
+extern struct iop *io_open(int, int, int);
extern void io_init(void);
-extern void io_close(struct iop *);
-extern int io_input(struct iop *, int);
+extern void io_close(struct iop *, struct timeval *);
+extern int io_input(struct iop *, struct timeval *);
extern int io_inputwaiting(struct iop *);
extern int io_outputwaiting(struct iop *);
extern int io_output(struct iop *, int);
int flags;
int bufsize;
int last_out;
- struct timeval input_timeout;
};
void
}
struct iop *
-io_open(int fd, int flags, int bufsize, struct timeval timeout)
+io_open(int fd, int flags, int bufsize)
{
int fdfl;
struct iop *iop;
iop->flags = flags;
iop->last_out = 0;
iop->bufsize = bufsize;
- iop->input_timeout = timeout;
if (flags & IO_READ)
iop->input = ioq_create(bufsize);
if (flags & IO_WRITE)
}
void
-io_close(struct iop *iop)
+io_close(struct iop *iop, struct timeval *timeout)
{
char buf[IO_BUFSIZE];
int ret;
while (io_output(iop, 1) > 0) ;
shutdown(iop->fd, SHUT_WR);
- while (empth_select(iop->fd, EMPTH_FD_READ, &iop->input_timeout) > 0) {
+ while (empth_select(iop->fd, EMPTH_FD_READ, timeout) > 0) {
ret = read(iop->fd, buf, sizeof(buf));
if (ret <= 0)
break;
}
/*
- * Return number of bytes read on success, zero on timeout, early
- * wakeup or EOF, -1 on error. In particular, return 0 when no data
- * is available for non-blocking input (WAITFORINPUT false).
- * Use io_eof() to distinguish timeout and early wakeup from EOF.
+ * Read input from IOP and enqueue it.
+ * If TIMEOUT is non-null, wait at most that long for input to arrive.
+ * Does not yield the processor when timeout is zero.
+ * A wait for input can be cut short by empth_wakeup().
+ * Return number of bytes read on success, -1 on error.
+ * In particular, return zero on timeout, early wakeup or EOF. Use
+ * io_eof() to distinguish timeout and early wakeup from EOF.
*/
int
-io_input(struct iop *iop, int waitforinput)
+io_input(struct iop *iop, struct timeval *timeout)
{
char buf[IO_BUFSIZE];
int cc;
if (iop->flags & IO_EOF)
return 0;
- /* Wait for the file to have input. */
- if (waitforinput) {
- res = empth_select(iop->fd, EMPTH_FD_READ, &iop->input_timeout);
+ if (!timeout || timeout->tv_sec || timeout->tv_usec) {
+ res = empth_select(iop->fd, EMPTH_FD_READ, timeout);
if (res < 0) {
iop->flags |= IO_ERROR;
return -1;
return 0;
}
- /* Do the actual read. */
cc = read(iop->fd, buf, sizeof(buf));
if (cc < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK)
iop->flags |= IO_ERROR;
return -1;
}
-
- /* We eof'd */
if (cc == 0) {
iop->flags |= IO_EOF;
return 0;
}
- /* Append the input to the IOQ. */
ioq_append(iop->input, buf, cc);
return cc;
}
*
* Known contributors to this file:
* Dave Pare, 1994
- * Markus Armbruster, 2005-2010
+ * Markus Armbruster, 2005-2012
*/
#include <config.h>
player_new(int s)
{
struct player *lp;
- struct timeval idle_timeout;
lp = malloc(sizeof(struct player));
if (!lp)
return NULL;
memset(lp, 0, sizeof(struct player));
- idle_timeout.tv_sec = max_idle * 60;
- idle_timeout.tv_usec = 0;
if (s >= 0) {
/* real player, not dummy created by update and market update */
- lp->iop = io_open(s, IO_READ | IO_WRITE, IO_BUFSIZE, idle_timeout);
+ lp->iop = io_open(s, IO_READ | IO_WRITE, IO_BUFSIZE);
if (!lp->iop) {
free(lp);
return NULL;
struct player *
player_delete(struct player *lp)
{
+ struct timeval timeout;
struct player *back;
if (lp->iop) {
/* it's a real player */
- io_close(lp->iop);
+ timeout.tv_sec = minutes(max_idle);
+ timeout.tv_usec = 0;
+ io_close(lp->iop, &timeout);
lp->iop = NULL;
}
back = (struct player *)lp->queue.q_back;
void
player_login(void *ud)
{
+ struct timeval timeout;
char buf[128];
char space[128];
int ac;
for (;;) {
io_output(player->iop, 1);
if (io_gets(player->iop, buf, sizeof(buf)) < 0) {
- res = io_input(player->iop, 1);
+ timeout.tv_sec = minutes(max_idle);
+ timeout.tv_usec = 0;
+ res = io_input(player->iop, &timeout);
if (res <= 0) {
if (res == 0 && !io_eof(player->iop))
pr_id(player, C_DATA, "idle connection terminated\n");
#include "empio.h"
#include "journal.h"
+#include "optlist.h"
#include "player.h"
#include "prototypes.h"
recvclient(char *cmd, int size)
{
int count, res;
+ struct timeval timeout;
count = -1;
while (!player->aborted) {
if (player->aborted)
break;
- res = io_input(player->iop, 1);
+ timeout.tv_sec = minutes(max_idle);
+ timeout.tv_usec = 0;
+ res = io_input(player->iop, &timeout);
if (res > 0)
;
else if (res < 0)