Replace the per-iop input_timeout by per-function timeouts

Commit 08b94556 (v4.3.20) added io_open() parameter input_timeout.  It
applies to io_input() and, since commit 904822e3, to io_close().  Add
timeout parameters to these functions instead.
This commit is contained in:
Markus Armbruster 2012-03-10 13:27:34 +01:00
parent 0513ec136b
commit a96b400da3
5 changed files with 32 additions and 29 deletions

View file

@ -27,7 +27,7 @@
* empio.h: Describes io pointers used in Empire * empio.h: Describes io pointers used in Empire
* *
* Known contributors to this file: * Known contributors to this file:
* Markus Armbruster, 2004-2010 * Markus Armbruster, 2004-2012
*/ */
#ifndef EMPIO_H #ifndef EMPIO_H
@ -41,10 +41,10 @@
#define IO_BUFSIZE 4096 #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_init(void);
extern void io_close(struct iop *); extern void io_close(struct iop *, struct timeval *);
extern int io_input(struct iop *, int); extern int io_input(struct iop *, struct timeval *);
extern int io_inputwaiting(struct iop *); extern int io_inputwaiting(struct iop *);
extern int io_outputwaiting(struct iop *); extern int io_outputwaiting(struct iop *);
extern int io_output(struct iop *, int); extern int io_output(struct iop *, int);

View file

@ -63,7 +63,6 @@ struct iop {
int flags; int flags;
int bufsize; int bufsize;
int last_out; int last_out;
struct timeval input_timeout;
}; };
void void
@ -72,7 +71,7 @@ io_init(void)
} }
struct iop * struct iop *
io_open(int fd, int flags, int bufsize, struct timeval timeout) io_open(int fd, int flags, int bufsize)
{ {
int fdfl; int fdfl;
struct iop *iop; struct iop *iop;
@ -97,7 +96,6 @@ io_open(int fd, int flags, int bufsize, struct timeval timeout)
iop->flags = flags; iop->flags = flags;
iop->last_out = 0; iop->last_out = 0;
iop->bufsize = bufsize; iop->bufsize = bufsize;
iop->input_timeout = timeout;
if (flags & IO_READ) if (flags & IO_READ)
iop->input = ioq_create(bufsize); iop->input = ioq_create(bufsize);
if (flags & IO_WRITE) if (flags & IO_WRITE)
@ -106,14 +104,14 @@ io_open(int fd, int flags, int bufsize, struct timeval timeout)
} }
void void
io_close(struct iop *iop) io_close(struct iop *iop, struct timeval *timeout)
{ {
char buf[IO_BUFSIZE]; char buf[IO_BUFSIZE];
int ret; int ret;
while (io_output(iop, 1) > 0) ; while (io_output(iop, 1) > 0) ;
shutdown(iop->fd, SHUT_WR); 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)); ret = read(iop->fd, buf, sizeof(buf));
if (ret <= 0) if (ret <= 0)
break; break;
@ -127,13 +125,16 @@ io_close(struct iop *iop)
} }
/* /*
* Return number of bytes read on success, zero on timeout, early * Read input from IOP and enqueue it.
* wakeup or EOF, -1 on error. In particular, return 0 when no data * If TIMEOUT is non-null, wait at most that long for input to arrive.
* is available for non-blocking input (WAITFORINPUT false). * Does not yield the processor when timeout is zero.
* Use io_eof() to distinguish timeout and early wakeup from EOF. * 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 int
io_input(struct iop *iop, int waitforinput) io_input(struct iop *iop, struct timeval *timeout)
{ {
char buf[IO_BUFSIZE]; char buf[IO_BUFSIZE];
int cc; int cc;
@ -146,9 +147,8 @@ io_input(struct iop *iop, int waitforinput)
if (iop->flags & IO_EOF) if (iop->flags & IO_EOF)
return 0; return 0;
/* Wait for the file to have input. */ if (!timeout || timeout->tv_sec || timeout->tv_usec) {
if (waitforinput) { res = empth_select(iop->fd, EMPTH_FD_READ, timeout);
res = empth_select(iop->fd, EMPTH_FD_READ, &iop->input_timeout);
if (res < 0) { if (res < 0) {
iop->flags |= IO_ERROR; iop->flags |= IO_ERROR;
return -1; return -1;
@ -156,7 +156,6 @@ io_input(struct iop *iop, int waitforinput)
return 0; return 0;
} }
/* Do the actual read. */
cc = read(iop->fd, buf, sizeof(buf)); cc = read(iop->fd, buf, sizeof(buf));
if (cc < 0) { if (cc < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) if (errno == EAGAIN || errno == EWOULDBLOCK)
@ -164,14 +163,11 @@ io_input(struct iop *iop, int waitforinput)
iop->flags |= IO_ERROR; iop->flags |= IO_ERROR;
return -1; return -1;
} }
/* We eof'd */
if (cc == 0) { if (cc == 0) {
iop->flags |= IO_EOF; iop->flags |= IO_EOF;
return 0; return 0;
} }
/* Append the input to the IOQ. */
ioq_append(iop->input, buf, cc); ioq_append(iop->input, buf, cc);
return cc; return cc;
} }

View file

@ -28,7 +28,7 @@
* *
* Known contributors to this file: * Known contributors to this file:
* Dave Pare, 1994 * Dave Pare, 1994
* Markus Armbruster, 2005-2010 * Markus Armbruster, 2005-2012
*/ */
#include <config.h> #include <config.h>
@ -71,17 +71,14 @@ struct player *
player_new(int s) player_new(int s)
{ {
struct player *lp; struct player *lp;
struct timeval idle_timeout;
lp = malloc(sizeof(struct player)); lp = malloc(sizeof(struct player));
if (!lp) if (!lp)
return NULL; return NULL;
memset(lp, 0, sizeof(struct player)); memset(lp, 0, sizeof(struct player));
idle_timeout.tv_sec = max_idle * 60;
idle_timeout.tv_usec = 0;
if (s >= 0) { if (s >= 0) {
/* real player, not dummy created by update and market update */ /* 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) { if (!lp->iop) {
free(lp); free(lp);
return NULL; return NULL;
@ -98,11 +95,14 @@ player_new(int s)
struct player * struct player *
player_delete(struct player *lp) player_delete(struct player *lp)
{ {
struct timeval timeout;
struct player *back; struct player *back;
if (lp->iop) { if (lp->iop) {
/* it's a real player */ /* 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; lp->iop = NULL;
} }
back = (struct player *)lp->queue.q_back; back = (struct player *)lp->queue.q_back;

View file

@ -76,6 +76,7 @@ static struct cmndstr login_coms[] = {
void void
player_login(void *ud) player_login(void *ud)
{ {
struct timeval timeout;
char buf[128]; char buf[128];
char space[128]; char space[128];
int ac; int ac;
@ -89,7 +90,9 @@ player_login(void *ud)
for (;;) { for (;;) {
io_output(player->iop, 1); io_output(player->iop, 1);
if (io_gets(player->iop, buf, sizeof(buf)) < 0) { 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) {
if (res == 0 && !io_eof(player->iop)) if (res == 0 && !io_eof(player->iop))
pr_id(player, C_DATA, "idle connection terminated\n"); pr_id(player, C_DATA, "idle connection terminated\n");

View file

@ -36,6 +36,7 @@
#include "empio.h" #include "empio.h"
#include "journal.h" #include "journal.h"
#include "optlist.h"
#include "player.h" #include "player.h"
#include "prototypes.h" #include "prototypes.h"
@ -60,6 +61,7 @@ int
recvclient(char *cmd, int size) recvclient(char *cmd, int size)
{ {
int count, res; int count, res;
struct timeval timeout;
count = -1; count = -1;
while (!player->aborted) { while (!player->aborted) {
@ -90,7 +92,9 @@ recvclient(char *cmd, int size)
if (player->aborted) if (player->aborted)
break; 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) if (res > 0)
; ;
else if (res < 0) else if (res < 0)