* pqSendSome: send data waiting in the output buffer. * * len is how much to try to send (typically equal to outCount, but may * be less). * * Return 0 on success, -1 on failure and 1 when not all data could be sent * because the socket would block and the connection is non-blocking. * * Note that this is also responsible for consuming data from the socket * (putting it in conn->inBuffer)
| 835 | * (Thus, a -1 result is returned only for an internal *read* failure.) |
| 836 | */ |
| 837 | static int |
| 838 | pqSendSome(PGconn *conn, int len) |
| 839 | { |
| 840 | char *ptr = conn->outBuffer; |
| 841 | int remaining = conn->outCount; |
| 842 | int oldmsglen = conn->errorMessage.len; |
| 843 | int result = 0; |
| 844 | |
| 845 | /* |
| 846 | * If we already had a write failure, we will never again try to send data |
| 847 | * on that connection. Even if the kernel would let us, we've probably |
| 848 | * lost message boundary sync with the server. conn->write_failed |
| 849 | * therefore persists until the connection is reset, and we just discard |
| 850 | * all data presented to be written. However, as long as we still have a |
| 851 | * valid socket, we should continue to absorb data from the backend, so |
| 852 | * that we can collect any final error messages. |
| 853 | */ |
| 854 | if (conn->write_failed) |
| 855 | { |
| 856 | /* conn->write_err_msg should be set up already */ |
| 857 | conn->outCount = 0; |
| 858 | /* Absorb input data if any, and detect socket closure */ |
| 859 | if (conn->sock != PGINVALID_SOCKET) |
| 860 | { |
| 861 | if (pqReadData(conn) < 0) |
| 862 | return -1; |
| 863 | } |
| 864 | return 0; |
| 865 | } |
| 866 | |
| 867 | if (conn->sock == PGINVALID_SOCKET) |
| 868 | { |
| 869 | conn->write_failed = true; |
| 870 | /* Insert error message into conn->write_err_msg, if possible */ |
| 871 | /* (strdup failure is OK, we'll cope later) */ |
| 872 | conn->write_err_msg = strdup(libpq_gettext("connection not open\n")); |
| 873 | /* Discard queued data; no chance it'll ever be sent */ |
| 874 | conn->outCount = 0; |
| 875 | return 0; |
| 876 | } |
| 877 | |
| 878 | /* while there's still data to send */ |
| 879 | while (len > 0) |
| 880 | { |
| 881 | int sent; |
| 882 | |
| 883 | #ifndef WIN32 |
| 884 | sent = pqsecure_write(conn, ptr, len); |
| 885 | #else |
| 886 | |
| 887 | /* |
| 888 | * Windows can fail on large sends, per KB article Q201213. The |
| 889 | * failure-point appears to be different in different versions of |
| 890 | * Windows, but 64k should always be safe. |
| 891 | */ |
| 892 | sent = pqsecure_write(conn, ptr, Min(len, 65536)); |
| 893 | #endif |
| 894 |
no test coverage detected