2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2010, 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 int res = removecc(ioq, cc);
136 CANT_HAPPEN(res != cc);
141 ioq_append(struct ioqueue *ioq, char *buf, int cc)
143 appendcc(ioq, buf, cc);
147 ioq_qsize(struct ioqueue *ioq)
153 * read a line of text up to (but not including)
154 * the newline. return -1 and read nothing if
155 * no input is available
158 ioq_gets(struct ioqueue *ioq, char *buf, int cc)
163 nbytes = ioqtocbuf(ioq, buf, cc - 1, '\n');
168 /* telnet terminates lines with "\r\n", get rid of \r */
169 if (actual > 0 && buf[actual-1] == '\r')
172 /* remove the newline too */
173 removecc(ioq, nbytes + 1);
179 ioq_puts(struct ioqueue *ioq, char *buf)
181 return appendcc(ioq, buf, strlen(buf));
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, char *buf, int cc)
193 struct emp_qelem *qp;
194 struct emp_qelem *head;
200 head = &ioq->list.queue;
201 for (qp = head->q_forw; qp != head && nleft > 0; qp = qp->q_forw) {
202 io = (struct io *)qp;
203 if ((nbytes = io->nbytes - io->offset) < 0) {
204 /* XXX log something here */
210 memcpy(offset, io->data + io->offset, nbytes);
219 * copy at most cc bytes from ioq to buf,
220 * terminating on the stop character.
223 ioqtocbuf(struct ioqueue *ioq, char *buf, int cc, int stopc)
229 struct emp_qelem *qp;
230 struct emp_qelem *head;
234 head = &ioq->list.queue;
237 for (qp = head->q_forw; qp != head; qp = qp->q_forw) {
238 io = (struct io *)qp;
239 if ((nbytes = io->nbytes - io->offset) <= 0)
241 p = io->data + io->offset;
242 for (n = 0; n < nbytes && p[n] != stopc; n++) ;
251 ioqtobuf(ioq, buf, cc < total ? cc : total);
256 * initialize an iovec to point at max bytes worth
257 * of data from the ioqueue.
260 ioqtoiov(struct ioqueue *ioq, struct iovec *iov, int max)
264 struct emp_qelem *qp;
268 qp = ioq->list.queue.q_forw;
269 while (qp != &ioq->list.queue && cc > 0) {
270 io = (struct io *)qp;
271 len = io->nbytes - io->offset;
274 iov->iov_base = io->data + io->offset;
287 * append a buffer to the end of the ioq.
290 appendcc(struct ioqueue *ioq, char *buf, int cc)
297 /* determine if any space is left */
298 io = (struct io *)ioq->list.queue.q_back;
299 avail = io->size - io->nbytes;
301 /* append to existing buffer */
302 len = cc > avail ? avail : cc;
303 memcpy(io->data + io->nbytes, buf, len);
307 appendcc(ioq, buf + len, cc - len);
309 /* create a new buffer, minimum bufsize bytes */
310 len = cc > ioq->bufsize ? cc : ioq->bufsize;
312 memcpy(ptr, buf, cc);
313 io = malloc(sizeof(struct io));
318 emp_insque(&io->queue, ioq->list.queue.q_back);
325 * remove cc bytes from ioqueue ioq
326 * free memory, dequeue io elements
327 * which are no longer used.
330 removecc(struct ioqueue *ioq, int cc)
333 struct emp_qelem *qp;
334 int nbytes, there, remain;
338 while ((qp = ioq->list.queue.q_forw) != &ioq->list.queue) {
339 io = (struct io *)qp;
340 there = io->nbytes - io->offset;
343 emp_remque(&io->queue);
347 if (remain >= there) {
348 /* not enough or exact; free entry */
351 emp_remque(&io->queue);
355 /* too much; increment offset */
356 io->offset += remain;