2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2009, 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.
50 struct emp_qelem queue;
63 static int ioqtocbuf(struct ioqueue *ioq, char *buf, int cc, int stopc);
64 static int ioqtoiov(struct ioqueue *ioq, struct iovec *iov, int max);
65 static int ioqtobuf(struct ioqueue *ioq, char *buf, int cc);
66 static int appendcc(struct ioqueue *ioq, char *buf, int cc);
67 static int removecc(struct ioqueue *ioq, int cc);
74 ioq = malloc(sizeof(struct ioqueue));
75 emp_initque(&ioq->list.queue);
79 ioq->list.data = NULL;
86 ioq_destroy(struct ioqueue *ioq)
93 ioq_drain(struct ioqueue *ioq)
98 while ((qp = ioq->list.queue.q_forw) != &ioq->list.queue) {
100 emp_remque(&io->queue);
109 * copy batch of pointers into the passed
110 * iovec, but don't actually dequeue the data.
111 * return # of iovec initialized.
114 ioq_makeiov(struct ioqueue *ioq, struct iovec *iov, int cc)
118 return ioqtoiov(ioq, iov, cc);
122 * Copy the specified number of characters into the buffer
123 * provided, without actually dequeueing the data. Return
124 * number of bytes actually found.
127 ioq_peek(struct ioqueue *ioq, char *buf, int cc)
129 return ioqtobuf(ioq, buf, cc);
133 ioq_dequeue(struct ioqueue *ioq, int cc)
135 return removecc(ioq, cc);
139 ioq_append(struct ioqueue *ioq, char *buf, int cc)
141 appendcc(ioq, buf, cc);
145 ioq_qsize(struct ioqueue *ioq)
151 * read a line of text up to (but not including)
152 * the newline. return -1 and read nothing if
153 * no input is available
156 ioq_gets(struct ioqueue *ioq, char *buf, int cc)
161 nbytes = ioqtocbuf(ioq, buf, cc - 1, '\n');
166 /* telnet terminates lines with "\r\n", get rid of \r */
167 if (actual > 0 && buf[actual-1] == '\r')
170 /* remove the newline too */
171 removecc(ioq, nbytes + 1);
177 ioq_puts(struct ioqueue *ioq, char *buf)
179 return appendcc(ioq, buf, strlen(buf));
183 * copy cc bytes from ioq to buf.
184 * this routine doesn't free memory; this is
185 * left for a higher level.
188 ioqtobuf(struct ioqueue *ioq, char *buf, int cc)
191 struct emp_qelem *qp;
192 struct emp_qelem *head;
198 head = &ioq->list.queue;
199 for (qp = head->q_forw; qp != head && nleft > 0; qp = qp->q_forw) {
200 io = (struct io *)qp;
201 if ((nbytes = io->nbytes - io->offset) < 0) {
202 /* XXX log something here */
208 memcpy(offset, io->data + io->offset, nbytes);
217 * copy at most cc bytes from ioq to buf,
218 * terminating on the stop character.
221 ioqtocbuf(struct ioqueue *ioq, char *buf, int cc, int stopc)
227 struct emp_qelem *qp;
228 struct emp_qelem *head;
232 head = &ioq->list.queue;
235 for (qp = head->q_forw; qp != head; qp = qp->q_forw) {
236 io = (struct io *)qp;
237 if ((nbytes = io->nbytes - io->offset) <= 0)
239 p = io->data + io->offset;
240 for (n = 0; n < nbytes && p[n] != stopc; n++) ;
249 ioqtobuf(ioq, buf, cc < total ? cc : total);
254 * initialize an iovec to point at max bytes worth
255 * of data from the ioqueue.
258 ioqtoiov(struct ioqueue *ioq, struct iovec *iov, int max)
262 struct emp_qelem *qp;
266 qp = ioq->list.queue.q_forw;
267 while (qp != &ioq->list.queue && cc > 0) {
268 io = (struct io *)qp;
269 len = io->nbytes - io->offset;
272 iov->iov_base = io->data + io->offset;
285 * append a buffer to the end of the ioq.
288 appendcc(struct ioqueue *ioq, char *buf, int cc)
295 /* determine if any space is left */
296 io = (struct io *)ioq->list.queue.q_back;
297 avail = io->size - io->nbytes;
299 /* append to existing buffer */
300 len = cc > avail ? avail : cc;
301 memcpy(io->data + io->nbytes, buf, len);
305 appendcc(ioq, buf + len, cc - len);
307 /* create a new buffer, minimum bufsize bytes */
308 len = cc > ioq->bufsize ? cc : ioq->bufsize;
310 memcpy(ptr, buf, cc);
311 io = malloc(sizeof(struct io));
316 emp_insque(&io->queue, ioq->list.queue.q_back);
323 * remove cc bytes from ioqueue ioq
324 * free memory, dequeue io elements
325 * which are no longer used.
328 removecc(struct ioqueue *ioq, int cc)
331 struct emp_qelem *qp;
332 int nbytes, there, remain;
336 while ((qp = ioq->list.queue.q_forw) != &ioq->list.queue) {
337 io = (struct io *)qp;
338 there = io->nbytes - io->offset;
341 emp_remque(&io->queue);
345 if (remain >= there) {
346 /* not enough or exact; free entry */
349 emp_remque(&io->queue);
353 /* too much; increment offset */
354 io->offset += remain;