chiark / gitweb /
The callback function can free the @bres_client@ structure! Make sure we
[mLib] / selpk.c
1 /* -*-c-*-
2  *
3  * $Id: selpk.c,v 1.3 2004/04/08 01:36:13 mdw Exp $
4  *
5  * Packet-buffering select handler
6  *
7  * (c) 1999 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of the mLib utilities library.
13  *
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.
18  * 
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.
23  * 
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,
27  * MA 02111-1307, USA.
28  */
29
30 /*----- Header files ------------------------------------------------------*/
31
32 #include <errno.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include <sys/types.h>
38 #include <sys/time.h>
39 #include <unistd.h>
40
41 #include "pkbuf.h"
42 #include "sel.h"
43 #include "selpk.h"
44
45 /*----- Main code ---------------------------------------------------------*/
46
47 /* --- @selpk_enable@ --- *
48  *
49  * Arguments:   @selpk *pk@ = pointer to buffer block
50  *
51  * Returns:     ---
52  *
53  * Use:         Enables a buffer for reading, and emits any queued packets
54  *              to the buffer's owner.
55  */
56
57 void selpk_enable(selpk *pk)
58 {
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);
63   }
64 }
65
66 /* --- @selpk_disable@ --- *
67  *
68  * Arguments:   @selpk *pk@ = pointer to a buffer block
69  *
70  * Returns:     ---
71  *
72  * Use:         Disables a buffer.  It won't be read from until it's
73  *              enabled again.
74  */
75
76 void selpk_disable(selpk *pk)
77 {
78   if (pk->pk.f & PKBUF_ENABLE) {
79     pk->pk.f &= ~PKBUF_ENABLE;
80     sel_rmfile(&pk->reader);
81   }
82 }
83
84 /* --- @selpk_read@ --- *
85  *
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
89  *
90  * Returns:     ---
91  *
92  * Use:         Acts on the result of a @select@ call.
93  */
94
95 static void selpk_read(int fd, unsigned mode, void *vp)
96 {
97   selpk *pk = vp;
98   octet *p;
99   size_t sz;
100   int n;
101
102   sz = pkbuf_free(&pk->pk, &p);
103   n = read(fd, p, sz);
104   if (n == 0)
105     pkbuf_close(&pk->pk);
106   else if (n > 0)
107     pkbuf_flush(&pk->pk, p, n);
108   else switch (errno) {
109     case EINTR:
110     case EAGAIN:
111 #if EAGAIN != EWOULDBLOCK
112     case EWOULDBLOCK:
113 #endif
114       return;
115     default:
116       pkbuf_close(&pk->pk);
117   }
118 }
119
120 /* --- @selpk_want@ --- *
121  *
122  * Arguments:   @selpk *pk@ = pointer to buffer block
123  *              @size_t sz@ = size of buffer
124  *
125  * Returns:     ---
126  *
127  * Use:         Sets the size of the packet to be read next.
128  */
129
130 void selpk_want(selpk *pk, size_t sz)
131 {
132   pkbuf_want(&pk->pk, sz);
133 }
134
135 /* --- @selpk_init@ --- *
136  *
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
142  *
143  * Returns:     ---
144  *
145  * Use:         Initializes a buffer block.
146  */
147
148 void selpk_init(selpk *pk, sel_state *s, int fd, pkbuf_func *func, void *p)
149 {
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);
153   selpk_enable(pk);
154 }
155
156 /* --- @selpk_destroy@ --- *
157  *
158  * Arguments:   @selpk *pk@ = pointer to buffer block
159  *
160  * Returns:     ---
161  *
162  * Use:         Deallocates a packet buffer and frees any resources it owned.
163  */
164
165 void selpk_destroy(selpk *pk)
166 {
167   selpk_disable(pk);
168   pkbuf_destroy(&pk->pk);
169 }
170
171 /*----- That's all, folks -------------------------------------------------*/