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 ------------------------------------------------------*/
35 #include <sys/types.h>
43 /*----- Main code ---------------------------------------------------------*/
45 /* --- @selpk_enable@ --- *
47 * Arguments: @selpk *pk@ = pointer to buffer block
51 * Use: Enables a buffer for reading, and emits any queued packets
52 * to the buffer's owner.
55 void selpk_enable(selpk
*pk
)
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);
64 /* --- @selpk_disable@ --- *
66 * Arguments: @selpk *pk@ = pointer to a buffer block
70 * Use: Disables a buffer. It won't be read from until it's
74 void selpk_disable(selpk
*pk
)
76 if (pk
->pk
.f
& PKBUF_ENABLE
) {
77 pk
->pk
.f
&= ~PKBUF_ENABLE
;
78 sel_rmfile(&pk
->reader
);
82 /* --- @selpk_read@ --- *
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
90 * Use: Acts on the result of a @select@ call.
93 static void selpk_read(int fd
, unsigned mode
, void *vp
)
100 sz
= pkbuf_free(&pk
->pk
, &p
);
103 pkbuf_close(&pk
->pk
);
105 pkbuf_flush(&pk
->pk
, p
, n
);
106 else switch (errno
) {
109 #if EAGAIN != EWOULDBLOCK
114 pkbuf_close(&pk
->pk
);
118 /* --- @selpk_want@ --- *
120 * Arguments: @selpk *pk@ = pointer to buffer block
121 * @size_t sz@ = size of buffer
125 * Use: Sets the size of the packet to be read next.
128 void selpk_want(selpk
*pk
, size_t sz
)
130 pkbuf_want(&pk
->pk
, sz
);
133 /* --- @selpk_init@ --- *
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
143 * Use: Initializes a buffer block.
146 void selpk_init(selpk
*pk
, sel_state
*s
, int fd
, pkbuf_func
*func
, void *p
)
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
);
154 /* --- @selpk_destroy@ --- *
156 * Arguments: @selpk *pk@ = pointer to buffer block
160 * Use: Deallocates a packet buffer and frees any resources it owned.
163 void selpk_destroy(selpk
*pk
)
166 pkbuf_destroy(&pk
->pk
);
169 /*----- That's all, folks -------------------------------------------------*/