3 * $Id: selpk.c,v 1.1 2000/06/17 10:39:19 mdw Exp $
5 * Packet-buffering select handler
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.1 2000/06/17 10:39:19 mdw
34 * Experimental new support for packet buffering.
38 /*----- Header files ------------------------------------------------------*/
45 #include <sys/types.h>
53 /*----- Main code ---------------------------------------------------------*/
55 /* --- @selpk_enable@ --- *
57 * Arguments: @selpk *pk@ = pointer to buffer block
61 * Use: Enables a buffer for reading, and emits any queued packets
62 * to the buffer's owner.
65 void selpk_enable(selpk *pk)
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);
74 /* --- @selpk_disable@ --- *
76 * Arguments: @selpk *pk@ = pointer to a buffer block
80 * Use: Disables a buffer. It won't be read from until it's
84 void selpk_disable(selpk *pk)
86 if (pk->pk.f & PKBUF_ENABLE) {
87 pk->pk.f &= ~PKBUF_ENABLE;
88 sel_rmfile(&pk->reader);
92 /* --- @selpk_read@ --- *
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
100 * Use: Acts on the result of a @select@ call.
103 static void selpk_read(int fd, unsigned mode, void *vp)
110 sz = pkbuf_free(&pk->pk, &p);
113 pkbuf_close(&pk->pk);
115 pkbuf_flush(&pk->pk, p, n);
116 else switch (errno) {
119 #if EAGAIN != EWOULDBLOCK
124 pkbuf_close(&pk->pk);
128 /* --- @selpk_want@ --- *
130 * Arguments: @selpk *pk@ = pointer to buffer block
131 * @size_t sz@ = size of buffer
135 * Use: Sets the size of the packet to be read next.
138 void selpk_want(selpk *pk, size_t sz)
140 pkbuf_want(&pk->pk, sz);
143 /* --- @selpk_init@ --- *
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
153 * Use: Initializes a buffer block.
156 void selpk_init(selpk *pk,
159 void (*func)(octet */*b*/, size_t /*sz*/, pkbuf */*pk*/,
160 size_t */*keep*/, void */*p*/),
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);
169 /* --- @selpk_destroy@ --- *
171 * Arguments: @selpk *pk@ = pointer to buffer block
175 * Use: Deallocates a packet buffer and frees any resources it owned.
178 void selpk_destroy(selpk *pk)
181 pkbuf_destroy(&pk->pk);
184 /*----- That's all, folks -------------------------------------------------*/