chiark / gitweb /
@@@ fltfmt fettling
[mLib] / sel / selpk.c
1 /* -*-c-*-
2  *
3  * Packet-buffering select handler
4  *
5  * (c) 1999 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the mLib utilities library.
11  *
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.
16  *
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.
21  *
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,
25  * MA 02111-1307, USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <errno.h>
31 #include <sys/types.h>
32 #include <sys/time.h>
33 #include <unistd.h>
34
35 #include "pkbuf.h"
36 #include "sel.h"
37 #include "selpk.h"
38
39 /*----- Main code ---------------------------------------------------------*/
40
41 /* --- @selpk_enable@ --- *
42  *
43  * Arguments:   @selpk *pk@ = pointer to buffer block
44  *
45  * Returns:     ---
46  *
47  * Use:         Enables a buffer for reading, and emits any queued packets
48  *              to the buffer's owner.
49  */
50
51 void selpk_enable(selpk *pk)
52 {
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);
57   }
58 }
59
60 /* --- @selpk_disable@ --- *
61  *
62  * Arguments:   @selpk *pk@ = pointer to a buffer block
63  *
64  * Returns:     ---
65  *
66  * Use:         Disables a buffer.  It won't be read from until it's
67  *              enabled again.
68  */
69
70 void selpk_disable(selpk *pk)
71 {
72   if (pk->pk.f & PKBUF_ENABLE) {
73     pk->pk.f &= ~PKBUF_ENABLE;
74     sel_rmfile(&pk->reader);
75   }
76 }
77
78 /* --- @selpk_read@ --- *
79  *
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
83  *
84  * Returns:     ---
85  *
86  * Use:         Acts on the result of a @select@ call.
87  */
88
89 static void selpk_read(int fd, unsigned mode, void *vp)
90 {
91   selpk *pk = vp;
92   octet *p;
93   size_t sz;
94   int n;
95
96   sz = pkbuf_free(&pk->pk, &p);
97   n = read(fd, p, sz);
98   if (n == 0)
99     pkbuf_close(&pk->pk);
100   else if (n > 0)
101     pkbuf_flush(&pk->pk, p, n);
102   else switch (errno) {
103     case EINTR:
104     case EAGAIN:
105 #if EAGAIN != EWOULDBLOCK
106     case EWOULDBLOCK:
107 #endif
108       return;
109     default:
110       pkbuf_close(&pk->pk);
111   }
112 }
113
114 /* --- @selpk_want@ --- *
115  *
116  * Arguments:   @selpk *pk@ = pointer to buffer block
117  *              @size_t sz@ = size of buffer
118  *
119  * Returns:     ---
120  *
121  * Use:         Sets the size of the packet to be read next.
122  */
123
124 void selpk_want(selpk *pk, size_t sz)
125 {
126   pkbuf_want(&pk->pk, sz);
127 }
128
129 /* --- @selpk_init@ --- *
130  *
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
136  *
137  * Returns:     ---
138  *
139  * Use:         Initializes a buffer block.
140  */
141
142 void selpk_init(selpk *pk, sel_state *s, int fd, pkbuf_func *func, void *p)
143 {
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);
147   selpk_enable(pk);
148 }
149
150 /* --- @selpk_destroy@ --- *
151  *
152  * Arguments:   @selpk *pk@ = pointer to buffer block
153  *
154  * Returns:     ---
155  *
156  * Use:         Deallocates a packet buffer and frees any resources it owned.
157  */
158
159 void selpk_destroy(selpk *pk)
160 {
161   selpk_disable(pk);
162   pkbuf_destroy(&pk->pk);
163 }
164
165 /*----- That's all, folks -------------------------------------------------*/