From: Nick Mathewson Date: Sun, 9 Aug 2009 20:17:29 +0000 (+0000) Subject: When bufferevent_socket_connect is called with no address, assume that our existing... X-Git-Tag: release-2.0.3-alpha~127 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=800f9aa607e4fd48d50615c10bb6f71d831c7071;p=libevent When bufferevent_socket_connect is called with no address, assume that our existing fd is connecting and put the connection into "connecting" mode. svn:r1411 --- diff --git a/bufferevent_sock.c b/bufferevent_sock.c index f1eb0777..035add36 100644 --- a/bufferevent_sock.c +++ b/bufferevent_sock.c @@ -280,7 +280,7 @@ bufferevent_socket_connect(struct bufferevent *bev, EVUTIL_UPCAST(bev, struct bufferevent_private, bev); evutil_socket_t fd; - int r; + int r = 0; int result=-1; int ownfd = 0; @@ -291,6 +291,8 @@ bufferevent_socket_connect(struct bufferevent *bev, fd = bufferevent_getfd(bev); if (fd < 0) { + if (!sa) + goto done; fd = socket(sa->sa_family, SOCK_STREAM, 0); if (fd < 0) goto done; @@ -298,15 +300,16 @@ bufferevent_socket_connect(struct bufferevent *bev, goto done; ownfd = 1; } - r = evutil_socket_connect(&fd, sa, socklen); - if (r < 0) { - _bufferevent_run_eventcb(bev, BEV_EVENT_ERROR); - if (ownfd) - EVUTIL_CLOSESOCKET(fd); - /* do something about the error? */ - goto done; + if (sa) { + r = evutil_socket_connect(&fd, sa, socklen); + if (r < 0) { + _bufferevent_run_eventcb(bev, BEV_EVENT_ERROR); + if (ownfd) + EVUTIL_CLOSESOCKET(fd); + /* do something about the error? */ + goto done; + } } - bufferevent_setfd(bev, fd); if (r == 0) { if (! bufferevent_enable(bev, EV_WRITE)) { diff --git a/include/event2/bufferevent.h b/include/event2/bufferevent.h index cc3be050..f8eae402 100644 --- a/include/event2/bufferevent.h +++ b/include/event2/bufferevent.h @@ -150,6 +150,10 @@ struct bufferevent *bufferevent_socket_new(struct event_base *base, evutil_socke If the bufferevent does not already have a socket set, we allocate a new socket here and make it nonblocking before we begin. + If no address is provided, we assume that the socket is already connecting, + and configure the bufferevent so that a BEV_EVENT_CONNECTED event will be + yielded when it is done connecting. + @param bufev an existing bufferevent allocated with bufferevent_socket_new(). @param addr the address we should connect to