From ca5a51627919c6fb6ab5e23739615a674caa4037 Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Sat, 21 Jul 2001 04:32:42 +0000
Subject: [PATCH] I downloaded new source for lib (only few hours old !!!), and
 made changes on this new source to make non-blocking connection work. I
 tested it, and PQSendQuery and PQGetResult are working fine.

In win32.h I added one line:
#define snprintf _snprintf

Darko Prenosil
---
 src/interfaces/libpq/fe-connect.c | 37 ++++++++++++++++++++++---------
 src/interfaces/libpq/win32.h      |  1 +
 2 files changed, 27 insertions(+), 11 deletions(-)

diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 2582cdee8a8..1b8d1b79776 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.169 2001/07/20 17:45:05 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.170 2001/07/21 04:32:41 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -70,7 +70,7 @@ static SSL_CTX *SSL_context = NULL;
 
 #define NOTIFYLIST_INITIAL_SIZE 10
 #define NOTIFYLIST_GROWBY 10
-
+#define WIN32_NON_BLOCKING_CONNECTIONS
 
 /* ----------
  * Definition of the conninfo parameters and their fallback resources.
@@ -932,9 +932,15 @@ connectDBStart(PGconn *conn)
 	 * Thus, we have make arrangements for all eventualities.
 	 * ----------
 	 */
+#ifndef WIN32
 	if (connect(conn->sock, &conn->raddr.sa, conn->raddr_len) < 0)
 	{
 		if (errno == EINPROGRESS || errno == 0)
+#else
+ if (connect(conn->sock, &conn->raddr.sa, conn->raddr_len) != 0)
+ {
+  if (errno == EINPROGRESS || errno == EWOULDBLOCK)
+#endif
 		{
 
 			/*
@@ -1208,15 +1214,6 @@ keep_going:						/* We will come back to here until there
 			{
 				ACCEPT_TYPE_ARG3 laddrlen;
 
-#ifndef WIN32
-				int			optval;
-
-#else
-				char		optval;
-
-#endif
-				ACCEPT_TYPE_ARG3 optlen = sizeof(optval);
-
 				/*
 				 * Write ready, since we've made it here, so the
 				 * connection has been made.
@@ -1227,6 +1224,10 @@ keep_going:						/* We will come back to here until there
 				 * state waiting for us on the socket.
 				 */
 
+#ifndef WIN32
+				int   optval;
+				ACCEPT_TYPE_ARG3 optlen = sizeof(optval);
+
 				if (getsockopt(conn->sock, SOL_SOCKET, SO_ERROR,
 							   (char *) &optval, &optlen) == -1)
 				{
@@ -1235,6 +1236,20 @@ keep_going:						/* We will come back to here until there
 									  strerror(errno));
 					goto error_return;
 				}
+#else
+				char far  optval[8];
+				ACCEPT_TYPE_ARG3 optlen = sizeof(optval);
+			
+				int OptResult=getsockopt(conn->sock, SOL_SOCKET, SO_ERROR,optval, &optlen);
+				if (OptResult==SOCKET_ERROR)
+				{
+					printfPQExpBuffer(&conn->errorMessage,
+							"PQconnectPoll() -- getsockopt() failed: "
+							"errno=%i\n", errno);
+					connectFailureMessage(conn, OptResult);
+					goto error_return;
+				}
+#endif
 				else if (optval != 0)
 				{
 
diff --git a/src/interfaces/libpq/win32.h b/src/interfaces/libpq/win32.h
index eb250350be4..988455a8ec1 100644
--- a/src/interfaces/libpq/win32.h
+++ b/src/interfaces/libpq/win32.h
@@ -16,6 +16,7 @@
 #define popen(a,b) _popen(a,b)
 #define pclose(a) _pclose(a)
 #define vsnprintf(a,b,c,d) _vsnprintf(a,b,c,d)
+#define snprintf _snprintf
 
 /*
  * crypt not available (yet)
-- 
GitLab