2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2007, 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 static int ioqtocbuf(struct ioqueue *ioq, char *buf, int cc, int stopc);
51 static int ioqtoiov(struct ioqueue *ioq, struct iovec *iov, int max);
52 static int ioqtobuf(struct ioqueue *ioq, char *buf, int cc);
53 static int appendcc(struct ioqueue *ioq, char *buf, int cc);
54 static int removecc(struct ioqueue *ioq, int cc);
61 ioq = malloc(sizeof(struct ioqueue));
62 emp_initque(&ioq->list.queue);
73 ioq_destroy(struct ioqueue *ioq)
80 ioq_drain(struct ioqueue *ioq)
85 while ((qp = ioq->list.queue.q_forw) != &ioq->list.queue) {
87 emp_remque(&io->queue);
96 * copy batch of pointers into the passed
97 * iovec, but don't actually dequeue the data.
98 * return # of iovec initialized.
101 ioq_makeiov(struct ioqueue *ioq, struct iovec *iov, int cc)
105 return ioqtoiov(ioq, iov, cc);
109 * Copy the specified number of characters into the buffer
110 * provided, without actually dequeueing the data. Return
111 * number of bytes actually found.
114 ioq_peek(struct ioqueue *ioq, char *buf, int cc)
116 return ioqtobuf(ioq, buf, cc);
120 ioq_dequeue(struct ioqueue *ioq, int cc)
122 return removecc(ioq, cc);
126 ioq_append(struct ioqueue *ioq, char *buf, int cc)
128 appendcc(ioq, buf, cc);
132 ioq_qsize(struct ioqueue *ioq)
138 * read a line of text up to (but not including)
139 * the newline. return -1 and read nothing if
140 * no input is available
143 ioq_gets(struct ioqueue *ioq, char *buf, int cc)
148 nbytes = ioqtocbuf(ioq, buf, cc - 1, '\n');
153 /* telnet terminates lines with "\r\n", get rid of \r */
154 if (actual > 0 && buf[actual-1] == '\r')
157 /* remove the newline too */
158 removecc(ioq, nbytes + 1);
164 ioq_puts(struct ioqueue *ioq, char *buf)
166 return appendcc(ioq, buf, strlen(buf));
170 * copy cc bytes from ioq to buf.
171 * this routine doesn't free memory; this is
172 * left for a higher level.
175 ioqtobuf(struct ioqueue *ioq, char *buf, int cc)
178 struct emp_qelem *qp;
179 struct emp_qelem *head;
185 head = &ioq->list.queue;
186 for (qp = head->q_forw; qp != head && nleft > 0; qp = qp->q_forw) {
187 io = (struct io *)qp;
188 if ((nbytes = io->nbytes - io->offset) < 0) {
189 /* XXX log something here */
195 memcpy(offset, io->data + io->offset, nbytes);
204 * copy at most cc bytes from ioq to buf,
205 * terminating on the stop character.
208 ioqtocbuf(struct ioqueue *ioq, char *buf, int cc, int stopc)
214 struct emp_qelem *qp;
215 struct emp_qelem *head;
219 head = &ioq->list.queue;
222 for (qp = head->q_forw; qp != head; qp = qp->q_forw) {
223 io = (struct io *)qp;
224 if ((nbytes = io->nbytes - io->offset) <= 0)
226 p = io->data + io->offset;
227 for (n = 0; n < nbytes && p[n] != stopc; n++) ;
236 ioqtobuf(ioq, buf, cc < total ? cc : total);
241 * initialize an iovec to point at max bytes worth
242 * of data from the ioqueue.
245 ioqtoiov(struct ioqueue *ioq, struct iovec *iov, int max)
249 struct emp_qelem *qp;
253 qp = ioq->list.queue.q_forw;
254 while (qp != &ioq->list.queue && cc > 0) {
255 io = (struct io *)qp;
256 len = io->nbytes - io->offset;
259 iov->iov_base = io->data + io->offset;
272 * append a buffer to the end of the ioq.
275 appendcc(struct ioqueue *ioq, char *buf, int cc)
282 /* determine if any space is left */
283 io = (struct io *)ioq->list.queue.q_back;
284 avail = io->size - io->nbytes;
286 /* append to existing buffer */
287 len = cc > avail ? avail : cc;
288 memcpy(io->data + io->nbytes, buf, len);
292 appendcc(ioq, buf + len, cc - len);
294 /* create a new buffer, minimum bufsize bytes */
295 len = cc > ioq->bufsize ? cc : ioq->bufsize;
297 memcpy(ptr, buf, cc);
298 io = malloc(sizeof(struct io));
303 emp_insque(&io->queue, ioq->list.queue.q_back);
310 * remove cc bytes from ioqueue ioq
311 * free memory, dequeue io elements
312 * which are no longer used.
315 removecc(struct ioqueue *ioq, int cc)
318 struct emp_qelem *qp;
319 int nbytes, there, remain;
323 while ((qp = ioq->list.queue.q_forw) != &ioq->list.queue) {
324 io = (struct io *)qp;
325 there = io->nbytes - io->offset;
328 emp_remque(&io->queue);
332 if (remain >= there) {
333 /* not enough or exact; free entry */
336 emp_remque(&io->queue);
340 /* too much; increment offset */
341 io->offset += remain;