Ok, i figured something out anyway ... hopefully it will be helpful to the
group ...
The following works on my WinXP pro machine most of the time. I have
confirmed
that I have the latest versions of everything.
///////////////////////////////////
#pragma once
#include <stdlib.h>
#include <libpq-fe.h>
#include <windows.h>
#include <winsock2.h>
PGconn* asyncconnect(const char* cs,bool* lp_USER_cancel){
int pqcp = 0;
PGconn* lpcn = 0;
if (lpcn = PQconnectStart(cs)){
if (CONNECTION_BAD != PQstatus(lpcn)){
bool keepon = true;
while (keepon && !*lp_USER_cancel){
switch(pqcp = PQconnectPoll(lpcn)){
case PGRES_POLLING_READING:
{
SOCKET s = PQsocket(lpcn);
timeval tv = {0,100000};
int ns = 0;
while (!*lp_USER_cancel && !ns){
fd_set fdr,fde;
FD_ZERO(&fdr);
FD_ZERO(&fde);
FD_SET(s,&fdr);
FD_SET(s,&fde);
ns = select(0,&fdr,0,&fde,&tv);
};
};
break;
case PGRES_POLLING_FAILED:
case PGRES_POLLING_OK:
keepon = false;
break;
};
// NO!
// Sleep(1);
/******************
I'm guessing the connection gets discoed while the
thread is sleeping, resetting
the error message buffer to something other than
the initial error?
if we use a select(), (the right way) we do not
have to sleep. Still, even the select does NOT always
allow for catching it in time.
IMHO, the error that causes
the disco should never be overwritten by the disco
message itself:
'server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.'
within the internal connection thread, IF this is
indeed what is happening.
*********************/
};
};
};
return lpcn;
};
int main(int na,char** sa){
char* host = "localhost";
unsigned short port = 5432;
char* dbname = "nonexistantdb";
char* user = "";
char* password = "";
bool cancel = false;
char cs[1024];
sprintf(
cs,
"host=%s port=%u dbname=%s user=%s password=%s",
host,port,dbname,user,password
);
if (PGconn* lpcn = asyncconnect(cs,&cancel)){
printf("PQerrorMessage(lpcn) returns:\n\n%s\n\nPQstatus(lpcn) returns
%d\n",PQerrorMessage(lpcn),PQstatus(lpcn));
PQfinish(lpcn);
};
return 0;
};
/////////////////////////////////////////////////
cheers, madhtr
----- Original Message -----
From: "Tom Lane" <tgl@xxxxxxxxxxxxx>
To: "madhtr" <madhtr@xxxxxxxxx>
Cc: <pgsql-general@xxxxxxxxxxxxxx>
Sent: Tuesday, August 14, 2007 21:42
Subject: Re: pqlib in c++: PQconnectStart PQconnectPoll
"madhtr" <madhtr@xxxxxxxxx> writes:
From: "Tom Lane" <tgl@xxxxxxxxxxxxx>
... although it takes a good long while (several seconds) because of the
"sleep(1)" in the interaction with the postmaster.
Sleep(1) should be only 1/1000 of a second. I do that so I don't hammer
the
processor with my while loop when i am not using a select().
Ah. I was interpreting it in Unix terms, where sleep() measures in
seconds. With a wait of a few msec it might not be too intolerable.
regards, tom lane
---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match
---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@xxxxxxxxxxxxxx so that your
message can get through to the mailing list cleanly