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
This commit is contained in:
Nick Mathewson 2009-08-09 20:17:29 +00:00
parent 8a99083f01
commit 800f9aa607
2 changed files with 16 additions and 9 deletions

View File

@ -280,7 +280,7 @@ bufferevent_socket_connect(struct bufferevent *bev,
EVUTIL_UPCAST(bev, struct bufferevent_private, bev); EVUTIL_UPCAST(bev, struct bufferevent_private, bev);
evutil_socket_t fd; evutil_socket_t fd;
int r; int r = 0;
int result=-1; int result=-1;
int ownfd = 0; int ownfd = 0;
@ -291,6 +291,8 @@ bufferevent_socket_connect(struct bufferevent *bev,
fd = bufferevent_getfd(bev); fd = bufferevent_getfd(bev);
if (fd < 0) { if (fd < 0) {
if (!sa)
goto done;
fd = socket(sa->sa_family, SOCK_STREAM, 0); fd = socket(sa->sa_family, SOCK_STREAM, 0);
if (fd < 0) if (fd < 0)
goto done; goto done;
@ -298,15 +300,16 @@ bufferevent_socket_connect(struct bufferevent *bev,
goto done; goto done;
ownfd = 1; ownfd = 1;
} }
r = evutil_socket_connect(&fd, sa, socklen); if (sa) {
if (r < 0) { r = evutil_socket_connect(&fd, sa, socklen);
_bufferevent_run_eventcb(bev, BEV_EVENT_ERROR); if (r < 0) {
if (ownfd) _bufferevent_run_eventcb(bev, BEV_EVENT_ERROR);
EVUTIL_CLOSESOCKET(fd); if (ownfd)
/* do something about the error? */ EVUTIL_CLOSESOCKET(fd);
goto done; /* do something about the error? */
goto done;
}
} }
bufferevent_setfd(bev, fd); bufferevent_setfd(bev, fd);
if (r == 0) { if (r == 0) {
if (! bufferevent_enable(bev, EV_WRITE)) { if (! bufferevent_enable(bev, EV_WRITE)) {

View File

@ -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 If the bufferevent does not already have a socket set, we allocate a new
socket here and make it nonblocking before we begin. 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 @param bufev an existing bufferevent allocated with
bufferevent_socket_new(). bufferevent_socket_new().
@param addr the address we should connect to @param addr the address we should connect to