2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2007, 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:
43 static int ioqtobuf(struct ioqueue *ioq, char *buf, int cc);
44 static void enqueuecc(struct ioqueue *ioq, char *buf, int cc);
45 static int dequeuecc(struct ioqueue *ioq, int cc);
49 ioq_init(struct ioqueue *ioq, int bsize)
57 * Copy the specified number of characters into the buffer
58 * provided, without actually dequeueing the data. Return
59 * number of bytes actually found.
62 ioq_peek(struct ioqueue *ioq, char *buf, int cc)
64 return ioqtobuf(ioq, buf, cc);
68 ioq_dequeue(struct ioqueue *ioq, int cc)
70 if (dequeuecc(ioq, cc) != cc)
76 ioq_read(struct ioqueue *ioq, char *buf, int cc)
80 n = ioqtobuf(ioq, buf, cc);
87 ioq_write(struct ioqueue *ioq, char *buf, int cc)
89 enqueuecc(ioq, buf, cc);
93 ioq_qsize(struct ioqueue *ioq)
99 ioq_drain(struct ioqueue *ioq)
104 while ((qp = ioq->queue.q_forw) != &ioq->queue) {
105 io = (struct io *)qp;
107 (void)remque(&io->queue);
114 ioq_gets(struct ioqueue *ioq, char *buf, int cc, int *eol)
124 nbytes = ioqtobuf(ioq, buf, cc);
126 for (p = buf; p < end && *p; p++) {
130 dequeuecc(ioq, p - buf);
134 if (cc && (p - buf) == cc) {
143 * all the rest are local to this module
148 * copy cc bytes from ioq to buf.
149 * this routine doesn't free memory; this is
150 * left for a higher level.
153 ioqtobuf(struct ioqueue *ioq, char *buf, int cc)
163 for (qp = ioq->queue.q_forw; qp != &ioq->queue; qp = qp->q_forw) {
164 io = (struct io *)qp;
165 if ((nbytes = io->nbytes - io->offset) < 0) {
166 fprintf(stderr, "ioqtobuf: offset %d nbytes %d\n",
167 io->offset, io->nbytes);
173 memcpy(offset, io->data + io->offset, nbytes);
182 * append a buffer to the end of the ioq.
185 enqueuecc(struct ioqueue *ioq, char *buf, int cc)
189 io = malloc(sizeof(*io));
193 insque(&io->queue, ioq->queue.q_back);
198 * remove cc bytes from ioqueue ioq
199 * free memory, dequeue io elements
200 * which are no longer used.
203 dequeuecc(struct ioqueue *ioq, int cc)
211 while ((qp = ioq->queue.q_forw) != &ioq->queue) {
212 io = (struct io *)qp;
213 there = io->nbytes - io->offset;
215 fprintf(stderr, "dequeuecc: nbytes %d, offset %d\n",
216 io->nbytes, io->offset);
222 (void)remque(&io->queue);