2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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: Read and write i/o queues
30 * Known contributors to this file:
35 * Read and write onto io queues. Note that
36 * the io queues don't actually do any writing;
37 * that is left for a higher level.
41 #include <stdlib.h> /* malloc free */
42 #include <sys/types.h>
50 static int ioqtocbuf(struct ioqueue *ioq, s_char *buf, int cc,
53 static int ioqtoiov(struct ioqueue *ioq, struct iovec *iov,
56 static int ioqtobuf(struct ioqueue *ioq, s_char *buf, int cc);
57 static int appendcc(struct ioqueue *ioq, s_char *buf, int cc);
58 static int removecc(struct ioqueue *ioq, register int cc);
61 static void loc_StripDels(char *pBuf);
69 ioq = malloc(sizeof(struct ioqueue));
70 emp_initque(&ioq->list.queue);
81 ioq_destroy(struct ioqueue *ioq)
88 ioq_drain(struct ioqueue *ioq)
93 while ((qp = ioq->list.queue.q_forw) != &ioq->list.queue) {
95 emp_remque(&io->queue);
104 * copy batch of pointers into the passed
105 * iovec, but don't actually dequeue the data.
106 * return # of iovec initialized.
110 ioq_makeiov(struct ioqueue *ioq, struct iovec *iov, int cc)
114 return ioqtoiov(ioq, iov, cc);
119 * Copy the specified number of characters into the buffer
120 * provided, without actually dequeueing the data. Return
121 * number of bytes actually found.
124 ioq_peek(struct ioqueue *ioq, s_char *buf, int cc)
126 return ioqtobuf(ioq, buf, cc);
130 ioq_dequeue(struct ioqueue *ioq, int cc)
132 return removecc(ioq, cc);
136 ioq_append(struct ioqueue *ioq, s_char *buf, int cc)
138 appendcc(ioq, buf, cc);
142 ioq_qsize(struct ioqueue *ioq)
148 * read a line of text up to (but not including)
149 * the newline. return -1 and read nothing if
150 * no input is available
153 ioq_gets(struct ioqueue *ioq, s_char *buf, int cc)
158 nbytes = ioqtocbuf(ioq, buf, cc - 1, '\n');
163 /* telnet terminates lines with "\r\n", get rid of \r */
164 if (actual > 0 && buf[actual-1] == '\r')
167 /* remove the newline too */
168 removecc(ioq, nbytes + 1);
174 ioq_puts(struct ioqueue *ioq, s_char *buf)
176 return appendcc(ioq, buf, strlen(buf));
180 * all the rest are local to this module
185 * copy cc bytes from ioq to buf.
186 * this routine doesn't free memory; this is
187 * left for a higher level.
190 ioqtobuf(struct ioqueue *ioq, s_char *buf, int cc)
193 struct emp_qelem *qp;
194 struct emp_qelem *head;
197 register s_char *offset;
201 head = &ioq->list.queue;
202 for (qp = head->q_forw; qp != head && nleft > 0; qp = qp->q_forw) {
203 io = (struct io *)qp;
204 if ((nbytes = io->nbytes - io->offset) < 0) {
205 /* XXX log something here */
211 memcpy(offset, io->data + io->offset, nbytes);
220 * copy at most cc bytes from ioq to buf,
221 * terminating on the stop character.
224 ioqtocbuf(struct ioqueue *ioq, s_char *buf, int cc, register int stopc)
230 struct emp_qelem *qp;
231 struct emp_qelem *head;
235 head = &ioq->list.queue;
238 for (qp = head->q_forw; qp != head; qp = qp->q_forw) {
239 io = (struct io *)qp;
240 if ((nbytes = io->nbytes - io->offset) <= 0)
242 p = io->data + io->offset;
243 for (n = 0; n < nbytes && p[n] != stopc; n++) ;
252 ioqtobuf(ioq, buf, cc < total ? cc : total);
257 * initialize an iovec to point at max bytes worth
258 * of data from the ioqueue.
262 ioqtoiov(struct ioqueue *ioq, struct iovec *iov, register int max)
268 struct emp_qelem *qp;
272 qp = ioq->list.queue.q_forw;
273 while (qp != &ioq->list.queue && cc > 0) {
274 io = (struct io *)qp;
275 len = io->nbytes - io->offset;
278 iov->iov_base = io->data + io->offset;
292 * append a buffer to the end of the ioq.
295 appendcc(struct ioqueue *ioq, s_char *buf, int cc)
302 /* determine if any space is left */
303 io = (struct io *)ioq->list.queue.q_back;
304 avail = io->size - io->nbytes;
306 /* append to existing buffer */
307 len = cc > avail ? avail : cc;
308 memcpy(io->data + io->nbytes, buf, len);
312 appendcc(ioq, buf + len, cc - len);
314 /* create a new buffer, minimum bufsize bytes */
315 len = cc > ioq->bufsize ? cc : ioq->bufsize;
317 memcpy(ptr, buf, cc);
318 io = malloc(sizeof(struct io));
323 emp_insque(&io->queue, ioq->list.queue.q_back);
330 * remove cc bytes from ioqueue ioq
331 * free memory, dequeue io elements
332 * which are no longer used.
335 removecc(struct ioqueue *ioq, register int cc)
338 struct emp_qelem *qp;
345 while ((qp = ioq->list.queue.q_forw) != &ioq->list.queue) {
346 io = (struct io *)qp;
347 there = io->nbytes - io->offset;
350 emp_remque(&io->queue);
354 if (remain >= there) {
355 /* not enough or exact; free entry */
358 emp_remque(&io->queue);
362 /* too much; increment offset */
363 io->offset += remain;
376 * Make an (output) buffer up to the
377 * maximum size of the buffer.
379 * We don't free the bytes...
382 ioq_makebuf(struct ioqueue *ioq, char *pBuf, int nBufLen)
385 struct emp_qelem *qp;
386 struct emp_qelem *head;
395 head = &ioq->list.queue;
397 for (qp = head->q_forw; (qp != head) && (nleft > 0); qp = qp->q_forw) {
398 io = (struct io *)qp;
399 nbytes = io->nbytes - io->offset;
401 /* Paranoid check for bad buffer. */
405 /* too many bytes, wait till next time. */
409 memcpy(offset, io->data + io->offset, nbytes);