be: introduce bufferevent_generic_adj_existing_timeouts_()

And use it in openssl/sock layers to avoid copy-pasting it's variants.
This commit is contained in:
Azat Khuzhin 2015-11-05 17:45:09 +03:00
parent f4b6284b83
commit 3c1f58f58b
4 changed files with 26 additions and 42 deletions

View File

@ -412,6 +412,7 @@ void bufferevent_init_generic_timeout_cbs_(struct bufferevent *bev);
* we delete it.) Call this from anything that changes the timeout values,
* that enabled EV_READ or EV_WRITE, or that disables EV_READ or EV_WRITE. */
int bufferevent_generic_adj_timeouts_(struct bufferevent *bev);
int bufferevent_generic_adj_existing_timeouts_(struct bufferevent *bev);
enum bufferevent_options bufferevent_get_options_(struct bufferevent *bev);

View File

@ -969,6 +969,29 @@ bufferevent_generic_adj_timeouts_(struct bufferevent *bev)
return 0;
}
int
bufferevent_generic_adj_existing_timeouts_(struct bufferevent *bev)
{
int r = 0;
if (event_pending(&bev->ev_read, EV_READ, NULL)) {
if (evutil_timerisset(&bev->timeout_read)) {
if (bufferevent_add_event_(&bev->ev_read, &bev->timeout_read) < 0)
r = -1;
} else {
event_remove_timer(&bev->ev_read);
}
}
if (event_pending(&bev->ev_write, EV_WRITE, NULL)) {
if (evutil_timerisset(&bev->timeout_write)) {
if (bufferevent_add_event_(&bev->ev_write, &bev->timeout_write) < 0)
r = -1;
} else {
event_remove_timer(&bev->ev_write);
}
}
return r;
}
int
bufferevent_add_event_(struct event *ev, const struct timeval *tv)
{

View File

@ -1254,23 +1254,7 @@ be_openssl_adj_timeouts(struct bufferevent *bev)
if (bev_ssl->underlying) {
return bufferevent_generic_adj_timeouts_(bev);
} else {
int r1=0, r2=0;
if (event_pending(&bev->ev_read, EV_READ, NULL)) {
if (evutil_timerisset(&bev->timeout_read)) {
r1 = bufferevent_add_event_(&bev->ev_read, &bev->timeout_read);
} else {
event_remove_timer(&bev->ev_read);
}
}
if (event_pending(&bev->ev_write, EV_WRITE, NULL)) {
if (evutil_timerisset(&bev->timeout_write)) {
r2 = bufferevent_add_event_(&bev->ev_write, &bev->timeout_write);
} else {
event_remove_timer(&bev->ev_write);
}
}
return (r1<0 || r2<0) ? -1 : 0;
return bufferevent_generic_adj_existing_timeouts_(bev);
}
}

View File

@ -79,7 +79,6 @@
static int be_socket_enable(struct bufferevent *, short);
static int be_socket_disable(struct bufferevent *, short);
static void be_socket_destruct(struct bufferevent *);
static int be_socket_adj_timeouts(struct bufferevent *);
static int be_socket_flush(struct bufferevent *, short, enum bufferevent_flush_mode);
static int be_socket_ctrl(struct bufferevent *, enum bufferevent_ctrl_op, union bufferevent_ctrl_data *);
@ -92,7 +91,7 @@ const struct bufferevent_ops bufferevent_ops_socket = {
be_socket_disable,
NULL, /* unlink */
be_socket_destruct,
be_socket_adj_timeouts,
bufferevent_generic_adj_existing_timeouts_,
be_socket_flush,
be_socket_ctrl,
};
@ -619,29 +618,6 @@ be_socket_destruct(struct bufferevent *bufev)
EVUTIL_CLOSESOCKET(fd);
}
static int
be_socket_adj_timeouts(struct bufferevent *bufev)
{
int r = 0;
if (event_pending(&bufev->ev_read, EV_READ, NULL)) {
if (evutil_timerisset(&bufev->timeout_read)) {
if (be_socket_add(&bufev->ev_read, &bufev->timeout_read) < 0)
r = -1;
} else {
event_remove_timer(&bufev->ev_read);
}
}
if (event_pending(&bufev->ev_write, EV_WRITE, NULL)) {
if (evutil_timerisset(&bufev->timeout_write)) {
if (be_socket_add(&bufev->ev_write, &bufev->timeout_write) < 0)
r = -1;
} else {
event_remove_timer(&bufev->ev_write);
}
}
return r;
}
static int
be_socket_flush(struct bufferevent *bev, short iotype,
enum bufferevent_flush_mode mode)