2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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);
46 void insque(struct qelem *, struct qelem *);
47 void remque(struct qelem *);
48 void initque(struct qelem *p);
49 struct qelem *makeqt(int nelem);
52 ioq_init(struct ioqueue *ioq, int bsize)
54 extern s_char num_teles[];
63 * Copy the specified number of characters into the buffer
64 * provided, without actually dequeueing the data. Return
65 * number of bytes actually found.
68 ioq_peek(struct ioqueue *ioq, s_char *buf, int cc)
70 return ioqtobuf(ioq, buf, cc);
74 ioq_dequeue(struct ioqueue *ioq, int cc)
76 if (dequeuecc(ioq, cc) != cc)
82 ioq_read(struct ioqueue *ioq, s_char *buf, int cc)
86 n = ioqtobuf(ioq, buf, cc);
93 ioq_write(struct ioqueue *ioq, s_char *buf, int cc)
95 enqueuecc(ioq, buf, cc);
99 ioq_qsize(struct ioqueue *ioq)
105 ioq_drain(struct ioqueue *ioq)
110 while ((qp = ioq->queue.q_forw) != &ioq->queue) {
111 io = (struct io *)qp;
113 (void)remque(&io->queue);
120 ioq_gets(struct ioqueue *ioq, s_char *buf, int cc)
123 register s_char *end;
126 nbytes = ioqtobuf(ioq, buf, cc);
130 for (p = buf; p < end && *p; p++) {
133 dequeuecc(ioq, (p - buf) + 1);
141 * all the rest are local to this module
146 * copy cc bytes from ioq to buf.
147 * this routine doesn't free memory; this is
148 * left for a higher level.
151 ioqtobuf(register struct ioqueue *ioq, s_char *buf, int cc)
153 register struct io *io;
161 for (qp = ioq->queue.q_forw; qp != &ioq->queue; qp = qp->q_forw) {
162 io = (struct io *)qp;
163 if ((nbytes = io->nbytes - io->offset) < 0) {
164 fprintf(stderr, "ioqtobuf: offset %d nbytes %d\n",
165 io->offset, io->nbytes);
171 memcpy(offset, io->data + io->offset, nbytes);
180 * append a buffer to the end of the ioq.
183 enqueuecc(struct ioqueue *ioq, s_char *buf, int cc)
187 io = (struct io *)malloc(sizeof(*io));
191 insque(&io->queue, ioq->queue.q_back);
196 * remove cc bytes from ioqueue ioq
197 * free memory, dequeue io elements
198 * which are no longer used.
201 dequeuecc(register struct ioqueue *ioq, register int cc)
203 register struct io *io;
204 register struct qelem *qp;
209 while ((qp = ioq->queue.q_forw) != &ioq->queue) {
210 io = (struct io *)qp;
211 there = io->nbytes - io->offset;
213 fprintf(stderr, "dequeuecc: nbytes %d, offset %d\n",
214 io->nbytes, io->offset);
220 (void)remque(&io->queue);