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