Functions to actually use evhttp_bound_socket with/as evconnlistener.

This commit is contained in:
Nick Mathewson 2010-10-25 11:50:51 -04:00
parent 46ee061ca0
commit 006efa7dbb
2 changed files with 41 additions and 10 deletions

38
http.c
View File

@ -2764,23 +2764,35 @@ evhttp_accept_socket_with_handle(struct evhttp *http, evutil_socket_t fd)
const int flags =
LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_EXEC|LEV_OPT_CLOSE_ON_FREE;
listener = evconnlistener_new(http->base, NULL, NULL,
flags,
0, /* Backlog is '0' because we already said 'listen' */
fd);
if (!listener)
return (NULL);
bound = evhttp_bind_listener(http, listener);
if (!bound) {
evconnlistener_free(listener);
return (NULL);
}
return (bound);
}
struct evhttp_bound_socket *
evhttp_bind_listener(struct evhttp *http, struct evconnlistener *listener)
{
struct evhttp_bound_socket *bound;
bound = mm_malloc(sizeof(struct evhttp_bound_socket));
if (bound == NULL)
return (NULL);
listener = evconnlistener_new(http->base, accept_socket_cb, http,
flags,
0, /* Backlog is '0' because we already said 'listen' */
fd);
if (!listener) {
mm_free(bound);
return (NULL);
}
bound->listener = listener;
TAILQ_INSERT_TAIL(&http->sockets, bound, next);
return (bound);
evconnlistener_set_cb(listener, accept_socket_cb, http);
return bound;
}
evutil_socket_t evhttp_bound_socket_get_fd(struct evhttp_bound_socket *bound)
@ -2788,6 +2800,12 @@ evutil_socket_t evhttp_bound_socket_get_fd(struct evhttp_bound_socket *bound)
return evconnlistener_get_fd(bound->listener);
}
struct evconnlistener *
evhttp_bound_socket_get_listener(struct evhttp_bound_socket *bound)
{
return bound->listener;
}
void
evhttp_del_accept_socket(struct evhttp *http, struct evhttp_bound_socket *bound)
{

View File

@ -63,6 +63,7 @@ struct evhttp;
struct evhttp_request;
struct evkeyvalq;
struct evhttp_bound_socket;
struct evconnlistener;
/**
* Create a new HTTP server.
@ -130,6 +131,18 @@ int evhttp_accept_socket(struct evhttp *http, evutil_socket_t fd);
*/
struct evhttp_bound_socket *evhttp_accept_socket_with_handle(struct evhttp *http, evutil_socket_t fd);
/**
* The most low-level evhttp_bind/accept method: takes an evconnlistener, and
* returns an evhttp_bound_socket. The listener will be freed when the bound
* socket is freed.
*/
struct evhttp_bound_socket *evhttp_bind_listener(struct evhttp *http, struct evconnlistener *listener);
/**
* Return the listener used to implement a bound socket.
*/
struct evconnlistener *evhttp_bound_socket_get_listener(struct evhttp_bound_socket *bound);
/**
* Makes an HTTP server stop accepting connections on the specified socket
*