]> git.pond.sub.org Git - empserver/blob - src/lib/gen/ioqueue.c
Change ioq_dequeue() to return void
[empserver] / src / lib / gen / ioqueue.c
1 /*
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
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  ioqueue.c: Read and write i/o queues
29  *
30  *  Known contributors to this file:
31  *
32  */
33
34 /*
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.
38  */
39
40 #include <config.h>
41
42 #include <stdlib.h>
43 #include <string.h>
44 #include <sys/uio.h>
45 #include "ioqueue.h"
46 #include "misc.h"
47 #include "queue.h"
48
49 struct io {
50     struct emp_qelem queue;
51     int size;
52     int nbytes;
53     int offset;
54     char *data;
55 };
56
57 struct ioqueue {
58     struct io list;
59     int bufsize;
60     int cc;
61 };
62
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);
68
69 struct ioqueue *
70 ioq_create(int size)
71 {
72     struct ioqueue *ioq;
73
74     ioq = malloc(sizeof(struct ioqueue));
75     emp_initque(&ioq->list.queue);
76     ioq->list.nbytes = 0;
77     ioq->list.offset = 0;
78     ioq->list.size = 0;
79     ioq->list.data = NULL;
80     ioq->bufsize = size;
81     ioq->cc = 0;
82     return ioq;
83 }
84
85 void
86 ioq_destroy(struct ioqueue *ioq)
87 {
88     ioq_drain(ioq);
89     free(ioq);
90 }
91
92 void
93 ioq_drain(struct ioqueue *ioq)
94 {
95     struct emp_qelem *qp;
96     struct io *io;
97
98     while ((qp = ioq->list.queue.q_forw) != &ioq->list.queue) {
99         io = (struct io *)qp;
100         emp_remque(&io->queue);
101         free(io->data);
102         free(io);
103     }
104
105     ioq->cc = 0;
106 }
107
108 /*
109  * copy batch of pointers into the passed
110  * iovec, but don't actually dequeue the data.
111  * return # of iovec initialized.
112  */
113 int
114 ioq_makeiov(struct ioqueue *ioq, struct iovec *iov, int cc)
115 {
116     if (ioq->cc <= 0)
117         return 0;
118     return ioqtoiov(ioq, iov, cc);
119 }
120
121 /*
122  * Copy the specified number of characters into the buffer
123  * provided, without actually dequeueing the data.  Return
124  * number of bytes actually found.
125  */
126 int
127 ioq_peek(struct ioqueue *ioq, char *buf, int cc)
128 {
129     return ioqtobuf(ioq, buf, cc);
130 }
131
132 void
133 ioq_dequeue(struct ioqueue *ioq, int cc)
134 {
135     int res = removecc(ioq, cc);
136     CANT_HAPPEN(res != cc);
137 }
138
139 void
140 ioq_append(struct ioqueue *ioq, char *buf, int cc)
141 {
142     appendcc(ioq, buf, cc);
143 }
144
145 int
146 ioq_qsize(struct ioqueue *ioq)
147 {
148     return ioq->cc;
149 }
150
151 /*
152  * read a line of text up to (but not including)
153  * the newline.  return -1 and read nothing if
154  * no input is available
155  */
156 int
157 ioq_gets(struct ioqueue *ioq, char *buf, int cc)
158 {
159     int nbytes;
160     int actual;
161
162     nbytes = ioqtocbuf(ioq, buf, cc - 1, '\n');
163     if (nbytes >= 0) {
164         actual = nbytes;
165         if (actual > cc - 1)
166             actual = cc - 1;
167         /* telnet terminates lines with "\r\n", get rid of \r */
168         if (actual > 0 && buf[actual-1] == '\r')
169             actual--;
170         buf[actual] = '\0';
171         /* remove the newline too */
172         removecc(ioq, nbytes + 1);
173     }
174     return nbytes;
175 }
176
177 int
178 ioq_puts(struct ioqueue *ioq, char *buf)
179 {
180     return appendcc(ioq, buf, strlen(buf));
181 }
182
183 /*
184  * copy cc bytes from ioq to buf.
185  * this routine doesn't free memory; this is
186  * left for a higher level.
187  */
188 static int
189 ioqtobuf(struct ioqueue *ioq, char *buf, int cc)
190 {
191     struct io *io;
192     struct emp_qelem *qp;
193     struct emp_qelem *head;
194     int nbytes, nleft;
195     char *offset;
196
197     nleft = cc;
198     offset = buf;
199     head = &ioq->list.queue;
200     for (qp = head->q_forw; qp != head && nleft > 0; qp = qp->q_forw) {
201         io = (struct io *)qp;
202         if ((nbytes = io->nbytes - io->offset) < 0) {
203             /* XXX log something here */
204             continue;
205         }
206         if (nbytes > 0) {
207             if (nleft < nbytes)
208                 nbytes = nleft;
209             memcpy(offset, io->data + io->offset, nbytes);
210             offset += nbytes;
211             nleft -= nbytes;
212         }
213     }
214     return offset - buf;
215 }
216
217 /*
218  * copy at most cc bytes from ioq to buf,
219  * terminating on the stop character.
220  */
221 static int
222 ioqtocbuf(struct ioqueue *ioq, char *buf, int cc, int stopc)
223 {
224     int nbytes;
225     char *p;
226     int n;
227     struct io *io;
228     struct emp_qelem *qp;
229     struct emp_qelem *head;
230     int total;
231     int found;
232
233     head = &ioq->list.queue;
234     found = 0;
235     total = 0;
236     for (qp = head->q_forw; qp != head; qp = qp->q_forw) {
237         io = (struct io *)qp;
238         if ((nbytes = io->nbytes - io->offset) <= 0)
239             continue;
240         p = io->data + io->offset;
241         for (n = 0; n < nbytes && p[n] != stopc; n++) ;
242         total += n;
243         if (n < nbytes) {
244             found++;
245             break;
246         }
247     }
248     if (found == 0)
249         return -1;
250     ioqtobuf(ioq, buf, cc < total ? cc : total);
251     return total;
252 }
253
254 /*
255  * initialize an iovec to point at max bytes worth
256  * of data from the ioqueue.
257  */
258 static int
259 ioqtoiov(struct ioqueue *ioq, struct iovec *iov, int max)
260 {
261     struct io *io;
262     int cc, niov, len;
263     struct emp_qelem *qp;
264
265     cc = max;
266     niov = 0;
267     qp = ioq->list.queue.q_forw;
268     while (qp != &ioq->list.queue && cc > 0) {
269         io = (struct io *)qp;
270         len = io->nbytes - io->offset;
271         if (len > cc)
272             len = cc;
273         iov->iov_base = io->data + io->offset;
274         iov->iov_len = len;
275         cc -= len;
276         niov++;
277         iov++;
278         qp = qp->q_forw;
279         if (niov >= 16)
280             break;
281     }
282     return niov;
283 }
284
285 /*
286  * append a buffer to the end of the ioq.
287  */
288 static int
289 appendcc(struct ioqueue *ioq, char *buf, int cc)
290 {
291     struct io *io;
292     int len;
293     char *ptr;
294     int avail;
295
296     /* determine if any space is left */
297     io = (struct io *)ioq->list.queue.q_back;
298     avail = io->size - io->nbytes;
299     if (avail > 0) {
300         /* append to existing buffer */
301         len = cc > avail ? avail : cc;
302         memcpy(io->data + io->nbytes, buf, len);
303         io->nbytes += len;
304         ioq->cc += len;
305         if (avail < cc)
306             appendcc(ioq, buf + len, cc - len);
307     } else {
308         /* create a new buffer, minimum bufsize bytes */
309         len = cc > ioq->bufsize ? cc : ioq->bufsize;
310         ptr = malloc(len);
311         memcpy(ptr, buf, cc);
312         io = malloc(sizeof(struct io));
313         io->nbytes = cc;
314         io->size = len;
315         io->offset = 0;
316         io->data = ptr;
317         emp_insque(&io->queue, ioq->list.queue.q_back);
318         ioq->cc += cc;
319     }
320     return cc;
321 }
322
323 /*
324  * remove cc bytes from ioqueue ioq
325  * free memory, dequeue io elements
326  * which are no longer used.
327  */
328 static int
329 removecc(struct ioqueue *ioq, int cc)
330 {
331     struct io *io;
332     struct emp_qelem *qp;
333     int nbytes, there, remain;
334
335     nbytes = 0;
336     remain = cc;
337     while ((qp = ioq->list.queue.q_forw) != &ioq->list.queue) {
338         io = (struct io *)qp;
339         there = io->nbytes - io->offset;
340         if (there < 0) {
341             /* error */
342             emp_remque(&io->queue);
343             free(io);
344             continue;
345         }
346         if (remain >= there) {
347             /* not enough or exact; free entry */
348             nbytes += there;
349             remain -= there;
350             emp_remque(&io->queue);
351             free(io->data);
352             free(io);
353         } else {
354             /* too much; increment offset */
355             io->offset += remain;
356             nbytes += remain;
357             remain = 0;
358         }
359         if (remain <= 0)
360             break;
361     }
362     ioq->cc -= nbytes;
363     return nbytes;
364 }