Experimental new support for packet buffering.
[mLib] / selpk.c
1 /* -*-c-*-
2 *
3 * $Id: selpk.c,v 1.1 2000/06/17 10:39:19 mdw Exp $
4 *
5 * Packet-buffering select handler
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the mLib utilities library.
13 *
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: selpk.c,v $
33 * Revision 1.1 2000/06/17 10:39:19 mdw
34 * Experimental new support for packet buffering.
35 *
36 */
37
38 /*----- Header files ------------------------------------------------------*/
39
40 #include <errno.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44
45 #include <sys/types.h>
46 #include <sys/time.h>
47 #include <unistd.h>
48
49 #include "pkbuf.h"
50 #include "sel.h"
51 #include "selpk.h"
52
53 /*----- Main code ---------------------------------------------------------*/
54
55 /* --- @selpk_enable@ --- *
56 *
57 * Arguments: @selpk *pk@ = pointer to buffer block
58 *
59 * Returns: ---
60 *
61 * Use: Enables a buffer for reading, and emits any queued packets
62 * to the buffer's owner.
63 */
64
65 void selpk_enable(selpk *pk)
66 {
67 if (!(pk->pk.f & PKBUF_ENABLE)) {
68 pk->pk.f |= PKBUF_ENABLE;
69 sel_addfile(&pk->reader);
70 pkbuf_flush(&pk->pk, 0, 0);
71 }
72 }
73
74 /* --- @selpk_disable@ --- *
75 *
76 * Arguments: @selpk *pk@ = pointer to a buffer block
77 *
78 * Returns: ---
79 *
80 * Use: Disables a buffer. It won't be read from until it's
81 * enabled again.
82 */
83
84 void selpk_disable(selpk *pk)
85 {
86 if (pk->pk.f & PKBUF_ENABLE) {
87 pk->pk.f &= ~PKBUF_ENABLE;
88 sel_rmfile(&pk->reader);
89 }
90 }
91
92 /* --- @selpk_read@ --- *
93 *
94 * Arguments: @int fd@ = file descriptor to read from
95 * @int mode@ = what we can do to the file
96 * @void *vp@ = pointer to buffer context
97 *
98 * Returns: ---
99 *
100 * Use: Acts on the result of a @select@ call.
101 */
102
103 static void selpk_read(int fd, unsigned mode, void *vp)
104 {
105 selpk *pk = vp;
106 octet *p;
107 size_t sz;
108 int n;
109
110 sz = pkbuf_free(&pk->pk, &p);
111 n = read(fd, p, sz);
112 if (n == 0)
113 pkbuf_close(&pk->pk);
114 else if (n > 0)
115 pkbuf_flush(&pk->pk, p, n);
116 else switch (errno) {
117 case EINTR:
118 case EAGAIN:
119 #if EAGAIN != EWOULDBLOCK
120 case EWOULDBLOCK:
121 #endif
122 return;
123 default:
124 pkbuf_close(&pk->pk);
125 }
126 }
127
128 /* --- @selpk_want@ --- *
129 *
130 * Arguments: @selpk *pk@ = pointer to buffer block
131 * @size_t sz@ = size of buffer
132 *
133 * Returns: ---
134 *
135 * Use: Sets the size of the packet to be read next.
136 */
137
138 void selpk_want(selpk *pk, size_t sz)
139 {
140 pkbuf_want(&pk->pk, sz);
141 }
142
143 /* --- @selpk_init@ --- *
144 *
145 * Arguments: @selpk *pk@ = pointer to buffer block
146 * @sel_state *s@ = pointer to select state to attach to
147 * @int fd@ = file descriptor to listen to
148 * @void (*func)(char *s, void *p)@ = function to call
149 * @void *p@ = argument for function
150 *
151 * Returns: ---
152 *
153 * Use: Initializes a buffer block.
154 */
155
156 void selpk_init(selpk *pk,
157 sel_state *s,
158 int fd,
159 void (*func)(octet */*b*/, size_t /*sz*/, pkbuf */*pk*/,
160 size_t */*keep*/, void */*p*/),
161 void *p)
162 {
163 pkbuf_init(&pk->pk, func, p);
164 pk->pk.f &= ~PKBUF_ENABLE;
165 sel_initfile(s, &pk->reader, fd, SEL_READ, selpk_read, pk);
166 selpk_enable(pk);
167 }
168
169 /* --- @selpk_destroy@ --- *
170 *
171 * Arguments: @selpk *pk@ = pointer to buffer block
172 *
173 * Returns: ---
174 *
175 * Use: Deallocates a packet buffer and frees any resources it owned.
176 */
177
178 void selpk_destroy(selpk *pk)
179 {
180 selpk_disable(pk);
181 pkbuf_destroy(&pk->pk);
182 }
183
184 /*----- That's all, folks -------------------------------------------------*/