---------- * pqReadData: read more data, if any is available * Possible return values: * 1: successfully loaded at least one more byte * 0: no data is presently available, but no error detected * -1: error detected (including EOF = connection closure); * conn->errorMessage set * NOTE: callers must not assume that pointers or indexes into conn->inBuffer * remain valid across this call!
| 615 | * ---------- |
| 616 | */ |
| 617 | int |
| 618 | pqReadData(PGconn *conn) |
| 619 | { |
| 620 | int someread = 0; |
| 621 | int nread; |
| 622 | |
| 623 | if (conn->sock == PGINVALID_SOCKET) |
| 624 | { |
| 625 | appendPQExpBufferStr(&conn->errorMessage, |
| 626 | libpq_gettext("connection not open\n")); |
| 627 | return -1; |
| 628 | } |
| 629 | |
| 630 | /* Left-justify any data in the buffer to make room */ |
| 631 | if (conn->inStart < conn->inEnd) |
| 632 | { |
| 633 | if (conn->inStart > 0) |
| 634 | { |
| 635 | memmove(conn->inBuffer, conn->inBuffer + conn->inStart, |
| 636 | conn->inEnd - conn->inStart); |
| 637 | conn->inEnd -= conn->inStart; |
| 638 | conn->inCursor -= conn->inStart; |
| 639 | conn->inStart = 0; |
| 640 | } |
| 641 | } |
| 642 | else |
| 643 | { |
| 644 | /* buffer is logically empty, reset it */ |
| 645 | conn->inStart = conn->inCursor = conn->inEnd = 0; |
| 646 | } |
| 647 | |
| 648 | /* |
| 649 | * If the buffer is fairly full, enlarge it. We need to be able to enlarge |
| 650 | * the buffer in case a single message exceeds the initial buffer size. We |
| 651 | * enlarge before filling the buffer entirely so as to avoid asking the |
| 652 | * kernel for a partial packet. The magic constant here should be large |
| 653 | * enough for a TCP packet or Unix pipe bufferload. 8K is the usual pipe |
| 654 | * buffer size, so... |
| 655 | */ |
| 656 | if (conn->inBufSize - conn->inEnd < 8192) |
| 657 | { |
| 658 | if (pqCheckInBufferSpace(conn->inEnd + (size_t) 8192, conn)) |
| 659 | { |
| 660 | /* |
| 661 | * We don't insist that the enlarge worked, but we need some room |
| 662 | */ |
| 663 | if (conn->inBufSize - conn->inEnd < 100) |
| 664 | return -1; /* errorMessage already set */ |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | /* OK, try to read some data */ |
| 669 | retry3: |
| 670 | nread = pqsecure_read(conn, conn->inBuffer + conn->inEnd, |
| 671 | conn->inBufSize - conn->inEnd); |
| 672 | if (nread < 0) |
| 673 | { |
| 674 | switch (SOCK_ERRNO) |
no test coverage detected