]> git.pond.sub.org Git - empserver/blob - src/client/ioqueue.c
e1caac94ce4293cde02de51eb1b30b4fcb892583
[empserver] / src / client / ioqueue.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  ioqueue.c: Manage an i/o queue
29  * 
30  *  Known contributors to this file:
31  *     Steve McClure, 1998
32  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/types.h>
38 #include "misc.h"
39 #include "queue.h"
40 #include "ioqueue.h"
41
42 static int ioqtobuf(register struct ioqueue *ioq, s_char *buf, int cc);
43 static void enqueuecc(struct ioqueue *ioq, s_char *buf, int cc);
44 static int dequeuecc(register struct ioqueue *ioq, register int cc);
45
46
47 void
48 ioq_init(struct ioqueue *ioq, int bsize)
49 {
50     extern s_char num_teles[];
51
52     initque(&ioq->queue);
53     ioq->cc = 0;
54     ioq->bsize = bsize;
55     *num_teles = '\0';
56 }
57
58 /*
59  * Copy the specified number of characters into the buffer
60  * provided, without actually dequeueing the data.  Return
61  * number of bytes actually found.
62  */
63 int
64 ioq_peek(struct ioqueue *ioq, s_char *buf, int cc)
65 {
66     return ioqtobuf(ioq, buf, cc);
67 }
68
69 int
70 ioq_dequeue(struct ioqueue *ioq, int cc)
71 {
72     if (dequeuecc(ioq, cc) != cc)
73         return 0;
74     return cc;
75 }
76
77 int
78 ioq_read(struct ioqueue *ioq, s_char *buf, int cc)
79 {
80     int n;
81
82     n = ioqtobuf(ioq, buf, cc);
83     if (n > 0)
84         dequeuecc(ioq, n);
85     return n;
86 }
87
88 void
89 ioq_write(struct ioqueue *ioq, s_char *buf, int cc)
90 {
91     enqueuecc(ioq, buf, cc);
92 }
93
94 int
95 ioq_qsize(struct ioqueue *ioq)
96 {
97     return ioq->cc;
98 }
99
100 void
101 ioq_drain(struct ioqueue *ioq)
102 {
103     struct io *io;
104     struct qelem *qp;
105
106     while ((qp = ioq->queue.q_forw) != &ioq->queue) {
107         io = (struct io *)qp;
108         free(io->data);
109         (void)remque(&io->queue);
110         (void)free(io);
111     }
112     ioq->cc = 0;
113 }
114
115 s_char *
116 ioq_gets(struct ioqueue *ioq, s_char *buf, int cc)
117 {
118     register s_char *p;
119     register s_char *end;
120     int nbytes;
121
122     nbytes = ioqtobuf(ioq, buf, cc);
123     if (nbytes < cc)
124         cc = nbytes;
125     end = &buf[cc];
126     for (p = buf; p < end && *p; p++) {
127         if (*p == '\n') {
128             *p = '\0';
129             dequeuecc(ioq, (p - buf) + 1);
130             return buf;
131         }
132     }
133     return 0;
134 }
135
136 /*
137  * all the rest are local to this module
138  */
139
140
141 /*
142  * copy cc bytes from ioq to buf.
143  * this routine doesn't free memory; this is
144  * left for a higher level.
145  */
146 static int
147 ioqtobuf(register struct ioqueue *ioq, s_char *buf, int cc)
148 {
149     register struct io *io;
150     struct qelem *qp;
151     s_char *offset;
152     int nbytes;
153     int nleft;
154
155     nleft = cc;
156     offset = buf;
157     for (qp = ioq->queue.q_forw; qp != &ioq->queue; qp = qp->q_forw) {
158         io = (struct io *)qp;
159         if ((nbytes = io->nbytes - io->offset) < 0) {
160             fprintf(stderr, "ioqtobuf: offset %d nbytes %d\n",
161                     io->offset, io->nbytes);
162             continue;
163         }
164         if (nbytes > 0) {
165             if (nleft < nbytes)
166                 nbytes = nleft;
167             memcpy(offset, io->data + io->offset, nbytes);
168             offset += nbytes;
169             nleft -= nbytes;
170         }
171     }
172     return offset - buf;
173 }
174
175 /*
176  * append a buffer to the end of the ioq.
177  */
178 static void
179 enqueuecc(struct ioqueue *ioq, s_char *buf, int cc)
180 {
181     struct io *io;
182
183     io = (struct io *)malloc(sizeof(*io));
184     io->nbytes = cc;
185     io->offset = 0;
186     io->data = buf;
187     insque(&io->queue, ioq->queue.q_back);
188     ioq->cc += cc;
189 }
190
191 /*
192  * remove cc bytes from ioqueue ioq
193  * free memory, dequeue io elements
194  * which are no longer used.
195  */
196 static int
197 dequeuecc(register struct ioqueue *ioq, register int cc)
198 {
199     register struct io *io;
200     register struct qelem *qp;
201     register int nbytes;
202     register int there;
203
204     nbytes = 0;
205     while ((qp = ioq->queue.q_forw) != &ioq->queue) {
206         io = (struct io *)qp;
207         there = io->nbytes - io->offset;
208         if (there < 0) {
209             fprintf(stderr, "dequeuecc: nbytes %d, offset %d\n",
210                     io->nbytes, io->offset);
211             continue;
212         }
213         if (cc > there) {
214             cc -= there;
215             nbytes += there;
216             (void)remque(&io->queue);
217             free(io->data);
218         } else {
219             io->offset += cc;
220             nbytes += cc;
221             break;
222         }
223     }
224     ioq->cc -= nbytes;
225     return nbytes;
226 }