/bitcoin/depends/work/build/x86_64-pc-linux-gnu/libevent/2.1.12-stable-7656baec08e/bufferevent.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu> |
3 | | * Copyright (c) 2007-2012 Niels Provos, Nick Mathewson |
4 | | * |
5 | | * Redistribution and use in source and binary forms, with or without |
6 | | * modification, are permitted provided that the following conditions |
7 | | * are met: |
8 | | * 1. Redistributions of source code must retain the above copyright |
9 | | * notice, this list of conditions and the following disclaimer. |
10 | | * 2. Redistributions in binary form must reproduce the above copyright |
11 | | * notice, this list of conditions and the following disclaimer in the |
12 | | * documentation and/or other materials provided with the distribution. |
13 | | * 3. The name of the author may not be used to endorse or promote products |
14 | | * derived from this software without specific prior written permission. |
15 | | * |
16 | | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | | */ |
27 | | |
28 | | #include "event2/event-config.h" |
29 | | #include "evconfig-private.h" |
30 | | |
31 | | #include <sys/types.h> |
32 | | |
33 | | #ifdef EVENT__HAVE_SYS_TIME_H |
34 | | #include <sys/time.h> |
35 | | #endif |
36 | | |
37 | | #include <errno.h> |
38 | | #include <stdio.h> |
39 | | #include <stdlib.h> |
40 | | #include <string.h> |
41 | | #ifdef EVENT__HAVE_STDARG_H |
42 | | #include <stdarg.h> |
43 | | #endif |
44 | | |
45 | | #ifdef _WIN32 |
46 | | #include <winsock2.h> |
47 | | #endif |
48 | | |
49 | | #include "event2/util.h" |
50 | | #include "event2/buffer.h" |
51 | | #include "event2/buffer_compat.h" |
52 | | #include "event2/bufferevent.h" |
53 | | #include "event2/bufferevent_struct.h" |
54 | | #include "event2/bufferevent_compat.h" |
55 | | #include "event2/event.h" |
56 | | #include "event-internal.h" |
57 | | #include "log-internal.h" |
58 | | #include "mm-internal.h" |
59 | | #include "bufferevent-internal.h" |
60 | | #include "evbuffer-internal.h" |
61 | | #include "util-internal.h" |
62 | | |
63 | | static void bufferevent_cancel_all_(struct bufferevent *bev); |
64 | | static void bufferevent_finalize_cb_(struct event_callback *evcb, void *arg_); |
65 | | |
66 | | void |
67 | | bufferevent_suspend_read_(struct bufferevent *bufev, bufferevent_suspend_flags what) |
68 | 0 | { |
69 | 0 | struct bufferevent_private *bufev_private = BEV_UPCAST(bufev); |
70 | 0 | BEV_LOCK(bufev); |
71 | 0 | if (!bufev_private->read_suspended) Branch (71:6): [True: 0, False: 0]
|
72 | 0 | bufev->be_ops->disable(bufev, EV_READ); |
73 | 0 | bufev_private->read_suspended |= what; |
74 | 0 | BEV_UNLOCK(bufev); |
75 | 0 | } |
76 | | |
77 | | void |
78 | | bufferevent_unsuspend_read_(struct bufferevent *bufev, bufferevent_suspend_flags what) |
79 | 0 | { |
80 | 0 | struct bufferevent_private *bufev_private = BEV_UPCAST(bufev); |
81 | 0 | BEV_LOCK(bufev); |
82 | 0 | bufev_private->read_suspended &= ~what; |
83 | 0 | if (!bufev_private->read_suspended && (bufev->enabled & EV_READ)) Branch (83:6): [True: 0, False: 0]
Branch (83:40): [True: 0, False: 0]
|
84 | 0 | bufev->be_ops->enable(bufev, EV_READ); |
85 | 0 | BEV_UNLOCK(bufev); |
86 | 0 | } |
87 | | |
88 | | void |
89 | | bufferevent_suspend_write_(struct bufferevent *bufev, bufferevent_suspend_flags what) |
90 | 0 | { |
91 | 0 | struct bufferevent_private *bufev_private = BEV_UPCAST(bufev); |
92 | 0 | BEV_LOCK(bufev); |
93 | 0 | if (!bufev_private->write_suspended) Branch (93:6): [True: 0, False: 0]
|
94 | 0 | bufev->be_ops->disable(bufev, EV_WRITE); |
95 | 0 | bufev_private->write_suspended |= what; |
96 | 0 | BEV_UNLOCK(bufev); |
97 | 0 | } |
98 | | |
99 | | void |
100 | | bufferevent_unsuspend_write_(struct bufferevent *bufev, bufferevent_suspend_flags what) |
101 | 0 | { |
102 | 0 | struct bufferevent_private *bufev_private = BEV_UPCAST(bufev); |
103 | 0 | BEV_LOCK(bufev); |
104 | 0 | bufev_private->write_suspended &= ~what; |
105 | 0 | if (!bufev_private->write_suspended && (bufev->enabled & EV_WRITE)) Branch (105:6): [True: 0, False: 0]
Branch (105:41): [True: 0, False: 0]
|
106 | 0 | bufev->be_ops->enable(bufev, EV_WRITE); |
107 | 0 | BEV_UNLOCK(bufev); |
108 | 0 | } |
109 | | |
110 | | /** |
111 | | * Sometimes bufferevent's implementation can overrun high watermarks |
112 | | * (one of examples is openssl) and in this case if the read callback |
113 | | * will not handle enough data do over condition above the read |
114 | | * callback will never be called again (due to suspend above). |
115 | | * |
116 | | * To avoid this we are scheduling read callback again here, but only |
117 | | * from the user callback to avoid multiple scheduling: |
118 | | * - when the data had been added to it |
119 | | * - when the data had been drained from it (user specified read callback) |
120 | | */ |
121 | | static void bufferevent_inbuf_wm_check(struct bufferevent *bev) |
122 | 2.35M | { |
123 | 2.35M | if (!bev->wm_read.high) Branch (123:6): [True: 2.35M, False: 0]
|
124 | 2.35M | return; |
125 | 0 | if (!(bev->enabled & EV_READ)) Branch (125:6): [True: 0, False: 0]
|
126 | 0 | return; |
127 | 0 | if (evbuffer_get_length(bev->input) < bev->wm_read.high) Branch (127:6): [True: 0, False: 0]
|
128 | 0 | return; |
129 | | |
130 | 0 | bufferevent_trigger(bev, EV_READ, BEV_OPT_DEFER_CALLBACKS); |
131 | 0 | } |
132 | | |
133 | | /* Callback to implement watermarks on the input buffer. Only enabled |
134 | | * if the watermark is set. */ |
135 | | static void |
136 | | bufferevent_inbuf_wm_cb(struct evbuffer *buf, |
137 | | const struct evbuffer_cb_info *cbinfo, |
138 | | void *arg) |
139 | 0 | { |
140 | 0 | struct bufferevent *bufev = arg; |
141 | 0 | size_t size; |
142 | |
|
143 | 0 | size = evbuffer_get_length(buf); |
144 | |
|
145 | 0 | if (size >= bufev->wm_read.high) Branch (145:6): [True: 0, False: 0]
|
146 | 0 | bufferevent_wm_suspend_read(bufev); |
147 | 0 | else |
148 | 0 | bufferevent_wm_unsuspend_read(bufev); |
149 | 0 | } |
150 | | |
151 | | static void |
152 | | bufferevent_run_deferred_callbacks_locked(struct event_callback *cb, void *arg) |
153 | 0 | { |
154 | 0 | struct bufferevent_private *bufev_private = arg; |
155 | 0 | struct bufferevent *bufev = &bufev_private->bev; |
156 | |
|
157 | 0 | BEV_LOCK(bufev); |
158 | 0 | if ((bufev_private->eventcb_pending & BEV_EVENT_CONNECTED) && Branch (158:6): [True: 0, False: 0]
|
159 | 0 | bufev->errorcb) { Branch (159:6): [True: 0, False: 0]
|
160 | | /* The "connected" happened before any reads or writes, so |
161 | | send it first. */ |
162 | 0 | bufev_private->eventcb_pending &= ~BEV_EVENT_CONNECTED; |
163 | 0 | bufev->errorcb(bufev, BEV_EVENT_CONNECTED, bufev->cbarg); |
164 | 0 | } |
165 | 0 | if (bufev_private->readcb_pending && bufev->readcb) { Branch (165:6): [True: 0, False: 0]
Branch (165:39): [True: 0, False: 0]
|
166 | 0 | bufev_private->readcb_pending = 0; |
167 | 0 | bufev->readcb(bufev, bufev->cbarg); |
168 | 0 | bufferevent_inbuf_wm_check(bufev); |
169 | 0 | } |
170 | 0 | if (bufev_private->writecb_pending && bufev->writecb) { Branch (170:6): [True: 0, False: 0]
Branch (170:40): [True: 0, False: 0]
|
171 | 0 | bufev_private->writecb_pending = 0; |
172 | 0 | bufev->writecb(bufev, bufev->cbarg); |
173 | 0 | } |
174 | 0 | if (bufev_private->eventcb_pending && bufev->errorcb) { Branch (174:6): [True: 0, False: 0]
Branch (174:40): [True: 0, False: 0]
|
175 | 0 | short what = bufev_private->eventcb_pending; |
176 | 0 | int err = bufev_private->errno_pending; |
177 | 0 | bufev_private->eventcb_pending = 0; |
178 | 0 | bufev_private->errno_pending = 0; |
179 | 0 | EVUTIL_SET_SOCKET_ERROR(err); |
180 | 0 | bufev->errorcb(bufev, what, bufev->cbarg); |
181 | 0 | } |
182 | 0 | bufferevent_decref_and_unlock_(bufev); |
183 | 0 | } |
184 | | |
185 | | static void |
186 | | bufferevent_run_deferred_callbacks_unlocked(struct event_callback *cb, void *arg) |
187 | 0 | { |
188 | 0 | struct bufferevent_private *bufev_private = arg; |
189 | 0 | struct bufferevent *bufev = &bufev_private->bev; |
190 | |
|
191 | 0 | BEV_LOCK(bufev); |
192 | 0 | #define UNLOCKED(stmt) \ |
193 | 0 | do { BEV_UNLOCK(bufev); stmt; BEV_LOCK(bufev); } while(0) |
194 | |
|
195 | 0 | if ((bufev_private->eventcb_pending & BEV_EVENT_CONNECTED) && Branch (195:6): [True: 0, False: 0]
|
196 | 0 | bufev->errorcb) { Branch (196:6): [True: 0, False: 0]
|
197 | | /* The "connected" happened before any reads or writes, so |
198 | | send it first. */ |
199 | 0 | bufferevent_event_cb errorcb = bufev->errorcb; |
200 | 0 | void *cbarg = bufev->cbarg; |
201 | 0 | bufev_private->eventcb_pending &= ~BEV_EVENT_CONNECTED; |
202 | 0 | UNLOCKED(errorcb(bufev, BEV_EVENT_CONNECTED, cbarg)); |
203 | 0 | } |
204 | 0 | if (bufev_private->readcb_pending && bufev->readcb) { Branch (204:6): [True: 0, False: 0]
Branch (204:39): [True: 0, False: 0]
|
205 | 0 | bufferevent_data_cb readcb = bufev->readcb; |
206 | 0 | void *cbarg = bufev->cbarg; |
207 | 0 | bufev_private->readcb_pending = 0; |
208 | 0 | UNLOCKED(readcb(bufev, cbarg)); |
209 | 0 | bufferevent_inbuf_wm_check(bufev); |
210 | 0 | } |
211 | 0 | if (bufev_private->writecb_pending && bufev->writecb) { Branch (211:6): [True: 0, False: 0]
Branch (211:40): [True: 0, False: 0]
|
212 | 0 | bufferevent_data_cb writecb = bufev->writecb; |
213 | 0 | void *cbarg = bufev->cbarg; |
214 | 0 | bufev_private->writecb_pending = 0; |
215 | 0 | UNLOCKED(writecb(bufev, cbarg)); |
216 | 0 | } |
217 | 0 | if (bufev_private->eventcb_pending && bufev->errorcb) { Branch (217:6): [True: 0, False: 0]
Branch (217:40): [True: 0, False: 0]
|
218 | 0 | bufferevent_event_cb errorcb = bufev->errorcb; |
219 | 0 | void *cbarg = bufev->cbarg; |
220 | 0 | short what = bufev_private->eventcb_pending; |
221 | 0 | int err = bufev_private->errno_pending; |
222 | 0 | bufev_private->eventcb_pending = 0; |
223 | 0 | bufev_private->errno_pending = 0; |
224 | 0 | EVUTIL_SET_SOCKET_ERROR(err); |
225 | 0 | UNLOCKED(errorcb(bufev,what,cbarg)); |
226 | 0 | } |
227 | 0 | bufferevent_decref_and_unlock_(bufev); |
228 | 0 | #undef UNLOCKED |
229 | 0 | } |
230 | | |
231 | | #define SCHEDULE_DEFERRED(bevp) \ |
232 | 0 | do { \ |
233 | 0 | if (event_deferred_cb_schedule_( \ |
234 | 0 | (bevp)->bev.ev_base, \ |
235 | 0 | &(bevp)->deferred)) \ |
236 | 0 | bufferevent_incref_(&(bevp)->bev); \ |
237 | 0 | } while (0) |
238 | | |
239 | | |
240 | | void |
241 | | bufferevent_run_readcb_(struct bufferevent *bufev, int options) |
242 | 2.35M | { |
243 | | /* Requires that we hold the lock and a reference */ |
244 | 2.35M | struct bufferevent_private *p = BEV_UPCAST(bufev); |
245 | 2.35M | if (bufev->readcb == NULL) Branch (245:6): [True: 0, False: 2.35M]
|
246 | 0 | return; |
247 | 2.35M | if ((p->options|options) & BEV_OPT_DEFER_CALLBACKS) { Branch (247:6): [True: 0, False: 2.35M]
|
248 | 0 | p->readcb_pending = 1; |
249 | 0 | SCHEDULE_DEFERRED(p); |
250 | 2.35M | } else { |
251 | 2.35M | bufev->readcb(bufev, bufev->cbarg); |
252 | 2.35M | bufferevent_inbuf_wm_check(bufev); |
253 | 2.35M | } |
254 | 2.35M | } |
255 | | |
256 | | void |
257 | | bufferevent_run_writecb_(struct bufferevent *bufev, int options) |
258 | 2.35M | { |
259 | | /* Requires that we hold the lock and a reference */ |
260 | 2.35M | struct bufferevent_private *p = BEV_UPCAST(bufev); |
261 | 2.35M | if (bufev->writecb == NULL) Branch (261:6): [True: 0, False: 2.35M]
|
262 | 0 | return; |
263 | 2.35M | if ((p->options|options) & BEV_OPT_DEFER_CALLBACKS) { Branch (263:6): [True: 0, False: 2.35M]
|
264 | 0 | p->writecb_pending = 1; |
265 | 0 | SCHEDULE_DEFERRED(p); |
266 | 2.35M | } else { |
267 | 2.35M | bufev->writecb(bufev, bufev->cbarg); |
268 | 2.35M | } |
269 | 2.35M | } |
270 | | |
271 | 0 | #define BEV_TRIG_ALL_OPTS ( \ |
272 | 0 | BEV_TRIG_IGNORE_WATERMARKS| \ |
273 | 0 | BEV_TRIG_DEFER_CALLBACKS \ |
274 | 0 | ) |
275 | | |
276 | | void |
277 | | bufferevent_trigger(struct bufferevent *bufev, short iotype, int options) |
278 | 0 | { |
279 | 0 | bufferevent_incref_and_lock_(bufev); |
280 | 0 | bufferevent_trigger_nolock_(bufev, iotype, options&BEV_TRIG_ALL_OPTS); |
281 | 0 | bufferevent_decref_and_unlock_(bufev); |
282 | 0 | } |
283 | | |
284 | | void |
285 | | bufferevent_run_eventcb_(struct bufferevent *bufev, short what, int options) |
286 | 2.34M | { |
287 | | /* Requires that we hold the lock and a reference */ |
288 | 2.34M | struct bufferevent_private *p = BEV_UPCAST(bufev); |
289 | 2.34M | if (bufev->errorcb == NULL) Branch (289:6): [True: 0, False: 2.34M]
|
290 | 0 | return; |
291 | 2.34M | if ((p->options|options) & BEV_OPT_DEFER_CALLBACKS) { Branch (291:6): [True: 0, False: 2.34M]
|
292 | 0 | p->eventcb_pending |= what; |
293 | 0 | p->errno_pending = EVUTIL_SOCKET_ERROR(); |
294 | 0 | SCHEDULE_DEFERRED(p); |
295 | 2.34M | } else { |
296 | 2.34M | bufev->errorcb(bufev, what, bufev->cbarg); |
297 | 2.34M | } |
298 | 2.34M | } |
299 | | |
300 | | void |
301 | | bufferevent_trigger_event(struct bufferevent *bufev, short what, int options) |
302 | 0 | { |
303 | 0 | bufferevent_incref_and_lock_(bufev); |
304 | 0 | bufferevent_run_eventcb_(bufev, what, options&BEV_TRIG_ALL_OPTS); |
305 | 0 | bufferevent_decref_and_unlock_(bufev); |
306 | 0 | } |
307 | | |
308 | | int |
309 | | bufferevent_init_common_(struct bufferevent_private *bufev_private, |
310 | | struct event_base *base, |
311 | | const struct bufferevent_ops *ops, |
312 | | enum bufferevent_options options) |
313 | 2.35M | { |
314 | 2.35M | struct bufferevent *bufev = &bufev_private->bev; |
315 | | |
316 | 2.35M | if (!bufev->input) { Branch (316:6): [True: 2.35M, False: 0]
|
317 | 2.35M | if ((bufev->input = evbuffer_new()) == NULL) Branch (317:7): [True: 0, False: 2.35M]
|
318 | 0 | goto err; |
319 | 2.35M | } |
320 | | |
321 | 2.35M | if (!bufev->output) { Branch (321:6): [True: 2.35M, False: 0]
|
322 | 2.35M | if ((bufev->output = evbuffer_new()) == NULL) Branch (322:7): [True: 0, False: 2.35M]
|
323 | 0 | goto err; |
324 | 2.35M | } |
325 | | |
326 | 2.35M | bufev_private->refcnt = 1; |
327 | 2.35M | bufev->ev_base = base; |
328 | | |
329 | | /* Disable timeouts. */ |
330 | 2.35M | evutil_timerclear(&bufev->timeout_read); |
331 | 2.35M | evutil_timerclear(&bufev->timeout_write); |
332 | | |
333 | 2.35M | bufev->be_ops = ops; |
334 | | |
335 | 2.35M | if (bufferevent_ratelim_init_(bufev_private)) Branch (335:6): [True: 0, False: 2.35M]
|
336 | 0 | goto err; |
337 | | |
338 | | /* |
339 | | * Set to EV_WRITE so that using bufferevent_write is going to |
340 | | * trigger a callback. Reading needs to be explicitly enabled |
341 | | * because otherwise no data will be available. |
342 | | */ |
343 | 2.35M | bufev->enabled = EV_WRITE; |
344 | | |
345 | 2.35M | #ifndef EVENT__DISABLE_THREAD_SUPPORT |
346 | 2.35M | if (options & BEV_OPT_THREADSAFE) { Branch (346:6): [True: 0, False: 2.35M]
|
347 | 0 | if (bufferevent_enable_locking_(bufev, NULL) < 0) Branch (347:7): [True: 0, False: 0]
|
348 | 0 | goto err; |
349 | 0 | } |
350 | 2.35M | #endif |
351 | 2.35M | if ((options & (BEV_OPT_DEFER_CALLBACKS|BEV_OPT_UNLOCK_CALLBACKS)) Branch (351:6): [True: 0, False: 2.35M]
|
352 | 2.35M | == BEV_OPT_UNLOCK_CALLBACKS) { |
353 | 0 | event_warnx("UNLOCK_CALLBACKS requires DEFER_CALLBACKS"); |
354 | 0 | goto err; |
355 | 0 | } |
356 | 2.35M | if (options & BEV_OPT_UNLOCK_CALLBACKS) Branch (356:6): [True: 0, False: 2.35M]
|
357 | 0 | event_deferred_cb_init_( |
358 | 0 | &bufev_private->deferred, |
359 | 0 | event_base_get_npriorities(base) / 2, |
360 | 0 | bufferevent_run_deferred_callbacks_unlocked, |
361 | 0 | bufev_private); |
362 | 2.35M | else |
363 | 2.35M | event_deferred_cb_init_( |
364 | 2.35M | &bufev_private->deferred, |
365 | 2.35M | event_base_get_npriorities(base) / 2, |
366 | 2.35M | bufferevent_run_deferred_callbacks_locked, |
367 | 2.35M | bufev_private); |
368 | | |
369 | 2.35M | bufev_private->options = options; |
370 | | |
371 | 2.35M | evbuffer_set_parent_(bufev->input, bufev); |
372 | 2.35M | evbuffer_set_parent_(bufev->output, bufev); |
373 | | |
374 | 2.35M | return 0; |
375 | | |
376 | 0 | err: |
377 | 0 | if (bufev->input) { Branch (377:6): [True: 0, False: 0]
|
378 | 0 | evbuffer_free(bufev->input); |
379 | 0 | bufev->input = NULL; |
380 | 0 | } |
381 | 0 | if (bufev->output) { Branch (381:6): [True: 0, False: 0]
|
382 | 0 | evbuffer_free(bufev->output); |
383 | 0 | bufev->output = NULL; |
384 | 0 | } |
385 | 0 | return -1; |
386 | 2.35M | } |
387 | | |
388 | | void |
389 | | bufferevent_setcb(struct bufferevent *bufev, |
390 | | bufferevent_data_cb readcb, bufferevent_data_cb writecb, |
391 | | bufferevent_event_cb eventcb, void *cbarg) |
392 | 11.7M | { |
393 | 11.7M | BEV_LOCK(bufev); |
394 | | |
395 | 11.7M | bufev->readcb = readcb; |
396 | 11.7M | bufev->writecb = writecb; |
397 | 11.7M | bufev->errorcb = eventcb; |
398 | | |
399 | 11.7M | bufev->cbarg = cbarg; |
400 | 11.7M | BEV_UNLOCK(bufev); |
401 | 11.7M | } |
402 | | |
403 | | void |
404 | | bufferevent_getcb(struct bufferevent *bufev, |
405 | | bufferevent_data_cb *readcb_ptr, |
406 | | bufferevent_data_cb *writecb_ptr, |
407 | | bufferevent_event_cb *eventcb_ptr, |
408 | | void **cbarg_ptr) |
409 | 0 | { |
410 | 0 | BEV_LOCK(bufev); |
411 | 0 | if (readcb_ptr) Branch (411:6): [True: 0, False: 0]
|
412 | 0 | *readcb_ptr = bufev->readcb; |
413 | 0 | if (writecb_ptr) Branch (413:6): [True: 0, False: 0]
|
414 | 0 | *writecb_ptr = bufev->writecb; |
415 | 0 | if (eventcb_ptr) Branch (415:6): [True: 0, False: 0]
|
416 | 0 | *eventcb_ptr = bufev->errorcb; |
417 | 0 | if (cbarg_ptr) Branch (417:6): [True: 0, False: 0]
|
418 | 0 | *cbarg_ptr = bufev->cbarg; |
419 | |
|
420 | 0 | BEV_UNLOCK(bufev); |
421 | 0 | } |
422 | | |
423 | | struct evbuffer * |
424 | | bufferevent_get_input(struct bufferevent *bufev) |
425 | 11.7M | { |
426 | 11.7M | return bufev->input; |
427 | 11.7M | } |
428 | | |
429 | | struct evbuffer * |
430 | | bufferevent_get_output(struct bufferevent *bufev) |
431 | 4.71M | { |
432 | 4.71M | return bufev->output; |
433 | 4.71M | } |
434 | | |
435 | | struct event_base * |
436 | | bufferevent_get_base(struct bufferevent *bufev) |
437 | 2.35M | { |
438 | 2.35M | return bufev->ev_base; |
439 | 2.35M | } |
440 | | |
441 | | int |
442 | | bufferevent_get_priority(const struct bufferevent *bufev) |
443 | 2.35M | { |
444 | 2.35M | if (event_initialized(&bufev->ev_read)) { Branch (444:6): [True: 2.35M, False: 0]
|
445 | 2.35M | return event_get_priority(&bufev->ev_read); |
446 | 2.35M | } else { |
447 | 0 | return event_base_get_npriorities(bufev->ev_base) / 2; |
448 | 0 | } |
449 | 2.35M | } |
450 | | |
451 | | int |
452 | | bufferevent_write(struct bufferevent *bufev, const void *data, size_t size) |
453 | 0 | { |
454 | 0 | if (evbuffer_add(bufev->output, data, size) == -1) Branch (454:6): [True: 0, False: 0]
|
455 | 0 | return (-1); |
456 | | |
457 | 0 | return 0; |
458 | 0 | } |
459 | | |
460 | | int |
461 | | bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf) |
462 | 0 | { |
463 | 0 | if (evbuffer_add_buffer(bufev->output, buf) == -1) Branch (463:6): [True: 0, False: 0]
|
464 | 0 | return (-1); |
465 | | |
466 | 0 | return 0; |
467 | 0 | } |
468 | | |
469 | | size_t |
470 | | bufferevent_read(struct bufferevent *bufev, void *data, size_t size) |
471 | 0 | { |
472 | 0 | return (evbuffer_remove(bufev->input, data, size)); |
473 | 0 | } |
474 | | |
475 | | int |
476 | | bufferevent_read_buffer(struct bufferevent *bufev, struct evbuffer *buf) |
477 | 0 | { |
478 | 0 | return (evbuffer_add_buffer(buf, bufev->input)); |
479 | 0 | } |
480 | | |
481 | | int |
482 | | bufferevent_enable(struct bufferevent *bufev, short event) |
483 | 11.7M | { |
484 | 11.7M | struct bufferevent_private *bufev_private = BEV_UPCAST(bufev); |
485 | 11.7M | short impl_events = event; |
486 | 11.7M | int r = 0; |
487 | | |
488 | 11.7M | bufferevent_incref_and_lock_(bufev); |
489 | 11.7M | if (bufev_private->read_suspended) Branch (489:6): [True: 0, False: 11.7M]
|
490 | 0 | impl_events &= ~EV_READ; |
491 | 11.7M | if (bufev_private->write_suspended) Branch (491:6): [True: 0, False: 11.7M]
|
492 | 0 | impl_events &= ~EV_WRITE; |
493 | | |
494 | 11.7M | bufev->enabled |= event; |
495 | | |
496 | 11.7M | if (impl_events && bufev->be_ops->enable(bufev, impl_events) < 0) Branch (496:6): [True: 11.7M, False: 0]
Branch (496:21): [True: 0, False: 11.7M]
|
497 | 0 | r = -1; |
498 | 11.7M | if (r) Branch (498:6): [True: 0, False: 11.7M]
|
499 | 0 | event_debug(("%s: cannot enable 0x%hx on %p", __func__, event, bufev)); |
500 | | |
501 | 11.7M | bufferevent_decref_and_unlock_(bufev); |
502 | 11.7M | return r; |
503 | 11.7M | } |
504 | | |
505 | | int |
506 | | bufferevent_set_timeouts(struct bufferevent *bufev, |
507 | | const struct timeval *tv_read, |
508 | | const struct timeval *tv_write) |
509 | 2.35M | { |
510 | 2.35M | int r = 0; |
511 | 2.35M | BEV_LOCK(bufev); |
512 | 2.35M | if (tv_read) { Branch (512:6): [True: 2.35M, False: 0]
|
513 | 2.35M | bufev->timeout_read = *tv_read; |
514 | 2.35M | } else { |
515 | 0 | evutil_timerclear(&bufev->timeout_read); |
516 | 0 | } |
517 | 2.35M | if (tv_write) { Branch (517:6): [True: 2.35M, False: 0]
|
518 | 2.35M | bufev->timeout_write = *tv_write; |
519 | 2.35M | } else { |
520 | 0 | evutil_timerclear(&bufev->timeout_write); |
521 | 0 | } |
522 | | |
523 | 2.35M | if (bufev->be_ops->adj_timeouts) Branch (523:6): [True: 2.35M, False: 0]
|
524 | 2.35M | r = bufev->be_ops->adj_timeouts(bufev); |
525 | 2.35M | BEV_UNLOCK(bufev); |
526 | | |
527 | 2.35M | return r; |
528 | 2.35M | } |
529 | | |
530 | | |
531 | | /* Obsolete; use bufferevent_set_timeouts */ |
532 | | void |
533 | | bufferevent_settimeout(struct bufferevent *bufev, |
534 | | int timeout_read, int timeout_write) |
535 | 0 | { |
536 | 0 | struct timeval tv_read, tv_write; |
537 | 0 | struct timeval *ptv_read = NULL, *ptv_write = NULL; |
538 | |
|
539 | 0 | memset(&tv_read, 0, sizeof(tv_read)); |
540 | 0 | memset(&tv_write, 0, sizeof(tv_write)); |
541 | |
|
542 | 0 | if (timeout_read) { Branch (542:6): [True: 0, False: 0]
|
543 | 0 | tv_read.tv_sec = timeout_read; |
544 | 0 | ptv_read = &tv_read; |
545 | 0 | } |
546 | 0 | if (timeout_write) { Branch (546:6): [True: 0, False: 0]
|
547 | 0 | tv_write.tv_sec = timeout_write; |
548 | 0 | ptv_write = &tv_write; |
549 | 0 | } |
550 | |
|
551 | 0 | bufferevent_set_timeouts(bufev, ptv_read, ptv_write); |
552 | 0 | } |
553 | | |
554 | | |
555 | | int |
556 | | bufferevent_disable_hard_(struct bufferevent *bufev, short event) |
557 | 0 | { |
558 | 0 | int r = 0; |
559 | 0 | struct bufferevent_private *bufev_private = BEV_UPCAST(bufev); |
560 | |
|
561 | 0 | BEV_LOCK(bufev); |
562 | 0 | bufev->enabled &= ~event; |
563 | |
|
564 | 0 | bufev_private->connecting = 0; |
565 | 0 | if (bufev->be_ops->disable(bufev, event) < 0) Branch (565:6): [True: 0, False: 0]
|
566 | 0 | r = -1; |
567 | |
|
568 | 0 | BEV_UNLOCK(bufev); |
569 | 0 | return r; |
570 | 0 | } |
571 | | |
572 | | int |
573 | | bufferevent_disable(struct bufferevent *bufev, short event) |
574 | 16.4M | { |
575 | 16.4M | int r = 0; |
576 | | |
577 | 16.4M | BEV_LOCK(bufev); |
578 | 16.4M | bufev->enabled &= ~event; |
579 | | |
580 | 16.4M | if (bufev->be_ops->disable(bufev, event) < 0) Branch (580:6): [True: 0, False: 16.4M]
|
581 | 0 | r = -1; |
582 | 16.4M | if (r) Branch (582:6): [True: 0, False: 16.4M]
|
583 | 0 | event_debug(("%s: cannot disable 0x%hx on %p", __func__, event, bufev)); |
584 | | |
585 | 16.4M | BEV_UNLOCK(bufev); |
586 | 16.4M | return r; |
587 | 16.4M | } |
588 | | |
589 | | /* |
590 | | * Sets the water marks |
591 | | */ |
592 | | |
593 | | void |
594 | | bufferevent_setwatermark(struct bufferevent *bufev, short events, |
595 | | size_t lowmark, size_t highmark) |
596 | 0 | { |
597 | 0 | struct bufferevent_private *bufev_private = BEV_UPCAST(bufev); |
598 | |
|
599 | 0 | BEV_LOCK(bufev); |
600 | 0 | if (events & EV_WRITE) { Branch (600:6): [True: 0, False: 0]
|
601 | 0 | bufev->wm_write.low = lowmark; |
602 | 0 | bufev->wm_write.high = highmark; |
603 | 0 | } |
604 | |
|
605 | 0 | if (events & EV_READ) { Branch (605:6): [True: 0, False: 0]
|
606 | 0 | bufev->wm_read.low = lowmark; |
607 | 0 | bufev->wm_read.high = highmark; |
608 | |
|
609 | 0 | if (highmark) { Branch (609:7): [True: 0, False: 0]
|
610 | | /* There is now a new high-water mark for read. |
611 | | enable the callback if needed, and see if we should |
612 | | suspend/bufferevent_wm_unsuspend. */ |
613 | |
|
614 | 0 | if (bufev_private->read_watermarks_cb == NULL) { Branch (614:8): [True: 0, False: 0]
|
615 | 0 | bufev_private->read_watermarks_cb = |
616 | 0 | evbuffer_add_cb(bufev->input, |
617 | 0 | bufferevent_inbuf_wm_cb, |
618 | 0 | bufev); |
619 | 0 | } |
620 | 0 | evbuffer_cb_set_flags(bufev->input, |
621 | 0 | bufev_private->read_watermarks_cb, |
622 | 0 | EVBUFFER_CB_ENABLED|EVBUFFER_CB_NODEFER); |
623 | |
|
624 | 0 | if (evbuffer_get_length(bufev->input) >= highmark) Branch (624:8): [True: 0, False: 0]
|
625 | 0 | bufferevent_wm_suspend_read(bufev); |
626 | 0 | else if (evbuffer_get_length(bufev->input) < highmark) Branch (626:13): [True: 0, False: 0]
|
627 | 0 | bufferevent_wm_unsuspend_read(bufev); |
628 | 0 | } else { |
629 | | /* There is now no high-water mark for read. */ |
630 | 0 | if (bufev_private->read_watermarks_cb) Branch (630:8): [True: 0, False: 0]
|
631 | 0 | evbuffer_cb_clear_flags(bufev->input, |
632 | 0 | bufev_private->read_watermarks_cb, |
633 | 0 | EVBUFFER_CB_ENABLED); |
634 | 0 | bufferevent_wm_unsuspend_read(bufev); |
635 | 0 | } |
636 | 0 | } |
637 | 0 | BEV_UNLOCK(bufev); |
638 | 0 | } |
639 | | |
640 | | int |
641 | | bufferevent_getwatermark(struct bufferevent *bufev, short events, |
642 | | size_t *lowmark, size_t *highmark) |
643 | 0 | { |
644 | 0 | if (events == EV_WRITE) { Branch (644:6): [True: 0, False: 0]
|
645 | 0 | BEV_LOCK(bufev); |
646 | 0 | if (lowmark) Branch (646:7): [True: 0, False: 0]
|
647 | 0 | *lowmark = bufev->wm_write.low; |
648 | 0 | if (highmark) Branch (648:7): [True: 0, False: 0]
|
649 | 0 | *highmark = bufev->wm_write.high; |
650 | 0 | BEV_UNLOCK(bufev); |
651 | 0 | return 0; |
652 | 0 | } |
653 | | |
654 | 0 | if (events == EV_READ) { Branch (654:6): [True: 0, False: 0]
|
655 | 0 | BEV_LOCK(bufev); |
656 | 0 | if (lowmark) Branch (656:7): [True: 0, False: 0]
|
657 | 0 | *lowmark = bufev->wm_read.low; |
658 | 0 | if (highmark) Branch (658:7): [True: 0, False: 0]
|
659 | 0 | *highmark = bufev->wm_read.high; |
660 | 0 | BEV_UNLOCK(bufev); |
661 | 0 | return 0; |
662 | 0 | } |
663 | 0 | return -1; |
664 | 0 | } |
665 | | |
666 | | int |
667 | | bufferevent_flush(struct bufferevent *bufev, |
668 | | short iotype, |
669 | | enum bufferevent_flush_mode mode) |
670 | 0 | { |
671 | 0 | int r = -1; |
672 | 0 | BEV_LOCK(bufev); |
673 | 0 | if (bufev->be_ops->flush) Branch (673:6): [True: 0, False: 0]
|
674 | 0 | r = bufev->be_ops->flush(bufev, iotype, mode); |
675 | 0 | BEV_UNLOCK(bufev); |
676 | 0 | return r; |
677 | 0 | } |
678 | | |
679 | | void |
680 | | bufferevent_incref_and_lock_(struct bufferevent *bufev) |
681 | 18.8M | { |
682 | 18.8M | struct bufferevent_private *bufev_private = BEV_UPCAST(bufev); |
683 | 18.8M | BEV_LOCK(bufev); |
684 | 18.8M | ++bufev_private->refcnt; |
685 | 18.8M | } |
686 | | |
687 | | #if 0 |
688 | | static void |
689 | | bufferevent_transfer_lock_ownership_(struct bufferevent *donor, |
690 | | struct bufferevent *recipient) |
691 | | { |
692 | | struct bufferevent_private *d = BEV_UPCAST(donor); |
693 | | struct bufferevent_private *r = BEV_UPCAST(recipient); |
694 | | if (d->lock != r->lock) |
695 | | return; |
696 | | if (r->own_lock) |
697 | | return; |
698 | | if (d->own_lock) { |
699 | | d->own_lock = 0; |
700 | | r->own_lock = 1; |
701 | | } |
702 | | } |
703 | | #endif |
704 | | |
705 | | int |
706 | | bufferevent_decref_and_unlock_(struct bufferevent *bufev) |
707 | 21.2M | { |
708 | 21.2M | struct bufferevent_private *bufev_private = BEV_UPCAST(bufev); |
709 | 21.2M | int n_cbs = 0; |
710 | 21.2M | #define MAX_CBS 16 |
711 | 21.2M | struct event_callback *cbs[MAX_CBS]; |
712 | | |
713 | 21.2M | EVUTIL_ASSERT(bufev_private->refcnt > 0); |
714 | | |
715 | 21.2M | if (--bufev_private->refcnt) { Branch (715:6): [True: 18.8M, False: 2.35M]
|
716 | 18.8M | BEV_UNLOCK(bufev); |
717 | 18.8M | return 0; |
718 | 18.8M | } |
719 | | |
720 | 2.35M | if (bufev->be_ops->unlink) Branch (720:6): [True: 0, False: 2.35M]
|
721 | 0 | bufev->be_ops->unlink(bufev); |
722 | | |
723 | | /* Okay, we're out of references. Let's finalize this once all the |
724 | | * callbacks are done running. */ |
725 | 2.35M | cbs[0] = &bufev->ev_read.ev_evcallback; |
726 | 2.35M | cbs[1] = &bufev->ev_write.ev_evcallback; |
727 | 2.35M | cbs[2] = &bufev_private->deferred; |
728 | 2.35M | n_cbs = 3; |
729 | 2.35M | if (bufev_private->rate_limiting) { Branch (729:6): [True: 0, False: 2.35M]
|
730 | 0 | struct event *e = &bufev_private->rate_limiting->refill_bucket_event; |
731 | 0 | if (event_initialized(e)) Branch (731:7): [True: 0, False: 0]
|
732 | 0 | cbs[n_cbs++] = &e->ev_evcallback; |
733 | 0 | } |
734 | 2.35M | n_cbs += evbuffer_get_callbacks_(bufev->input, cbs+n_cbs, MAX_CBS-n_cbs); |
735 | 2.35M | n_cbs += evbuffer_get_callbacks_(bufev->output, cbs+n_cbs, MAX_CBS-n_cbs); |
736 | | |
737 | 2.35M | event_callback_finalize_many_(bufev->ev_base, n_cbs, cbs, |
738 | 2.35M | bufferevent_finalize_cb_); |
739 | | |
740 | 2.35M | #undef MAX_CBS |
741 | 2.35M | BEV_UNLOCK(bufev); |
742 | | |
743 | 2.35M | return 1; |
744 | 21.2M | } |
745 | | |
746 | | static void |
747 | | bufferevent_finalize_cb_(struct event_callback *evcb, void *arg_) |
748 | 2.35M | { |
749 | 2.35M | struct bufferevent *bufev = arg_; |
750 | 2.35M | struct bufferevent *underlying; |
751 | 2.35M | struct bufferevent_private *bufev_private = BEV_UPCAST(bufev); |
752 | | |
753 | 2.35M | BEV_LOCK(bufev); |
754 | 2.35M | underlying = bufferevent_get_underlying(bufev); |
755 | | |
756 | | /* Clean up the shared info */ |
757 | 2.35M | if (bufev->be_ops->destruct) Branch (757:6): [True: 2.35M, False: 0]
|
758 | 2.35M | bufev->be_ops->destruct(bufev); |
759 | | |
760 | | /* XXX what happens if refcnt for these buffers is > 1? |
761 | | * The buffers can share a lock with this bufferevent object, |
762 | | * but the lock might be destroyed below. */ |
763 | | /* evbuffer will free the callbacks */ |
764 | 2.35M | evbuffer_free(bufev->input); |
765 | 2.35M | evbuffer_free(bufev->output); |
766 | | |
767 | 2.35M | if (bufev_private->rate_limiting) { Branch (767:6): [True: 0, False: 2.35M]
|
768 | 0 | if (bufev_private->rate_limiting->group) Branch (768:7): [True: 0, False: 0]
|
769 | 0 | bufferevent_remove_from_rate_limit_group_internal_(bufev,0); |
770 | 0 | mm_free(bufev_private->rate_limiting); |
771 | 0 | bufev_private->rate_limiting = NULL; |
772 | 0 | } |
773 | | |
774 | | |
775 | 2.35M | BEV_UNLOCK(bufev); |
776 | | |
777 | 2.35M | if (bufev_private->own_lock) Branch (777:6): [True: 0, False: 2.35M]
|
778 | 0 | EVTHREAD_FREE_LOCK(bufev_private->lock, |
779 | 2.35M | EVTHREAD_LOCKTYPE_RECURSIVE); |
780 | | |
781 | | /* Free the actual allocated memory. */ |
782 | 2.35M | mm_free(((char*)bufev) - bufev->be_ops->mem_offset); |
783 | | |
784 | | /* Release the reference to underlying now that we no longer need the |
785 | | * reference to it. We wait this long mainly in case our lock is |
786 | | * shared with underlying. |
787 | | * |
788 | | * The 'destruct' function will also drop a reference to underlying |
789 | | * if BEV_OPT_CLOSE_ON_FREE is set. |
790 | | * |
791 | | * XXX Should we/can we just refcount evbuffer/bufferevent locks? |
792 | | * It would probably save us some headaches. |
793 | | */ |
794 | 2.35M | if (underlying) Branch (794:6): [True: 0, False: 2.35M]
|
795 | 0 | bufferevent_decref_(underlying); |
796 | 2.35M | } |
797 | | |
798 | | int |
799 | | bufferevent_decref(struct bufferevent *bufev) |
800 | 0 | { |
801 | 0 | BEV_LOCK(bufev); |
802 | 0 | return bufferevent_decref_and_unlock_(bufev); |
803 | 0 | } |
804 | | |
805 | | void |
806 | | bufferevent_free(struct bufferevent *bufev) |
807 | 2.35M | { |
808 | 2.35M | BEV_LOCK(bufev); |
809 | 2.35M | bufferevent_setcb(bufev, NULL, NULL, NULL, NULL); |
810 | 2.35M | bufferevent_cancel_all_(bufev); |
811 | 2.35M | bufferevent_decref_and_unlock_(bufev); |
812 | 2.35M | } |
813 | | |
814 | | void |
815 | | bufferevent_incref(struct bufferevent *bufev) |
816 | 0 | { |
817 | 0 | struct bufferevent_private *bufev_private = BEV_UPCAST(bufev); |
818 | | |
819 | | /* XXX: now that this function is public, we might want to |
820 | | * - return the count from this function |
821 | | * - create a new function to atomically grab the current refcount |
822 | | */ |
823 | 0 | BEV_LOCK(bufev); |
824 | 0 | ++bufev_private->refcnt; |
825 | 0 | BEV_UNLOCK(bufev); |
826 | 0 | } |
827 | | |
828 | | int |
829 | | bufferevent_enable_locking_(struct bufferevent *bufev, void *lock) |
830 | 0 | { |
831 | | #ifdef EVENT__DISABLE_THREAD_SUPPORT |
832 | | return -1; |
833 | | #else |
834 | 0 | struct bufferevent *underlying; |
835 | |
|
836 | 0 | if (BEV_UPCAST(bufev)->lock) Branch (836:6): [True: 0, False: 0]
|
837 | 0 | return -1; |
838 | 0 | underlying = bufferevent_get_underlying(bufev); |
839 | |
|
840 | 0 | if (!lock && underlying && BEV_UPCAST(underlying)->lock) { Branch (840:6): [True: 0, False: 0]
Branch (840:15): [True: 0, False: 0]
Branch (840:29): [True: 0, False: 0]
|
841 | 0 | lock = BEV_UPCAST(underlying)->lock; |
842 | 0 | BEV_UPCAST(bufev)->lock = lock; |
843 | 0 | BEV_UPCAST(bufev)->own_lock = 0; |
844 | 0 | } else if (!lock) { Branch (844:13): [True: 0, False: 0]
|
845 | 0 | EVTHREAD_ALLOC_LOCK(lock, EVTHREAD_LOCKTYPE_RECURSIVE); |
846 | 0 | if (!lock) Branch (846:7): [True: 0, False: 0]
|
847 | 0 | return -1; |
848 | 0 | BEV_UPCAST(bufev)->lock = lock; |
849 | 0 | BEV_UPCAST(bufev)->own_lock = 1; |
850 | 0 | } else { |
851 | 0 | BEV_UPCAST(bufev)->lock = lock; |
852 | 0 | BEV_UPCAST(bufev)->own_lock = 0; |
853 | 0 | } |
854 | 0 | evbuffer_enable_locking(bufev->input, lock); |
855 | 0 | evbuffer_enable_locking(bufev->output, lock); |
856 | |
|
857 | 0 | if (underlying && !BEV_UPCAST(underlying)->lock) Branch (857:6): [True: 0, False: 0]
Branch (857:20): [True: 0, False: 0]
|
858 | 0 | bufferevent_enable_locking_(underlying, lock); |
859 | |
|
860 | 0 | return 0; |
861 | 0 | #endif |
862 | 0 | } |
863 | | |
864 | | int |
865 | | bufferevent_setfd(struct bufferevent *bev, evutil_socket_t fd) |
866 | 2.35M | { |
867 | 2.35M | union bufferevent_ctrl_data d; |
868 | 2.35M | int res = -1; |
869 | 2.35M | d.fd = fd; |
870 | 2.35M | BEV_LOCK(bev); |
871 | 2.35M | if (bev->be_ops->ctrl) Branch (871:6): [True: 2.35M, False: 0]
|
872 | 2.35M | res = bev->be_ops->ctrl(bev, BEV_CTRL_SET_FD, &d); |
873 | 2.35M | if (res) Branch (873:6): [True: 0, False: 2.35M]
|
874 | 0 | event_debug(("%s: cannot set fd for %p to "EV_SOCK_FMT, __func__, bev, fd)); |
875 | 2.35M | BEV_UNLOCK(bev); |
876 | 2.35M | return res; |
877 | 2.35M | } |
878 | | |
879 | | evutil_socket_t |
880 | | bufferevent_getfd(struct bufferevent *bev) |
881 | 0 | { |
882 | 0 | union bufferevent_ctrl_data d; |
883 | 0 | int res = -1; |
884 | 0 | d.fd = -1; |
885 | 0 | BEV_LOCK(bev); |
886 | 0 | if (bev->be_ops->ctrl) Branch (886:6): [True: 0, False: 0]
|
887 | 0 | res = bev->be_ops->ctrl(bev, BEV_CTRL_GET_FD, &d); |
888 | 0 | if (res) Branch (888:6): [True: 0, False: 0]
|
889 | 0 | event_debug(("%s: cannot get fd for %p", __func__, bev)); |
890 | 0 | BEV_UNLOCK(bev); |
891 | 0 | return (res<0) ? -1 : d.fd; Branch (891:9): [True: 0, False: 0]
|
892 | 0 | } |
893 | | |
894 | | enum bufferevent_options |
895 | | bufferevent_get_options_(struct bufferevent *bev) |
896 | 2.35M | { |
897 | 2.35M | struct bufferevent_private *bev_p = BEV_UPCAST(bev); |
898 | 2.35M | enum bufferevent_options options; |
899 | | |
900 | 2.35M | BEV_LOCK(bev); |
901 | 2.35M | options = bev_p->options; |
902 | 2.35M | BEV_UNLOCK(bev); |
903 | 2.35M | return options; |
904 | 2.35M | } |
905 | | |
906 | | |
907 | | static void |
908 | | bufferevent_cancel_all_(struct bufferevent *bev) |
909 | 2.35M | { |
910 | 2.35M | union bufferevent_ctrl_data d; |
911 | 2.35M | memset(&d, 0, sizeof(d)); |
912 | 2.35M | BEV_LOCK(bev); |
913 | 2.35M | if (bev->be_ops->ctrl) Branch (913:6): [True: 2.35M, False: 0]
|
914 | 2.35M | bev->be_ops->ctrl(bev, BEV_CTRL_CANCEL_ALL, &d); |
915 | 2.35M | BEV_UNLOCK(bev); |
916 | 2.35M | } |
917 | | |
918 | | short |
919 | | bufferevent_get_enabled(struct bufferevent *bufev) |
920 | 0 | { |
921 | 0 | short r; |
922 | 0 | BEV_LOCK(bufev); |
923 | 0 | r = bufev->enabled; |
924 | 0 | BEV_UNLOCK(bufev); |
925 | 0 | return r; |
926 | 0 | } |
927 | | |
928 | | struct bufferevent * |
929 | | bufferevent_get_underlying(struct bufferevent *bev) |
930 | 2.35M | { |
931 | 2.35M | union bufferevent_ctrl_data d; |
932 | 2.35M | int res = -1; |
933 | 2.35M | d.ptr = NULL; |
934 | 2.35M | BEV_LOCK(bev); |
935 | 2.35M | if (bev->be_ops->ctrl) Branch (935:6): [True: 2.35M, False: 0]
|
936 | 2.35M | res = bev->be_ops->ctrl(bev, BEV_CTRL_GET_UNDERLYING, &d); |
937 | 2.35M | BEV_UNLOCK(bev); |
938 | 2.35M | return (res<0) ? NULL : d.ptr; Branch (938:9): [True: 2.35M, False: 0]
|
939 | 2.35M | } |
940 | | |
941 | | static void |
942 | | bufferevent_generic_read_timeout_cb(evutil_socket_t fd, short event, void *ctx) |
943 | 0 | { |
944 | 0 | struct bufferevent *bev = ctx; |
945 | 0 | bufferevent_incref_and_lock_(bev); |
946 | 0 | bufferevent_disable(bev, EV_READ); |
947 | 0 | bufferevent_run_eventcb_(bev, BEV_EVENT_TIMEOUT|BEV_EVENT_READING, 0); |
948 | 0 | bufferevent_decref_and_unlock_(bev); |
949 | 0 | } |
950 | | static void |
951 | | bufferevent_generic_write_timeout_cb(evutil_socket_t fd, short event, void *ctx) |
952 | 0 | { |
953 | 0 | struct bufferevent *bev = ctx; |
954 | 0 | bufferevent_incref_and_lock_(bev); |
955 | 0 | bufferevent_disable(bev, EV_WRITE); |
956 | 0 | bufferevent_run_eventcb_(bev, BEV_EVENT_TIMEOUT|BEV_EVENT_WRITING, 0); |
957 | 0 | bufferevent_decref_and_unlock_(bev); |
958 | 0 | } |
959 | | |
960 | | void |
961 | | bufferevent_init_generic_timeout_cbs_(struct bufferevent *bev) |
962 | 0 | { |
963 | 0 | event_assign(&bev->ev_read, bev->ev_base, -1, EV_FINALIZE, |
964 | 0 | bufferevent_generic_read_timeout_cb, bev); |
965 | 0 | event_assign(&bev->ev_write, bev->ev_base, -1, EV_FINALIZE, |
966 | 0 | bufferevent_generic_write_timeout_cb, bev); |
967 | 0 | } |
968 | | |
969 | | int |
970 | | bufferevent_generic_adj_timeouts_(struct bufferevent *bev) |
971 | 0 | { |
972 | 0 | const short enabled = bev->enabled; |
973 | 0 | struct bufferevent_private *bev_p = BEV_UPCAST(bev); |
974 | 0 | int r1=0, r2=0; |
975 | 0 | if ((enabled & EV_READ) && !bev_p->read_suspended && Branch (975:6): [True: 0, False: 0]
Branch (975:29): [True: 0, False: 0]
|
976 | 0 | evutil_timerisset(&bev->timeout_read)) Branch (976:6): [True: 0, False: 0]
|
977 | 0 | r1 = event_add(&bev->ev_read, &bev->timeout_read); |
978 | 0 | else |
979 | 0 | r1 = event_del(&bev->ev_read); |
980 | |
|
981 | 0 | if ((enabled & EV_WRITE) && !bev_p->write_suspended && Branch (981:6): [True: 0, False: 0]
Branch (981:30): [True: 0, False: 0]
|
982 | 0 | evutil_timerisset(&bev->timeout_write) && Branch (982:6): [True: 0, False: 0]
|
983 | 0 | evbuffer_get_length(bev->output)) Branch (983:6): [True: 0, False: 0]
|
984 | 0 | r2 = event_add(&bev->ev_write, &bev->timeout_write); |
985 | 0 | else |
986 | 0 | r2 = event_del(&bev->ev_write); |
987 | 0 | if (r1 < 0 || r2 < 0) Branch (987:6): [True: 0, False: 0]
Branch (987:16): [True: 0, False: 0]
|
988 | 0 | return -1; |
989 | 0 | return 0; |
990 | 0 | } |
991 | | |
992 | | int |
993 | | bufferevent_generic_adj_existing_timeouts_(struct bufferevent *bev) |
994 | 2.35M | { |
995 | 2.35M | int r = 0; |
996 | 2.35M | if (event_pending(&bev->ev_read, EV_READ, NULL)) { Branch (996:6): [True: 2.35M, False: 0]
|
997 | 2.35M | if (evutil_timerisset(&bev->timeout_read)) { Branch (997:7): [True: 2.35M, False: 0]
|
998 | 2.35M | if (bufferevent_add_event_(&bev->ev_read, &bev->timeout_read) < 0) Branch (998:12): [True: 0, False: 2.35M]
|
999 | 0 | r = -1; |
1000 | 2.35M | } else { |
1001 | 0 | event_remove_timer(&bev->ev_read); |
1002 | 0 | } |
1003 | 2.35M | } |
1004 | 2.35M | if (event_pending(&bev->ev_write, EV_WRITE, NULL)) { Branch (1004:6): [True: 0, False: 2.35M]
|
1005 | 0 | if (evutil_timerisset(&bev->timeout_write)) { Branch (1005:7): [True: 0, False: 0]
|
1006 | 0 | if (bufferevent_add_event_(&bev->ev_write, &bev->timeout_write) < 0) Branch (1006:8): [True: 0, False: 0]
|
1007 | 0 | r = -1; |
1008 | 0 | } else { |
1009 | 0 | event_remove_timer(&bev->ev_write); |
1010 | 0 | } |
1011 | 0 | } |
1012 | 2.35M | return r; |
1013 | 2.35M | } |
1014 | | |
1015 | | int |
1016 | | bufferevent_add_event_(struct event *ev, const struct timeval *tv) |
1017 | 16.5M | { |
1018 | 16.5M | if (!evutil_timerisset(tv)) Branch (1018:6): [True: 4.71M, False: 11.7M]
|
1019 | 4.71M | return event_add(ev, NULL); |
1020 | 11.7M | else |
1021 | 11.7M | return event_add(ev, tv); |
1022 | 16.5M | } |
1023 | | |
1024 | | /* For use by user programs only; internally, we should be calling |
1025 | | either bufferevent_incref_and_lock_(), or BEV_LOCK. */ |
1026 | | void |
1027 | | bufferevent_lock(struct bufferevent *bev) |
1028 | 0 | { |
1029 | 0 | bufferevent_incref_and_lock_(bev); |
1030 | 0 | } |
1031 | | |
1032 | | void |
1033 | | bufferevent_unlock(struct bufferevent *bev) |
1034 | 0 | { |
1035 | 0 | bufferevent_decref_and_unlock_(bev); |
1036 | 0 | } |