Tweak the evconnlistener interface a little.

svn:r1295
This commit is contained in:
Nick Mathewson 2009-05-18 16:15:56 +00:00
parent dc4c7b9570
commit ed1bbc7a9f
3 changed files with 18 additions and 3 deletions

View File

@ -35,12 +35,13 @@ struct evconnlistener;
/** /**
A callback that we invoke when a listener has a new connection. A callback that we invoke when a listener has a new connection.
@param listener The evconnlistener
@param fd The new file descriptor @param fd The new file descriptor
@param addr The source address of the connection @param addr The source address of the connection
@param socklen The length of addr @param socklen The length of addr
@param user_arg the pointer passed to evconnlistener_new() @param user_arg the pointer passed to evconnlistener_new()
*/ */
typedef void (*evconnlistener_cb)(evutil_socket_t, struct sockaddr *, int socklen, void *); typedef void (*evconnlistener_cb)(struct evconnlistener *, evutil_socket_t, struct sockaddr *, int socklen, void *);
/** Flag: Indicates that we should not make incoming sockets nonblocking /** Flag: Indicates that we should not make incoming sockets nonblocking
* before passing them to the callback. */ * before passing them to the callback. */
@ -64,6 +65,7 @@ typedef void (*evconnlistener_cb)(evutil_socket_t, struct sockaddr *, int sockle
@param flags Any number of LEV_OPT_* flags @param flags Any number of LEV_OPT_* flags
@param backlog Passed to the listen() call to determine the length of the @param backlog Passed to the listen() call to determine the length of the
acceptable connection backlog. Set to -1 for a reasonable default. acceptable connection backlog. Set to -1 for a reasonable default.
Set to 0 if the socket is already listening.
@param fd The file descriptor to listen on. It must be a nonblocking @param fd The file descriptor to listen on. It must be a nonblocking
file descriptor, and it should already be bound to an appropriate file descriptor, and it should already be bound to an appropriate
port and address. port and address.
@ -100,4 +102,7 @@ int evconnlistener_enable(struct evconnlistener *lev);
*/ */
int evconnlistener_disable(struct evconnlistener *lev); int evconnlistener_disable(struct evconnlistener *lev);
/** Return an evconnlistener's associated event_base. */
struct event_base *evconnlistener_get_base(struct evconnlistener *lev);
#endif #endif

View File

@ -70,6 +70,9 @@ evconnlistener_new(struct event_base *base,
if (backlog > 0) { if (backlog > 0) {
if (listen(fd, backlog) < 0) if (listen(fd, backlog) < 0)
return NULL; return NULL;
} else if (backlog < 0) {
if (listen(fd, 128) < 0)
return NULL;
} }
lev = mm_calloc(1, sizeof(struct evconnlistener)); lev = mm_calloc(1, sizeof(struct evconnlistener));
if (!lev) if (!lev)
@ -142,6 +145,12 @@ evconnlistener_disable(struct evconnlistener *lev)
return event_del(&lev->listener); return event_del(&lev->listener);
} }
struct event_base *
evconnlistener_get_base(struct evconnlistener *lev)
{
return event_get_base(&lev->listener);
}
static void static void
listener_read_cb(evutil_socket_t fd, short what, void *p) listener_read_cb(evutil_socket_t fd, short what, void *p)
{ {
@ -158,7 +167,7 @@ listener_read_cb(evutil_socket_t fd, short what, void *p)
if (!(lev->flags & LEV_OPT_LEAVE_SOCKETS_BLOCKING)) if (!(lev->flags & LEV_OPT_LEAVE_SOCKETS_BLOCKING))
evutil_make_socket_nonblocking(new_fd); evutil_make_socket_nonblocking(new_fd);
lev->cb(new_fd, (struct sockaddr*)&ss, (int)socklen, lev->cb(lev, new_fd, (struct sockaddr*)&ss, (int)socklen,
lev->user_data); lev->user_data);
} }
err = evutil_socket_geterror(fd); err = evutil_socket_geterror(fd);

View File

@ -390,7 +390,8 @@ static int n_strings_read = 0;
#define TEST_STR "Now is the time for all good events to signal for " \ #define TEST_STR "Now is the time for all good events to signal for " \
"the good of their protocol" "the good of their protocol"
static void static void
listen_cb(evutil_socket_t fd, struct sockaddr *sa, int socklen, void *arg) listen_cb(struct evconnlistener *listener, evutil_socket_t fd,
struct sockaddr *sa, int socklen, void *arg)
{ {
struct event_base *base = arg; struct event_base *base = arg;
struct bufferevent *bev; struct bufferevent *bev;