2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * ioqueue.c: Manage an i/o queue
30 * Known contributors to this file:
39 #include <sys/types.h>
44 static int ioqtobuf(struct ioqueue *ioq, char *buf, int cc);
45 static void enqueuecc(struct ioqueue *ioq, char *buf, int cc);
46 static int dequeuecc(struct ioqueue *ioq, int cc);
50 ioq_init(struct ioqueue *ioq, int bsize)
59 * Copy the specified number of characters into the buffer
60 * provided, without actually dequeueing the data. Return
61 * number of bytes actually found.
64 ioq_peek(struct ioqueue *ioq, char *buf, int cc)
66 return ioqtobuf(ioq, buf, cc);
70 ioq_dequeue(struct ioqueue *ioq, int cc)
72 if (dequeuecc(ioq, cc) != cc)
78 ioq_read(struct ioqueue *ioq, char *buf, int cc)
82 n = ioqtobuf(ioq, buf, cc);
89 ioq_write(struct ioqueue *ioq, char *buf, int cc)
91 enqueuecc(ioq, buf, cc);
95 ioq_qsize(struct ioqueue *ioq)
101 ioq_drain(struct ioqueue *ioq)
106 while ((qp = ioq->queue.q_forw) != &ioq->queue) {
107 io = (struct io *)qp;
109 (void)remque(&io->queue);
116 ioq_gets(struct ioqueue *ioq, char *buf, int cc, int *eol)
126 nbytes = ioqtobuf(ioq, buf, cc);
128 for (p = buf; p < end && *p; p++) {
132 dequeuecc(ioq, p - buf);
136 if (cc && (p - buf) == cc) {
145 * all the rest are local to this module
150 * copy cc bytes from ioq to buf.
151 * this routine doesn't free memory; this is
152 * left for a higher level.
155 ioqtobuf(struct ioqueue *ioq, char *buf, int cc)
165 for (qp = ioq->queue.q_forw; qp != &ioq->queue; qp = qp->q_forw) {
166 io = (struct io *)qp;
167 if ((nbytes = io->nbytes - io->offset) < 0) {
168 fprintf(stderr, "ioqtobuf: offset %d nbytes %d\n",
169 io->offset, io->nbytes);
175 memcpy(offset, io->data + io->offset, nbytes);
184 * append a buffer to the end of the ioq.
187 enqueuecc(struct ioqueue *ioq, char *buf, int cc)
191 io = malloc(sizeof(*io));
195 insque(&io->queue, ioq->queue.q_back);
200 * remove cc bytes from ioqueue ioq
201 * free memory, dequeue io elements
202 * which are no longer used.
205 dequeuecc(struct ioqueue *ioq, int cc)
213 while ((qp = ioq->queue.q_forw) != &ioq->queue) {
214 io = (struct io *)qp;
215 there = io->nbytes - io->offset;
217 fprintf(stderr, "dequeuecc: nbytes %d, offset %d\n",
218 io->nbytes, io->offset);
224 (void)remque(&io->queue);