diff options
author | Guido van Rossum <guido@python.org> | 2002-06-13 16:07:04 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2002-06-13 16:07:04 (GMT) |
commit | 7b8bac106aaddc6d9d438309f06cd3a51e701753 (patch) | |
tree | 55bc7dcf1d053c7bb25edf3d712573f9b83a4493 /Modules/socketmodule.c | |
parent | 129b17d538ddb5fa4df2d2d8ec1876d9a9ea4355 (diff) | |
download | cpython-7b8bac106aaddc6d9d438309f06cd3a51e701753.zip cpython-7b8bac106aaddc6d9d438309f06cd3a51e701753.tar.gz cpython-7b8bac106aaddc6d9d438309f06cd3a51e701753.tar.bz2 |
Fix non-blocking connect() for Windows. Refactored the code
that retries the connect() call in timeout mode so it can be shared
between connect() and connect_ex(), and needs only a single #ifdef.
The test for this was doing funky stuff I don't approve of,
so I removed it in favor of a simpler test. This allowed me
to implement a simpler, "purer" form of the timeout retry code.
Hopefully that's enough (if you want to be fancy, use non-blocking
mode and decode the errors yourself, like before).
Diffstat (limited to 'Modules/socketmodule.c')
-rw-r--r-- | Modules/socketmodule.c | 67 |
1 files changed, 40 insertions, 27 deletions
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 3896831..1da9afb 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -1270,6 +1270,43 @@ static char close_doc[] = \n\ Close the socket. It cannot be used after this call."; +static int +internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen) +{ + int res; + + res = connect(s->sock_fd, addr, addrlen); + +#ifdef MS_WINDOWS + + if (s->sock_timeout > 0.0) { + if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK) { + internal_select(s, 1); + res = connect(s->sock_fd, addr, addrlen); + if (res < 0 && WSAGetLastError() == WSAEISCONN) + res = 0; + } + } + + if (res < 0) + res = WSAGetLastError(); + +#else + + if (s->sock_timeout > 0.0) { + if (res < 0 && errno == EINPROGRESS) { + internal_select(s, 1); + res = connect(s->sock_fd, addr, addrlen); + } + } + + if (res < 0) + res = errno; + +#endif + + return res; +} /* s.connect(sockaddr) method */ @@ -1284,18 +1321,10 @@ sock_connect(PySocketSockObject *s, PyObject *addro) return NULL; Py_BEGIN_ALLOW_THREADS - if (s->sock_timeout > 0.0) { - res = connect(s->sock_fd, addr, addrlen); - if (res == EINPROGRESS) { - internal_select(s, 1); - res = connect(s->sock_fd, addr, addrlen); - } - } - else - res = connect(s->sock_fd, addr, addrlen); + res = internal_connect(s, addr, addrlen); Py_END_ALLOW_THREADS - if (res < 0) + if (res != 0) return s->errorhandler(); Py_INCREF(Py_None); return Py_None; @@ -1321,25 +1350,9 @@ sock_connect_ex(PySocketSockObject *s, PyObject *addro) return NULL; Py_BEGIN_ALLOW_THREADS - if (s->sock_timeout > 0.0) { - res = connect(s->sock_fd, addr, addrlen); - if (res == EINPROGRESS) { - internal_select(s, 1); - res = connect(s->sock_fd, addr, addrlen); - } - } - else - res = connect(s->sock_fd, addr, addrlen); + res = internal_connect(s, addr, addrlen); Py_END_ALLOW_THREADS - if (res != 0) { -#ifdef MS_WINDOWS - res = WSAGetLastError(); -#else - res = errno; -#endif - } - return PyInt_FromLong((long) res); } |