9 Star 29 Fork 15

Gitee 极速下载/libevent

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/libevent/libevent
克隆/下载
bufferevent_filter.c 17.52 KB
一键复制 编辑 原始数据 按行查看 历史
Cœur 提交于 2024-04-30 15:59 . Fix typos (#1634)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
/*
* Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
* Copyright (c) 2002-2006 Niels Provos <provos@citi.umich.edu>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "evconfig-private.h"
#include <sys/types.h>
#include "event2/event-config.h"
#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef EVENT__HAVE_STDARG_H
#include <stdarg.h>
#endif
#ifdef _WIN32
#include <winsock2.h>
#endif
#include "event2/util.h"
#include "event2/bufferevent.h"
#include "event2/buffer.h"
#include "event2/bufferevent_struct.h"
#include "event2/event.h"
#include "log-internal.h"
#include "mm-internal.h"
#include "bufferevent-internal.h"
#include "util-internal.h"
/* prototypes */
static int be_filter_enable(struct bufferevent *, short);
static int be_filter_disable(struct bufferevent *, short);
static void be_filter_unlink(struct bufferevent *);
static void be_filter_destruct(struct bufferevent *);
static void be_filter_readcb(struct bufferevent *, void *);
static void be_filter_writecb(struct bufferevent *, void *);
static void be_filter_eventcb(struct bufferevent *, short, void *);
static int be_filter_flush(struct bufferevent *bufev,
short iotype, enum bufferevent_flush_mode mode);
static int be_filter_ctrl(struct bufferevent *, enum bufferevent_ctrl_op, union bufferevent_ctrl_data *);
static void bufferevent_filtered_inbuf_cb(struct evbuffer *buf,
const struct evbuffer_cb_info *cbinfo, void *arg);
static void bufferevent_filtered_outbuf_cb(struct evbuffer *buf,
const struct evbuffer_cb_info *info, void *arg);
struct bufferevent_filtered {
struct bufferevent_private bev;
/** The bufferevent that we read/write filtered data from/to. */
struct bufferevent *underlying;
/** A callback on our inbuf to notice somebody removes data */
struct evbuffer_cb_entry *inbuf_cb;
/** A callback on our outbuf to notice when somebody adds data */
struct evbuffer_cb_entry *outbuf_cb;
/** True iff we have received an EOF callback from the underlying
* bufferevent. */
unsigned got_eof;
/** Function to free context when we're done. */
void (*free_context)(void *);
/** Input filter */
bufferevent_filter_cb process_in;
/** Output filter */
bufferevent_filter_cb process_out;
/** User-supplied argument to the filters. */
void *context;
};
const struct bufferevent_ops bufferevent_ops_filter = {
"filter",
evutil_offsetof(struct bufferevent_filtered, bev.bev),
be_filter_enable,
be_filter_disable,
be_filter_unlink,
be_filter_destruct,
bufferevent_generic_adj_timeouts_,
be_filter_flush,
be_filter_ctrl,
};
/* Given a bufferevent that's really the bev filter of a bufferevent_filtered,
* return that bufferevent_filtered. Returns NULL otherwise.*/
static inline struct bufferevent_filtered *
upcast(struct bufferevent *bev)
{
struct bufferevent_filtered *bev_f;
EVUTIL_ASSERT(BEV_IS_FILTER(bev));
bev_f = (void*)( ((char*)bev) -
evutil_offsetof(struct bufferevent_filtered, bev.bev));
EVUTIL_ASSERT(BEV_IS_FILTER(&bev_f->bev.bev));
return bev_f;
}
#define downcast(bev_f) (&(bev_f)->bev.bev)
/** Return 1 iff bevf's underlying bufferevent's output buffer is at or
* over its high watermark such that we should not write to it in a given
* flush mode. */
static int
be_underlying_writebuf_full(struct bufferevent_filtered *bevf,
enum bufferevent_flush_mode state)
{
struct bufferevent *u = bevf->underlying;
return state == BEV_NORMAL &&
u->wm_write.high &&
evbuffer_get_length(u->output) >= u->wm_write.high;
}
/** Return 1 if our input buffer is at or over its high watermark such that we
* should not write to it in a given flush mode. */
static int
be_readbuf_full(struct bufferevent_filtered *bevf,
enum bufferevent_flush_mode state)
{
struct bufferevent *bufev = downcast(bevf);
return state == BEV_NORMAL &&
bufev->wm_read.high &&
evbuffer_get_length(bufev->input) >= bufev->wm_read.high;
}
/* Filter to use when we're created with a NULL filter. */
static enum bufferevent_filter_result
be_null_filter(struct evbuffer *src, struct evbuffer *dst, ev_ssize_t lim,
enum bufferevent_flush_mode state, void *ctx)
{
(void)state;
if (evbuffer_remove_buffer(src, dst, lim) >= 0)
return BEV_OK;
else
return BEV_ERROR;
}
struct bufferevent *
bufferevent_filter_new(struct bufferevent *underlying,
bufferevent_filter_cb input_filter,
bufferevent_filter_cb output_filter,
int options,
void (*free_context)(void *),
void *ctx)
{
struct bufferevent_filtered *bufev_f;
int tmp_options = options & ~BEV_OPT_THREADSAFE;
if (!underlying)
return NULL;
if (!input_filter)
input_filter = be_null_filter;
if (!output_filter)
output_filter = be_null_filter;
bufev_f = mm_calloc(1, sizeof(struct bufferevent_filtered));
if (!bufev_f)
return NULL;
if (bufferevent_init_common_(&bufev_f->bev, underlying->ev_base,
&bufferevent_ops_filter, tmp_options) < 0) {
mm_free(bufev_f);
return NULL;
}
if (options & BEV_OPT_THREADSAFE) {
bufferevent_enable_locking_(downcast(bufev_f), NULL);
}
bufev_f->underlying = underlying;
bufev_f->process_in = input_filter;
bufev_f->process_out = output_filter;
bufev_f->free_context = free_context;
bufev_f->context = ctx;
bufferevent_setcb(bufev_f->underlying,
be_filter_readcb, be_filter_writecb, be_filter_eventcb, bufev_f);
bufev_f->inbuf_cb = evbuffer_add_cb(downcast(bufev_f)->input,
bufferevent_filtered_inbuf_cb, bufev_f);
evbuffer_cb_clear_flags(downcast(bufev_f)->input, bufev_f->inbuf_cb,
EVBUFFER_CB_ENABLED);
bufev_f->outbuf_cb = evbuffer_add_cb(downcast(bufev_f)->output,
bufferevent_filtered_outbuf_cb, bufev_f);
bufferevent_init_generic_timeout_cbs_(downcast(bufev_f));
bufferevent_incref_(underlying);
bufferevent_enable(underlying, EV_READ|EV_WRITE);
bufferevent_suspend_read_(underlying, BEV_SUSPEND_FILT_READ);
return downcast(bufev_f);
}
static void
be_filter_unlink(struct bufferevent *bev)
{
struct bufferevent_filtered *bevf = upcast(bev);
if (bevf->bev.options & BEV_OPT_CLOSE_ON_FREE) {
/* Yes, there is also a decref in bufferevent_decref_.
* That decref corresponds to the incref when we set
* underlying for the first time. This decref is an
* extra one to remove the last reference.
*/
if (BEV_UPCAST(bevf->underlying)->refcnt < 2) {
event_warnx("BEV_OPT_CLOSE_ON_FREE set on an "
"bufferevent with too few references");
} else {
bufferevent_free(bevf->underlying);
}
} else {
if (bevf->underlying) {
if (bevf->underlying->errorcb == be_filter_eventcb)
bufferevent_setcb(bevf->underlying,
NULL, NULL, NULL, NULL);
bufferevent_unsuspend_read_(bevf->underlying,
BEV_SUSPEND_FILT_READ);
}
}
}
static void
be_filter_destruct(struct bufferevent *bev)
{
struct bufferevent_filtered *bevf = upcast(bev);
if (bevf->free_context)
bevf->free_context(bevf->context);
if (bevf->inbuf_cb)
evbuffer_remove_cb_entry(bev->input, bevf->inbuf_cb);
if (bevf->outbuf_cb)
evbuffer_remove_cb_entry(bev->output, bevf->outbuf_cb);
}
static int
be_filter_enable(struct bufferevent *bev, short event)
{
struct bufferevent_filtered *bevf = upcast(bev);
if (event & EV_WRITE)
BEV_RESET_GENERIC_WRITE_TIMEOUT(bev);
if (event & EV_READ) {
BEV_RESET_GENERIC_READ_TIMEOUT(bev);
bufferevent_unsuspend_read_(bevf->underlying,
BEV_SUSPEND_FILT_READ);
}
return 0;
}
static int
be_filter_disable(struct bufferevent *bev, short event)
{
struct bufferevent_filtered *bevf = upcast(bev);
if (event & EV_WRITE)
BEV_DEL_GENERIC_WRITE_TIMEOUT(bev);
if (event & EV_READ) {
BEV_DEL_GENERIC_READ_TIMEOUT(bev);
bufferevent_suspend_read_(bevf->underlying,
BEV_SUSPEND_FILT_READ);
}
return 0;
}
static enum bufferevent_filter_result
be_filter_process_input(struct bufferevent_filtered *bevf,
enum bufferevent_flush_mode state,
int *processed_out)
{
enum bufferevent_filter_result res;
struct bufferevent *bev = downcast(bevf);
if (state == BEV_NORMAL) {
/* If we're in 'normal' mode, don't urge data on the filter
* unless we're reading data and under our high-water mark.*/
if (!(bev->enabled & EV_READ) ||
be_readbuf_full(bevf, state))
return BEV_OK;
}
do {
ev_ssize_t limit = -1;
if (state == BEV_NORMAL && bev->wm_read.high)
limit = bev->wm_read.high -
evbuffer_get_length(bev->input);
res = bevf->process_in(bevf->underlying->input,
bev->input, limit, state, bevf->context);
if (res == BEV_OK)
*processed_out = 1;
} while (res == BEV_OK &&
(bev->enabled & EV_READ) &&
evbuffer_get_length(bevf->underlying->input) &&
!be_readbuf_full(bevf, state));
if (*processed_out)
BEV_RESET_GENERIC_READ_TIMEOUT(bev);
return res;
}
static enum bufferevent_filter_result
be_filter_process_output(struct bufferevent_filtered *bevf,
enum bufferevent_flush_mode state,
int *processed_out)
{
/* Requires references and lock: might call writecb */
enum bufferevent_filter_result res = BEV_OK;
struct bufferevent *bufev = downcast(bevf);
int again = 0;
if (state == BEV_NORMAL) {
/* If we're in 'normal' mode, don't urge data on the
* filter unless we're writing data, and the underlying
* bufferevent is accepting data, and we have data to
* give the filter. If we're in 'flush' or 'finish',
* call the filter no matter what. */
if (!(bufev->enabled & EV_WRITE) ||
be_underlying_writebuf_full(bevf, state) ||
!evbuffer_get_length(bufev->output))
return BEV_OK;
}
/* disable the callback that calls this function
when the user adds to the output buffer. */
evbuffer_cb_clear_flags(bufev->output, bevf->outbuf_cb,
EVBUFFER_CB_ENABLED);
do {
int processed = 0;
again = 0;
do {
ev_ssize_t limit = -1;
if (state == BEV_NORMAL &&
bevf->underlying->wm_write.high)
limit = bevf->underlying->wm_write.high -
evbuffer_get_length(bevf->underlying->output);
res = bevf->process_out(downcast(bevf)->output,
bevf->underlying->output,
limit,
state,
bevf->context);
if (res == BEV_OK)
processed = *processed_out = 1;
} while (/* Stop if the filter wasn't successful...*/
res == BEV_OK &&
/* Or if we aren't writing any more. */
(bufev->enabled & EV_WRITE) &&
/* Of if we have nothing more to write and we are
* not flushing. */
evbuffer_get_length(bufev->output) &&
/* Or if we have filled the underlying output buffer. */
!be_underlying_writebuf_full(bevf,state));
if (processed) {
/* call the write callback.*/
bufferevent_trigger_nolock_(bufev, EV_WRITE, 0);
if (res == BEV_OK &&
(bufev->enabled & EV_WRITE) &&
evbuffer_get_length(bufev->output) &&
!be_underlying_writebuf_full(bevf, state)) {
again = 1;
}
}
} while (again);
/* reenable the outbuf_cb */
evbuffer_cb_set_flags(bufev->output,bevf->outbuf_cb,
EVBUFFER_CB_ENABLED);
if (*processed_out)
BEV_RESET_GENERIC_WRITE_TIMEOUT(bufev);
return res;
}
/* Called when the size of our outbuf changes. */
static void
bufferevent_filtered_outbuf_cb(struct evbuffer *buf,
const struct evbuffer_cb_info *cbinfo, void *arg)
{
struct bufferevent_filtered *bevf = arg;
struct bufferevent *bev = downcast(bevf);
if (cbinfo->n_added) {
int processed_any = 0;
/* Somebody added more data to the output buffer. Try to
* process it, if we should. */
bufferevent_incref_and_lock_(bev);
be_filter_process_output(bevf, BEV_NORMAL, &processed_any);
bufferevent_decref_and_unlock_(bev);
}
}
static void
be_filter_read_nolock_(struct bufferevent *underlying, void *me_)
{
struct bufferevent_filtered *bevf = me_;
enum bufferevent_filter_result res;
enum bufferevent_flush_mode state;
struct bufferevent *bufev = downcast(bevf);
struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
int processed_any = 0;
// It's possible our refcount is 0 at this point if another thread free'd our filterevent
EVUTIL_ASSERT(bufev_private->refcnt >= 0);
// If our refcount is > 0
if (bufev_private->refcnt > 0) {
if (bevf->got_eof)
state = BEV_FINISHED;
else
state = BEV_NORMAL;
/* XXXX use return value */
res = be_filter_process_input(bevf, state, &processed_any);
(void)res;
/* XXX This should be in process_input, not here. There are
* other places that can call process-input, and they should
* force readcb calls as needed. */
if (processed_any) {
bufferevent_trigger_nolock_(bufev, EV_READ, 0);
if (evbuffer_get_length(underlying->input) > 0 &&
be_readbuf_full(bevf, state)) {
/* data left in underlying buffer and filter input buffer
* hit its read high watermark.
* Schedule callback to avoid data gets stuck in underlying
* input buffer.
*/
evbuffer_cb_set_flags(bufev->input, bevf->inbuf_cb,
EVBUFFER_CB_ENABLED);
}
}
}
}
/* Called when the size of our inbuf changes. */
static void
bufferevent_filtered_inbuf_cb(struct evbuffer *buf,
const struct evbuffer_cb_info *cbinfo, void *arg)
{
struct bufferevent_filtered *bevf = arg;
enum bufferevent_flush_mode state;
struct bufferevent *bev = downcast(bevf);
BEV_LOCK(bev);
if (bevf->got_eof)
state = BEV_FINISHED;
else
state = BEV_NORMAL;
if (!be_readbuf_full(bevf, state)) {
/* opportunity to read data which was left in underlying
* input buffer because filter input buffer hit read
* high watermark.
*/
evbuffer_cb_clear_flags(bev->input, bevf->inbuf_cb,
EVBUFFER_CB_ENABLED);
if (evbuffer_get_length(bevf->underlying->input) > 0)
be_filter_read_nolock_(bevf->underlying, bevf);
}
BEV_UNLOCK(bev);
}
/* Called when the underlying socket has read. */
static void
be_filter_readcb(struct bufferevent *underlying, void *me_)
{
struct bufferevent_filtered *bevf = me_;
struct bufferevent *bev = downcast(bevf);
BEV_LOCK(bev);
be_filter_read_nolock_(underlying, me_);
BEV_UNLOCK(bev);
}
/* Called when the underlying socket has drained enough that we can write to
it. */
static void
be_filter_writecb(struct bufferevent *underlying, void *me_)
{
struct bufferevent_filtered *bevf = me_;
struct bufferevent *bev = downcast(bevf);
struct bufferevent_private *bufev_private = BEV_UPCAST(bev);
int processed_any = 0;
BEV_LOCK(bev);
// It's possible our refcount is 0 at this point if another thread free'd our filterevent
EVUTIL_ASSERT(bufev_private->refcnt >= 0);
// If our refcount is > 0
if (bufev_private->refcnt > 0) {
be_filter_process_output(bevf, BEV_NORMAL, &processed_any);
}
BEV_UNLOCK(bev);
}
/* Called when the underlying socket has given us an error */
static void
be_filter_eventcb(struct bufferevent *underlying, short what, void *me_)
{
struct bufferevent_filtered *bevf = me_;
struct bufferevent *bev = downcast(bevf);
struct bufferevent_private *bufev_private = BEV_UPCAST(bev);
BEV_LOCK(bev);
// It's possible our refcount is 0 at this point if another thread free'd our filterevent
EVUTIL_ASSERT(bufev_private->refcnt >= 0);
// If our refcount is > 0
if (bufev_private->refcnt > 0) {
/* All we can really to is tell our own eventcb. */
bufferevent_run_eventcb_(bev, what, 0);
}
BEV_UNLOCK(bev);
}
static int
be_filter_flush(struct bufferevent *bufev,
short iotype, enum bufferevent_flush_mode mode)
{
struct bufferevent_filtered *bevf = upcast(bufev);
int processed_any = 0;
bufferevent_incref_and_lock_(bufev);
if (iotype & EV_READ) {
be_filter_process_input(bevf, mode, &processed_any);
}
if (iotype & EV_WRITE) {
be_filter_process_output(bevf, mode, &processed_any);
}
/* XXX check the return value? */
/* XXX does this want to recursively call lower-level flushes? */
bufferevent_flush(bevf->underlying, iotype, mode);
bufferevent_decref_and_unlock_(bufev);
return processed_any;
}
static int
be_filter_ctrl(struct bufferevent *bev, enum bufferevent_ctrl_op op,
union bufferevent_ctrl_data *data)
{
struct bufferevent_filtered *bevf;
switch (op) {
case BEV_CTRL_GET_UNDERLYING:
bevf = upcast(bev);
data->ptr = bevf->underlying;
return 0;
case BEV_CTRL_SET_FD:
case BEV_CTRL_GET_FD:
bevf = upcast(bev);
if (bevf->underlying &&
bevf->underlying->be_ops &&
bevf->underlying->be_ops->ctrl) {
return (bevf->underlying->be_ops->ctrl)(bevf->underlying, op, data);
}
EVUTIL_FALLTHROUGH;
case BEV_CTRL_CANCEL_ALL:
EVUTIL_FALLTHROUGH;
default:
return -1;
}
return -1;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/libevent.git
git@gitee.com:mirrors/libevent.git
mirrors
libevent
libevent
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385