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: 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.
43 #include <stdlib.h> /* malloc free */
44 #include <sys/types.h>
52 static int ioqtocbuf(struct ioqueue *ioq, s_char *buf, int cc,
55 static int ioqtoiov(struct ioqueue *ioq, struct iovec *iov,
58 static int ioqtobuf(struct ioqueue *ioq, s_char *buf, int cc);
59 static int appendcc(struct ioqueue *ioq, s_char *buf, int cc);
60 static int removecc(struct ioqueue *ioq, register int cc);
63 static void loc_StripDels(char *pBuf);
71 ioq = malloc(sizeof(struct ioqueue));
72 emp_initque(&ioq->list.queue);
83 ioq_destroy(struct ioqueue *ioq)
90 ioq_drain(struct ioqueue *ioq)
95 while ((qp = ioq->list.queue.q_forw) != &ioq->list.queue) {
97 emp_remque(&io->queue);
106 * copy batch of pointers into the passed
107 * iovec, but don't actually dequeue the data.
108 * return # of iovec initialized.
112 ioq_makeiov(struct ioqueue *ioq, struct iovec *iov, int cc)
116 return ioqtoiov(ioq, iov, cc);
121 * Copy the specified number of characters into the buffer
122 * provided, without actually dequeueing the data. Return
123 * number of bytes actually found.
126 ioq_peek(struct ioqueue *ioq, s_char *buf, int cc)
128 return ioqtobuf(ioq, buf, cc);
132 ioq_dequeue(struct ioqueue *ioq, int cc)
134 return removecc(ioq, cc);
138 ioq_append(struct ioqueue *ioq, s_char *buf, int cc)
140 appendcc(ioq, buf, cc);
144 ioq_qsize(struct ioqueue *ioq)
150 * read a line of text up to (but not including)
151 * the newline. return -1 and read nothing if
152 * no input is available
155 ioq_gets(struct ioqueue *ioq, s_char *buf, int cc)
160 nbytes = ioqtocbuf(ioq, buf, cc - 1, '\n');
165 /* telnet terminates lines with "\r\n", get rid of \r */
166 if (actual > 0 && buf[actual-1] == '\r')
169 /* remove the newline too */
170 removecc(ioq, nbytes + 1);
176 ioq_puts(struct ioqueue *ioq, s_char *buf)
178 return appendcc(ioq, buf, strlen(buf));
182 * all the rest are local to this module
187 * copy cc bytes from ioq to buf.
188 * this routine doesn't free memory; this is
189 * left for a higher level.
192 ioqtobuf(struct ioqueue *ioq, s_char *buf, int cc)
195 struct emp_qelem *qp;
196 struct emp_qelem *head;
199 register s_char *offset;
203 head = &ioq->list.queue;
204 for (qp = head->q_forw; qp != head && nleft > 0; qp = qp->q_forw) {
205 io = (struct io *)qp;
206 if ((nbytes = io->nbytes - io->offset) < 0) {
207 /* XXX log something here */
213 memcpy(offset, io->data + io->offset, nbytes);
222 * copy at most cc bytes from ioq to buf,
223 * terminating on the stop character.
226 ioqtocbuf(struct ioqueue *ioq, s_char *buf, int cc, register int stopc)
232 struct emp_qelem *qp;
233 struct emp_qelem *head;
237 head = &ioq->list.queue;
240 for (qp = head->q_forw; qp != head; qp = qp->q_forw) {
241 io = (struct io *)qp;
242 if ((nbytes = io->nbytes - io->offset) <= 0)
244 p = io->data + io->offset;
245 for (n = 0; n < nbytes && p[n] != stopc; n++) ;
254 ioqtobuf(ioq, buf, cc < total ? cc : total);
259 * initialize an iovec to point at max bytes worth
260 * of data from the ioqueue.
264 ioqtoiov(struct ioqueue *ioq, struct iovec *iov, register int max)
270 struct emp_qelem *qp;
274 qp = ioq->list.queue.q_forw;
275 while (qp != &ioq->list.queue && cc > 0) {
276 io = (struct io *)qp;
277 len = io->nbytes - io->offset;
280 iov->iov_base = io->data + io->offset;
294 * append a buffer to the end of the ioq.
297 appendcc(struct ioqueue *ioq, s_char *buf, int cc)
304 /* determine if any space is left */
305 io = (struct io *)ioq->list.queue.q_back;
306 avail = io->size - io->nbytes;
308 /* append to existing buffer */
309 len = cc > avail ? avail : cc;
310 memcpy(io->data + io->nbytes, buf, len);
314 appendcc(ioq, buf + len, cc - len);
316 /* create a new buffer, minimum bufsize bytes */
317 len = cc > ioq->bufsize ? cc : ioq->bufsize;
319 memcpy(ptr, buf, cc);
320 io = malloc(sizeof(struct io));
325 emp_insque(&io->queue, ioq->list.queue.q_back);
332 * remove cc bytes from ioqueue ioq
333 * free memory, dequeue io elements
334 * which are no longer used.
337 removecc(struct ioqueue *ioq, register int cc)
340 struct emp_qelem *qp;
347 while ((qp = ioq->list.queue.q_forw) != &ioq->list.queue) {
348 io = (struct io *)qp;
349 there = io->nbytes - io->offset;
352 emp_remque(&io->queue);
356 if (remain >= there) {
357 /* not enough or exact; free entry */
360 emp_remque(&io->queue);
364 /* too much; increment offset */
365 io->offset += remain;
378 * Make an (output) buffer up to the
379 * maximum size of the buffer.
381 * We don't free the bytes...
384 ioq_makebuf(struct ioqueue *ioq, char *pBuf, int nBufLen)
387 struct emp_qelem *qp;
388 struct emp_qelem *head;
397 head = &ioq->list.queue;
399 for (qp = head->q_forw; (qp != head) && (nleft > 0); qp = qp->q_forw) {
400 io = (struct io *)qp;
401 nbytes = io->nbytes - io->offset;
403 /* Paranoid check for bad buffer. */
407 /* too many bytes, wait till next time. */
411 memcpy(offset, io->data + io->offset, nbytes);