[Python-checkins] python/dist/src/Modules socketmodule.c,1.226,1.227

gvanrossum@users.sourceforge.net gvanrossum@users.sourceforge.net
Thu, 13 Jun 2002 09:07:08 -0700


Update of /cvsroot/python/python/dist/src/Modules
In directory usw-pr-cvs1:/tmp/cvs-serv17318/Modules

Modified Files:
	socketmodule.c 
Log Message:
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).


Index: socketmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/socketmodule.c,v
retrieving revision 1.226
retrieving revision 1.227
diff -C2 -d -r1.226 -r1.227
*** socketmodule.c	13 Jun 2002 15:07:44 -0000	1.226
--- socketmodule.c	13 Jun 2002 16:07:04 -0000	1.227
***************
*** 1271,1274 ****
--- 1271,1311 ----
  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 */
***************
*** 1285,1300 ****
  
  	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);
  	Py_END_ALLOW_THREADS
  
! 	if (res < 0)
  		return s->errorhandler();
  	Py_INCREF(Py_None);
--- 1322,1329 ----
  
  	Py_BEGIN_ALLOW_THREADS
! 	res = internal_connect(s, addr, addrlen);
  	Py_END_ALLOW_THREADS
  
! 	if (res != 0)
  		return s->errorhandler();
  	Py_INCREF(Py_None);
***************
*** 1322,1343 ****
  
  	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);
  	Py_END_ALLOW_THREADS
- 
- 	if (res != 0) {
- #ifdef MS_WINDOWS
- 		res = WSAGetLastError();
- #else
- 		res = errno;
- #endif
- 	}
  
  	return PyInt_FromLong((long) res);
--- 1351,1356 ----
  
  	Py_BEGIN_ALLOW_THREADS
! 	res = internal_connect(s, addr, addrlen);
  	Py_END_ALLOW_THREADS
  
  	return PyInt_FromLong((long) res);