Path: blob/main/contrib/libevent/bufferevent_pair.c
39475 views
/*1* Copyright (c) 2009-2012 Niels Provos, Nick Mathewson2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer.8* 2. Redistributions in binary form must reproduce the above copyright9* notice, this list of conditions and the following disclaimer in the10* documentation and/or other materials provided with the distribution.11* 3. The name of the author may not be used to endorse or promote products12* derived from this software without specific prior written permission.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR15* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES16* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.17* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,18* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT19* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,20* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY21* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT22* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF23* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.24*/25#include "event2/event-config.h"26#include "evconfig-private.h"2728#include <sys/types.h>2930#ifdef _WIN3231#include <winsock2.h>32#endif3334#include "event2/util.h"35#include "event2/buffer.h"36#include "event2/bufferevent.h"37#include "event2/bufferevent_struct.h"38#include "event2/event.h"39#include "defer-internal.h"40#include "bufferevent-internal.h"41#include "mm-internal.h"42#include "util-internal.h"4344struct bufferevent_pair {45struct bufferevent_private bev;46struct bufferevent_pair *partner;47/* For ->destruct() lock checking */48struct bufferevent_pair *unlinked_partner;49};505152/* Given a bufferevent that's really a bev part of a bufferevent_pair,53* return that bufferevent_filtered. Returns NULL otherwise.*/54static inline struct bufferevent_pair *55upcast(struct bufferevent *bev)56{57struct bufferevent_pair *bev_p;58if (!BEV_IS_PAIR(bev))59return NULL;60bev_p = EVUTIL_UPCAST(bev, struct bufferevent_pair, bev.bev);61EVUTIL_ASSERT(BEV_IS_PAIR(&bev_p->bev.bev));62return bev_p;63}6465#define downcast(bev_pair) (&(bev_pair)->bev.bev)6667static inline void68incref_and_lock(struct bufferevent *b)69{70struct bufferevent_pair *bevp;71bufferevent_incref_and_lock_(b);72bevp = upcast(b);73if (bevp->partner)74bufferevent_incref_and_lock_(downcast(bevp->partner));75}7677static inline void78decref_and_unlock(struct bufferevent *b)79{80struct bufferevent_pair *bevp = upcast(b);81if (bevp->partner)82bufferevent_decref_and_unlock_(downcast(bevp->partner));83bufferevent_decref_and_unlock_(b);84}8586/* XXX Handle close */8788static void be_pair_outbuf_cb(struct evbuffer *,89const struct evbuffer_cb_info *, void *);9091static struct bufferevent_pair *92bufferevent_pair_elt_new(struct event_base *base,93int options)94{95struct bufferevent_pair *bufev;96if (! (bufev = mm_calloc(1, sizeof(struct bufferevent_pair))))97return NULL;98if (bufferevent_init_common_(&bufev->bev, base, &bufferevent_ops_pair,99options)) {100mm_free(bufev);101return NULL;102}103if (!evbuffer_add_cb(bufev->bev.bev.output, be_pair_outbuf_cb, bufev)) {104bufferevent_free(downcast(bufev));105return NULL;106}107108bufferevent_init_generic_timeout_cbs_(&bufev->bev.bev);109110return bufev;111}112113int114bufferevent_pair_new(struct event_base *base, int options,115struct bufferevent *pair[2])116{117struct bufferevent_pair *bufev1 = NULL, *bufev2 = NULL;118int tmp_options;119120options |= BEV_OPT_DEFER_CALLBACKS;121tmp_options = options & ~BEV_OPT_THREADSAFE;122123bufev1 = bufferevent_pair_elt_new(base, options);124if (!bufev1)125return -1;126bufev2 = bufferevent_pair_elt_new(base, tmp_options);127if (!bufev2) {128bufferevent_free(downcast(bufev1));129return -1;130}131132if (options & BEV_OPT_THREADSAFE) {133/*XXXX check return */134bufferevent_enable_locking_(downcast(bufev2), bufev1->bev.lock);135}136137bufev1->partner = bufev2;138bufev2->partner = bufev1;139140evbuffer_freeze(downcast(bufev1)->input, 0);141evbuffer_freeze(downcast(bufev1)->output, 1);142evbuffer_freeze(downcast(bufev2)->input, 0);143evbuffer_freeze(downcast(bufev2)->output, 1);144145pair[0] = downcast(bufev1);146pair[1] = downcast(bufev2);147148return 0;149}150151static void152be_pair_transfer(struct bufferevent *src, struct bufferevent *dst,153int ignore_wm)154{155size_t dst_size;156size_t n;157158evbuffer_unfreeze(src->output, 1);159evbuffer_unfreeze(dst->input, 0);160161if (dst->wm_read.high) {162dst_size = evbuffer_get_length(dst->input);163if (dst_size < dst->wm_read.high) {164n = dst->wm_read.high - dst_size;165evbuffer_remove_buffer(src->output, dst->input, n);166} else {167if (!ignore_wm)168goto done;169n = evbuffer_get_length(src->output);170evbuffer_add_buffer(dst->input, src->output);171}172} else {173n = evbuffer_get_length(src->output);174evbuffer_add_buffer(dst->input, src->output);175}176177if (n) {178BEV_RESET_GENERIC_READ_TIMEOUT(dst);179180if (evbuffer_get_length(dst->output))181BEV_RESET_GENERIC_WRITE_TIMEOUT(dst);182else183BEV_DEL_GENERIC_WRITE_TIMEOUT(dst);184}185186bufferevent_trigger_nolock_(dst, EV_READ, 0);187bufferevent_trigger_nolock_(src, EV_WRITE, 0);188done:189evbuffer_freeze(src->output, 1);190evbuffer_freeze(dst->input, 0);191}192193static inline int194be_pair_wants_to_talk(struct bufferevent_pair *src,195struct bufferevent_pair *dst)196{197return (downcast(src)->enabled & EV_WRITE) &&198(downcast(dst)->enabled & EV_READ) &&199!dst->bev.read_suspended &&200evbuffer_get_length(downcast(src)->output);201}202203static void204be_pair_outbuf_cb(struct evbuffer *outbuf,205const struct evbuffer_cb_info *info, void *arg)206{207struct bufferevent_pair *bev_pair = arg;208struct bufferevent_pair *partner = bev_pair->partner;209210incref_and_lock(downcast(bev_pair));211212if (info->n_added > info->n_deleted && partner) {213/* We got more data. If the other side's reading, then214hand it over. */215if (be_pair_wants_to_talk(bev_pair, partner)) {216be_pair_transfer(downcast(bev_pair), downcast(partner), 0);217}218}219220decref_and_unlock(downcast(bev_pair));221}222223static int224be_pair_enable(struct bufferevent *bufev, short events)225{226struct bufferevent_pair *bev_p = upcast(bufev);227struct bufferevent_pair *partner = bev_p->partner;228229incref_and_lock(bufev);230231if (events & EV_READ) {232BEV_RESET_GENERIC_READ_TIMEOUT(bufev);233}234if ((events & EV_WRITE) && evbuffer_get_length(bufev->output))235BEV_RESET_GENERIC_WRITE_TIMEOUT(bufev);236237/* We're starting to read! Does the other side have anything to write?*/238if ((events & EV_READ) && partner &&239be_pair_wants_to_talk(partner, bev_p)) {240be_pair_transfer(downcast(partner), bufev, 0);241}242/* We're starting to write! Does the other side want to read? */243if ((events & EV_WRITE) && partner &&244be_pair_wants_to_talk(bev_p, partner)) {245be_pair_transfer(bufev, downcast(partner), 0);246}247decref_and_unlock(bufev);248return 0;249}250251static int252be_pair_disable(struct bufferevent *bev, short events)253{254if (events & EV_READ) {255BEV_DEL_GENERIC_READ_TIMEOUT(bev);256}257if (events & EV_WRITE) {258BEV_DEL_GENERIC_WRITE_TIMEOUT(bev);259}260return 0;261}262263static void264be_pair_unlink(struct bufferevent *bev)265{266struct bufferevent_pair *bev_p = upcast(bev);267268if (bev_p->partner) {269bev_p->unlinked_partner = bev_p->partner;270bev_p->partner->partner = NULL;271bev_p->partner = NULL;272}273}274275/* Free *shared* lock in the latest be (since we share it between two of them). */276static void277be_pair_destruct(struct bufferevent *bev)278{279struct bufferevent_pair *bev_p = upcast(bev);280281/* Transfer ownership of the lock into partner, otherwise we will use282* already free'd lock during freeing second bev, see next example:283*284* bev1->own_lock = 1285* bev2->own_lock = 0286* bev2->lock = bev1->lock287*288* bufferevent_free(bev1) # refcnt == 0 -> unlink289* bufferevent_free(bev2) # refcnt == 0 -> unlink290*291* event_base_free() -> finilizers -> EVTHREAD_FREE_LOCK(bev1->lock)292* -> BEV_LOCK(bev2->lock) <-- already freed293*294* Where bev1 == pair[0], bev2 == pair[1].295*/296if (bev_p->unlinked_partner && bev_p->bev.own_lock) {297bev_p->unlinked_partner->bev.own_lock = 1;298bev_p->bev.own_lock = 0;299}300bev_p->unlinked_partner = NULL;301}302303static int304be_pair_flush(struct bufferevent *bev, short iotype,305enum bufferevent_flush_mode mode)306{307struct bufferevent_pair *bev_p = upcast(bev);308struct bufferevent *partner;309310if (!bev_p->partner)311return -1;312313if (mode == BEV_NORMAL)314return 0;315316incref_and_lock(bev);317318partner = downcast(bev_p->partner);319320if ((iotype & EV_READ) != 0)321be_pair_transfer(partner, bev, 1);322323if ((iotype & EV_WRITE) != 0)324be_pair_transfer(bev, partner, 1);325326if (mode == BEV_FINISHED) {327short what = BEV_EVENT_EOF;328if (iotype & EV_READ)329what |= BEV_EVENT_WRITING;330if (iotype & EV_WRITE)331what |= BEV_EVENT_READING;332bufferevent_run_eventcb_(partner, what, 0);333}334decref_and_unlock(bev);335return 0;336}337338struct bufferevent *339bufferevent_pair_get_partner(struct bufferevent *bev)340{341struct bufferevent_pair *bev_p;342struct bufferevent *partner = NULL;343bev_p = upcast(bev);344if (! bev_p)345return NULL;346347incref_and_lock(bev);348if (bev_p->partner)349partner = downcast(bev_p->partner);350decref_and_unlock(bev);351return partner;352}353354const struct bufferevent_ops bufferevent_ops_pair = {355"pair_elt",356evutil_offsetof(struct bufferevent_pair, bev.bev),357be_pair_enable,358be_pair_disable,359be_pair_unlink,360be_pair_destruct,361bufferevent_generic_adj_timeouts_,362be_pair_flush,363NULL, /* ctrl */364};365366367