Commit 6024ac1b authored by Bruce Momjian's avatar Bruce Momjian

Back out old version and update with newer patch of:

	Fix for non-blocking connections in libpq

Bernhard Herzog
parent 66cd6a0f
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.115 2002/03/05 05:20:12 momjian Exp $ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.116 2002/03/05 06:07:26 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
......
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.66 2002/03/05 05:20:12 momjian Exp $ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.67 2002/03/05 06:07:26 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -110,20 +110,18 @@ pqPutc(char c, PGconn *conn) ...@@ -110,20 +110,18 @@ pqPutc(char c, PGconn *conn)
static int static int
pqPutBytes(const char *s, size_t nbytes, PGconn *conn) pqPutBytes(const char *s, size_t nbytes, PGconn *conn)
{ {
/* /* Strategy to handle blocking and non-blocking connections: Fill
* Strategy to handle blocking and non-blocking connections: Fill the * the output buffer and flush it repeatedly until either all data
* output buffer and flush it repeatedly until either all data has * has been sent or is at least queued in the buffer.
* been sent or is at least queued in the buffer.
* *
* For non-blocking connections, grow the buffer if not all data fits * For non-blocking connections, grow the buffer if not all data
* into it and the buffer can't be sent because the socket would * fits into it and the buffer can't be sent because the socket
* block. * would block.
*/ */
while (nbytes) while (nbytes)
{ {
size_t avail, size_t avail, remaining;
remaining;
/* fill the output buffer */ /* fill the output buffer */
avail = Max(conn->outBufSize - conn->outCount, 0); avail = Max(conn->outBufSize - conn->outCount, 0);
...@@ -133,10 +131,8 @@ pqPutBytes(const char *s, size_t nbytes, PGconn *conn) ...@@ -133,10 +131,8 @@ pqPutBytes(const char *s, size_t nbytes, PGconn *conn)
s += remaining; s += remaining;
nbytes -= remaining; nbytes -= remaining;
/* /* if the data didn't fit completely into the buffer, try to
* if the data didn't fit completely into the buffer, try to flush * flush the buffer */
* the buffer
*/
if (nbytes) if (nbytes)
{ {
int send_result = pqSendSome(conn); int send_result = pqSendSome(conn);
...@@ -145,23 +141,21 @@ pqPutBytes(const char *s, size_t nbytes, PGconn *conn) ...@@ -145,23 +141,21 @@ pqPutBytes(const char *s, size_t nbytes, PGconn *conn)
if (send_result < 0) if (send_result < 0)
return EOF; return EOF;
/* /* if not all data could be sent, increase the output
* if not all data could be sent, increase the output buffer, * buffer, put the rest of s into it and return
* put the rest of s into it and return successfully. This * successfully. This case will only happen in a
* case will only happen in a non-blocking connection * non-blocking connection
*/ */
if (send_result > 0) if (send_result > 0)
{ {
/* /* try to grow the buffer.
* try to grow the buffer. FIXME: The new size could be * FIXME: The new size could be chosen more
* chosen more intelligently. * intelligently.
*/ */
size_t buflen = conn->outCount + nbytes; size_t buflen = conn->outCount + nbytes;
if (buflen > conn->outBufSize) if (buflen > conn->outBufSize)
{ {
char *newbuf = realloc(conn->outBuffer, buflen); char * newbuf = realloc(conn->outBuffer, buflen);
if (!newbuf) if (!newbuf)
{ {
/* realloc failed. Probably out of memory */ /* realloc failed. Probably out of memory */
...@@ -181,10 +175,8 @@ pqPutBytes(const char *s, size_t nbytes, PGconn *conn) ...@@ -181,10 +175,8 @@ pqPutBytes(const char *s, size_t nbytes, PGconn *conn)
} }
} }
/* /* pqSendSome was able to send all data. Continue with the next
* pqSendSome was able to send all data. Continue with the next * chunk of s. */
* chunk of s.
*/
} /* while */ } /* while */
return 0; return 0;
...@@ -631,19 +623,11 @@ definitelyFailed: ...@@ -631,19 +623,11 @@ definitelyFailed:
return -1; return -1;
} }
/* pqSendSome: send any data waiting in the output buffer and return 0 /*
* if all data was sent, -1 if an error occurred or 1 if not all data * pqSendSome: send any data waiting in the output buffer.
* could be written because the socket would have blocked.
*
* For a blocking connection all data will be sent unless an error
* occurrs. -1 will only be returned if the connection is non-blocking.
*
* Internally, the case of data remaining in the buffer after pqSendSome
* could be determined by looking at outCount, but this function also
* serves as the implementation of the new API function PQsendsome.
* *
* FIXME: perhaps it would be more useful to return the number of bytes * Return 0 on sucess, -1 on failure and 1 when data remains because the
* remaining? * socket would block and the connection is non-blocking.
*/ */
int int
pqSendSome(PGconn *conn) pqSendSome(PGconn *conn)
...@@ -655,7 +639,7 @@ pqSendSome(PGconn *conn) ...@@ -655,7 +639,7 @@ pqSendSome(PGconn *conn)
{ {
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("connection not open\n")); libpq_gettext("connection not open\n"));
return EOF; return -1;
} }
/* /*
...@@ -778,6 +762,7 @@ pqSendSome(PGconn *conn) ...@@ -778,6 +762,7 @@ pqSendSome(PGconn *conn)
} }
/* /*
* pqFlush: send any data waiting in the output buffer * pqFlush: send any data waiting in the output buffer
* *
...@@ -790,7 +775,9 @@ int ...@@ -790,7 +775,9 @@ int
pqFlush(PGconn *conn) pqFlush(PGconn *conn)
{ {
if (pqSendSome(conn)) if (pqSendSome(conn))
{
return EOF; return EOF;
}
return 0; return 0;
} }
...@@ -812,7 +799,7 @@ pqWait(int forRead, int forWrite, PGconn *conn) ...@@ -812,7 +799,7 @@ pqWait(int forRead, int forWrite, PGconn *conn)
{ {
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("connection not open\n")); libpq_gettext("connection not open\n"));
return -1; return EOF;
} }
if (forRead || forWrite) if (forRead || forWrite)
...@@ -919,7 +906,6 @@ const char * ...@@ -919,7 +906,6 @@ const char *
winsock_strerror(int eno) winsock_strerror(int eno)
{ {
static char err_buf[512]; static char err_buf[512];
#define WSSE_MAXLEN (sizeof(err_buf)-1-13) /* 13 for " (0x00000000)" */ #define WSSE_MAXLEN (sizeof(err_buf)-1-13) /* 13 for " (0x00000000)" */
int length; int length;
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: libpq-fe.h,v 1.82 2002/03/05 05:20:12 momjian Exp $ * $Id: libpq-fe.h,v 1.83 2002/03/05 06:07:26 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -282,10 +282,6 @@ extern int PQisnonblocking(const PGconn *conn); ...@@ -282,10 +282,6 @@ extern int PQisnonblocking(const PGconn *conn);
/* Force the write buffer to be written (or at least try) */ /* Force the write buffer to be written (or at least try) */
extern int PQflush(PGconn *conn); extern int PQflush(PGconn *conn);
/*
* Force the write buffer to be written (or at least try)
* (better than PQflush)
*/
extern int PQsendSome(PGconn *conn); extern int PQsendSome(PGconn *conn);
/* /*
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: libpq-int.h,v 1.45 2002/03/05 05:20:12 momjian Exp $ * $Id: libpq-int.h,v 1.46 2002/03/05 06:07:27 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment