3 * $Id: selpk.c,v 1.3 2004/04/08 01:36:13 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 /*----- Header files ------------------------------------------------------*/
37 #include <sys/types.h>
45 /*----- Main code ---------------------------------------------------------*/
47 /* --- @selpk_enable@ --- *
49 * Arguments: @selpk *pk@ = pointer to buffer block
53 * Use: Enables a buffer for reading, and emits any queued packets
54 * to the buffer's owner.
57 void selpk_enable(selpk *pk)
59 if (!(pk->pk.f & PKBUF_ENABLE)) {
60 pk->pk.f |= PKBUF_ENABLE;
61 sel_addfile(&pk->reader);
62 pkbuf_flush(&pk->pk, 0, 0);
66 /* --- @selpk_disable@ --- *
68 * Arguments: @selpk *pk@ = pointer to a buffer block
72 * Use: Disables a buffer. It won't be read from until it's
76 void selpk_disable(selpk *pk)
78 if (pk->pk.f & PKBUF_ENABLE) {
79 pk->pk.f &= ~PKBUF_ENABLE;
80 sel_rmfile(&pk->reader);
84 /* --- @selpk_read@ --- *
86 * Arguments: @int fd@ = file descriptor to read from
87 * @int mode@ = what we can do to the file
88 * @void *vp@ = pointer to buffer context
92 * Use: Acts on the result of a @select@ call.
95 static void selpk_read(int fd, unsigned mode, void *vp)
102 sz = pkbuf_free(&pk->pk, &p);
105 pkbuf_close(&pk->pk);
107 pkbuf_flush(&pk->pk, p, n);
108 else switch (errno) {
111 #if EAGAIN != EWOULDBLOCK
116 pkbuf_close(&pk->pk);
120 /* --- @selpk_want@ --- *
122 * Arguments: @selpk *pk@ = pointer to buffer block
123 * @size_t sz@ = size of buffer
127 * Use: Sets the size of the packet to be read next.
130 void selpk_want(selpk *pk, size_t sz)
132 pkbuf_want(&pk->pk, sz);
135 /* --- @selpk_init@ --- *
137 * Arguments: @selpk *pk@ = pointer to buffer block
138 * @sel_state *s@ = pointer to select state to attach to
139 * @int fd@ = file descriptor to listen to
140 * @pkbuf_func *func@ = function to call
141 * @void *p@ = argument for function
145 * Use: Initializes a buffer block.
148 void selpk_init(selpk *pk, sel_state *s, int fd, pkbuf_func *func, void *p)
150 pkbuf_init(&pk->pk, func, p);
151 pk->pk.f &= ~PKBUF_ENABLE;
152 sel_initfile(s, &pk->reader, fd, SEL_READ, selpk_read, pk);
156 /* --- @selpk_destroy@ --- *
158 * Arguments: @selpk *pk@ = pointer to buffer block
162 * Use: Deallocates a packet buffer and frees any resources it owned.
165 void selpk_destroy(selpk *pk)
168 pkbuf_destroy(&pk->pk);
171 /*----- That's all, folks -------------------------------------------------*/