436fddaa |
1 | /* -*-c-*- |
2 | * |
0daaeb18 |
3 | * $Id: selpk.c,v 1.2 2002/01/13 13:33:51 mdw Exp $ |
436fddaa |
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 | /*----- Revision history --------------------------------------------------* |
31 | * |
32 | * $Log: selpk.c,v $ |
0daaeb18 |
33 | * Revision 1.2 2002/01/13 13:33:51 mdw |
34 | * Packet handler functions now have a @typedef@ name. |
35 | * |
436fddaa |
36 | * Revision 1.1 2000/06/17 10:39:19 mdw |
37 | * Experimental new support for packet buffering. |
38 | * |
39 | */ |
40 | |
41 | /*----- Header files ------------------------------------------------------*/ |
42 | |
43 | #include <errno.h> |
44 | #include <stdio.h> |
45 | #include <stdlib.h> |
46 | #include <string.h> |
47 | |
48 | #include <sys/types.h> |
49 | #include <sys/time.h> |
50 | #include <unistd.h> |
51 | |
52 | #include "pkbuf.h" |
53 | #include "sel.h" |
54 | #include "selpk.h" |
55 | |
56 | /*----- Main code ---------------------------------------------------------*/ |
57 | |
58 | /* --- @selpk_enable@ --- * |
59 | * |
60 | * Arguments: @selpk *pk@ = pointer to buffer block |
61 | * |
62 | * Returns: --- |
63 | * |
64 | * Use: Enables a buffer for reading, and emits any queued packets |
65 | * to the buffer's owner. |
66 | */ |
67 | |
68 | void selpk_enable(selpk *pk) |
69 | { |
70 | if (!(pk->pk.f & PKBUF_ENABLE)) { |
71 | pk->pk.f |= PKBUF_ENABLE; |
72 | sel_addfile(&pk->reader); |
73 | pkbuf_flush(&pk->pk, 0, 0); |
74 | } |
75 | } |
76 | |
77 | /* --- @selpk_disable@ --- * |
78 | * |
79 | * Arguments: @selpk *pk@ = pointer to a buffer block |
80 | * |
81 | * Returns: --- |
82 | * |
83 | * Use: Disables a buffer. It won't be read from until it's |
84 | * enabled again. |
85 | */ |
86 | |
87 | void selpk_disable(selpk *pk) |
88 | { |
89 | if (pk->pk.f & PKBUF_ENABLE) { |
90 | pk->pk.f &= ~PKBUF_ENABLE; |
91 | sel_rmfile(&pk->reader); |
92 | } |
93 | } |
94 | |
95 | /* --- @selpk_read@ --- * |
96 | * |
97 | * Arguments: @int fd@ = file descriptor to read from |
98 | * @int mode@ = what we can do to the file |
99 | * @void *vp@ = pointer to buffer context |
100 | * |
101 | * Returns: --- |
102 | * |
103 | * Use: Acts on the result of a @select@ call. |
104 | */ |
105 | |
106 | static void selpk_read(int fd, unsigned mode, void *vp) |
107 | { |
108 | selpk *pk = vp; |
109 | octet *p; |
110 | size_t sz; |
111 | int n; |
112 | |
113 | sz = pkbuf_free(&pk->pk, &p); |
114 | n = read(fd, p, sz); |
115 | if (n == 0) |
116 | pkbuf_close(&pk->pk); |
117 | else if (n > 0) |
118 | pkbuf_flush(&pk->pk, p, n); |
119 | else switch (errno) { |
120 | case EINTR: |
121 | case EAGAIN: |
122 | #if EAGAIN != EWOULDBLOCK |
123 | case EWOULDBLOCK: |
124 | #endif |
125 | return; |
126 | default: |
127 | pkbuf_close(&pk->pk); |
128 | } |
129 | } |
130 | |
131 | /* --- @selpk_want@ --- * |
132 | * |
133 | * Arguments: @selpk *pk@ = pointer to buffer block |
134 | * @size_t sz@ = size of buffer |
135 | * |
136 | * Returns: --- |
137 | * |
138 | * Use: Sets the size of the packet to be read next. |
139 | */ |
140 | |
141 | void selpk_want(selpk *pk, size_t sz) |
142 | { |
143 | pkbuf_want(&pk->pk, sz); |
144 | } |
145 | |
146 | /* --- @selpk_init@ --- * |
147 | * |
148 | * Arguments: @selpk *pk@ = pointer to buffer block |
149 | * @sel_state *s@ = pointer to select state to attach to |
150 | * @int fd@ = file descriptor to listen to |
0daaeb18 |
151 | * @pkbuf_func *func@ = function to call |
436fddaa |
152 | * @void *p@ = argument for function |
153 | * |
154 | * Returns: --- |
155 | * |
156 | * Use: Initializes a buffer block. |
157 | */ |
158 | |
0daaeb18 |
159 | void selpk_init(selpk *pk, sel_state *s, int fd, pkbuf_func *func, void *p) |
436fddaa |
160 | { |
161 | pkbuf_init(&pk->pk, func, p); |
162 | pk->pk.f &= ~PKBUF_ENABLE; |
163 | sel_initfile(s, &pk->reader, fd, SEL_READ, selpk_read, pk); |
164 | selpk_enable(pk); |
165 | } |
166 | |
167 | /* --- @selpk_destroy@ --- * |
168 | * |
169 | * Arguments: @selpk *pk@ = pointer to buffer block |
170 | * |
171 | * Returns: --- |
172 | * |
173 | * Use: Deallocates a packet buffer and frees any resources it owned. |
174 | */ |
175 | |
176 | void selpk_destroy(selpk *pk) |
177 | { |
178 | selpk_disable(pk); |
179 | pkbuf_destroy(&pk->pk); |
180 | } |
181 | |
182 | /*----- That's all, folks -------------------------------------------------*/ |