[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[Libevent-users] When evhttp_connection_free should be called?
Hi,
I have a tunnel program to convert the TCP traffic to HTTP traffic
using the libevent.
When the data received, the listener's read_cb is invoked such as:
static read_cb(struct bufferevent *bev, void*ctx) {
struct evbuffer *bev = bufferevent_get_input(bev);
// now I need to start the HTTP connection.
}
As I need to consistently send the HTTP packets, I think it makes
senses to reuse a evhttp_connection instance which can perfectly fit
into the ctx. So in the accept callback, we initialize the
evhttp_connection once, attach it the ctx, then just reuse it:
// now I need to start the HTTP connection.
evhttp_connection_reset(ctx->evconn);
req = evhttp_request_new(response_cb, NULL);
// tweak the data.
evhttp_make_request(ctx->evconn, req, EVHTTP_REQ_POST, url);
}
ctx->evconn is freed in the event_cb of the listener.
This approach may introduce racing condition: the connection is
pending when we reset it. Also evhttp_connection_reset is an internal
function which should not be used.
So a better solution is to start a new evhttp_connection when read_cb
is invoked. But I wonder where I should put evhttp_connection_free
afterwards? The evhttp_make_request is not blocking, we can not just
free the connection at the end of the read_cb. Is the response_cb a
good place to free the evhttp_connection? The connection owns the
request, and free the req when req is done or some error incurs, so I
wonder whether it is OK to free the connection in evhttp_request_new's
callback.
I found somebody asks the same question:
http://archives.seul.org/libevent/users/Oct-2010/msg00060.html
But nobody responds yet.
Any suggestion is greatly appreciated.
Thanks,
Best regards,
Kun Xi
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users in the body.