2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * ioqueue.c: Manage an i/o queue
30 * Known contributors to this file:
37 #include <sys/types.h>
42 static int ioqtobuf(register struct ioqueue *ioq, s_char *buf, int cc);
43 static void enqueuecc(struct ioqueue *ioq, s_char *buf, int cc);
44 static int dequeuecc(register struct ioqueue *ioq, register int cc);
48 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, s_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, s_char *buf, int cc)
80 n = ioqtobuf(ioq, buf, cc);
87 ioq_write(struct ioqueue *ioq, s_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, s_char *buf, int cc)
117 register s_char *end;
120 nbytes = ioqtobuf(ioq, buf, cc);
124 for (p = buf; p < end && *p; p++) {
127 dequeuecc(ioq, (p - buf) + 1);
135 * all the rest are local to this module
140 * copy cc bytes from ioq to buf.
141 * this routine doesn't free memory; this is
142 * left for a higher level.
145 ioqtobuf(register struct ioqueue *ioq, s_char *buf, int cc)
147 register struct io *io;
155 for (qp = ioq->queue.q_forw; qp != &ioq->queue; qp = qp->q_forw) {
156 io = (struct io *)qp;
157 if ((nbytes = io->nbytes - io->offset) < 0) {
158 fprintf(stderr, "ioqtobuf: offset %d nbytes %d\n",
159 io->offset, io->nbytes);
165 memcpy(offset, io->data + io->offset, nbytes);
174 * append a buffer to the end of the ioq.
177 enqueuecc(struct ioqueue *ioq, s_char *buf, int cc)
181 io = (struct io *)malloc(sizeof(*io));
185 insque(&io->queue, ioq->queue.q_back);
190 * remove cc bytes from ioqueue ioq
191 * free memory, dequeue io elements
192 * which are no longer used.
195 dequeuecc(register struct ioqueue *ioq, register int cc)
197 register struct io *io;
198 register struct qelem *qp;
203 while ((qp = ioq->queue.q_forw) != &ioq->queue) {
204 io = (struct io *)qp;
205 there = io->nbytes - io->offset;
207 fprintf(stderr, "dequeuecc: nbytes %d, offset %d\n",
208 io->nbytes, io->offset);
214 (void)remque(&io->queue);