3 * Packet-buffering select handler
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
31 #include <sys/types.h>
39 /*----- Main code ---------------------------------------------------------*/
41 /* --- @selpk_enable@ --- *
43 * Arguments: @selpk *pk@ = pointer to buffer block
47 * Use: Enables a buffer for reading, and emits any queued packets
48 * to the buffer's owner.
51 void selpk_enable(selpk *pk)
53 if (!(pk->pk.f & PKBUF_ENABLE)) {
54 pk->pk.f |= PKBUF_ENABLE;
55 sel_addfile(&pk->reader);
56 pkbuf_flush(&pk->pk, 0, 0);
60 /* --- @selpk_disable@ --- *
62 * Arguments: @selpk *pk@ = pointer to a buffer block
66 * Use: Disables a buffer. It won't be read from until it's
70 void selpk_disable(selpk *pk)
72 if (pk->pk.f & PKBUF_ENABLE) {
73 pk->pk.f &= ~PKBUF_ENABLE;
74 sel_rmfile(&pk->reader);
78 /* --- @selpk_read@ --- *
80 * Arguments: @int fd@ = file descriptor to read from
81 * @int mode@ = what we can do to the file
82 * @void *vp@ = pointer to buffer context
86 * Use: Acts on the result of a @select@ call.
89 static void selpk_read(int fd, unsigned mode, void *vp)
96 sz = pkbuf_free(&pk->pk, &p);
101 pkbuf_flush(&pk->pk, p, n);
102 else switch (errno) {
105 #if EAGAIN != EWOULDBLOCK
110 pkbuf_close(&pk->pk);
114 /* --- @selpk_want@ --- *
116 * Arguments: @selpk *pk@ = pointer to buffer block
117 * @size_t sz@ = size of buffer
121 * Use: Sets the size of the packet to be read next.
124 void selpk_want(selpk *pk, size_t sz)
126 pkbuf_want(&pk->pk, sz);
129 /* --- @selpk_init@ --- *
131 * Arguments: @selpk *pk@ = pointer to buffer block
132 * @sel_state *s@ = pointer to select state to attach to
133 * @int fd@ = file descriptor to listen to
134 * @pkbuf_func *func@ = function to call
135 * @void *p@ = argument for function
139 * Use: Initializes a buffer block.
142 void selpk_init(selpk *pk, sel_state *s, int fd, pkbuf_func *func, void *p)
144 pkbuf_init(&pk->pk, func, p);
145 pk->pk.f &= ~PKBUF_ENABLE;
146 sel_initfile(s, &pk->reader, fd, SEL_READ, selpk_read, pk);
150 /* --- @selpk_destroy@ --- *
152 * Arguments: @selpk *pk@ = pointer to buffer block
156 * Use: Deallocates a packet buffer and frees any resources it owned.
159 void selpk_destroy(selpk *pk)
162 pkbuf_destroy(&pk->pk);
165 /*----- That's all, folks -------------------------------------------------*/