3 * $Id: selpk.c,v 1.2 2002/01/13 13:33:51 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.2 2002/01/13 13:33:51 mdw
34 * Packet handler functions now have a @typedef@ name.
36 * Revision 1.1 2000/06/17 10:39:19 mdw
37 * Experimental new support for packet buffering.
41 /*----- Header files ------------------------------------------------------*/
48 #include <sys/types.h>
56 /*----- Main code ---------------------------------------------------------*/
58 /* --- @selpk_enable@ --- *
60 * Arguments: @selpk *pk@ = pointer to buffer block
64 * Use: Enables a buffer for reading, and emits any queued packets
65 * to the buffer's owner.
68 void selpk_enable(selpk *pk)
70 if (!(pk->pk.f & PKBUF_ENABLE)) {
71 pk->pk.f |= PKBUF_ENABLE;
72 sel_addfile(&pk->reader);
73 pkbuf_flush(&pk->pk, 0, 0);
77 /* --- @selpk_disable@ --- *
79 * Arguments: @selpk *pk@ = pointer to a buffer block
83 * Use: Disables a buffer. It won't be read from until it's
87 void selpk_disable(selpk *pk)
89 if (pk->pk.f & PKBUF_ENABLE) {
90 pk->pk.f &= ~PKBUF_ENABLE;
91 sel_rmfile(&pk->reader);
95 /* --- @selpk_read@ --- *
97 * Arguments: @int fd@ = file descriptor to read from
98 * @int mode@ = what we can do to the file
99 * @void *vp@ = pointer to buffer context
103 * Use: Acts on the result of a @select@ call.
106 static void selpk_read(int fd, unsigned mode, void *vp)
113 sz = pkbuf_free(&pk->pk, &p);
116 pkbuf_close(&pk->pk);
118 pkbuf_flush(&pk->pk, p, n);
119 else switch (errno) {
122 #if EAGAIN != EWOULDBLOCK
127 pkbuf_close(&pk->pk);
131 /* --- @selpk_want@ --- *
133 * Arguments: @selpk *pk@ = pointer to buffer block
134 * @size_t sz@ = size of buffer
138 * Use: Sets the size of the packet to be read next.
141 void selpk_want(selpk *pk, size_t sz)
143 pkbuf_want(&pk->pk, sz);
146 /* --- @selpk_init@ --- *
148 * Arguments: @selpk *pk@ = pointer to buffer block
149 * @sel_state *s@ = pointer to select state to attach to
150 * @int fd@ = file descriptor to listen to
151 * @pkbuf_func *func@ = function to call
152 * @void *p@ = argument for function
156 * Use: Initializes a buffer block.
159 void selpk_init(selpk *pk, sel_state *s, int fd, pkbuf_func *func, void *p)
161 pkbuf_init(&pk->pk, func, p);
162 pk->pk.f &= ~PKBUF_ENABLE;
163 sel_initfile(s, &pk->reader, fd, SEL_READ, selpk_read, pk);
167 /* --- @selpk_destroy@ --- *
169 * Arguments: @selpk *pk@ = pointer to buffer block
173 * Use: Deallocates a packet buffer and frees any resources it owned.
176 void selpk_destroy(selpk *pk)
179 pkbuf_destroy(&pk->pk);
182 /*----- That's all, folks -------------------------------------------------*/