| 850 | } |
| 851 | |
| 852 | bool TcpSocket::update |
| 853 | ( |
| 854 | ) |
| 855 | { |
| 856 | if(!_OnUpdate()) |
| 857 | return false; |
| 858 | |
| 859 | if(!isOpen()) |
| 860 | return false; |
| 861 | |
| 862 | if(!_inbuf) |
| 863 | SetBufsizeIn(DEFAULT_BUFSIZE); |
| 864 | |
| 865 | int bytes = _readBytes((unsigned char*)_writeptr, _writeSize); |
| 866 | //traceprint("TcpSocket::update: _readBytes() result %d\n", bytes); |
| 867 | if(bytes > 0) // we received something |
| 868 | { |
| 869 | _inbuf[bytes] = 0; |
| 870 | _recvSize = bytes; |
| 871 | |
| 872 | // reset pointers for next read |
| 873 | _writeSize = _inbufSize - 1; |
| 874 | _readptr = _writeptr = _inbuf; |
| 875 | |
| 876 | _OnData(); |
| 877 | } |
| 878 | else if(bytes == 0) // remote has closed the connection |
| 879 | { |
| 880 | close(); |
| 881 | } |
| 882 | else // whoops, error? |
| 883 | { |
| 884 | // Possible that the error is returned directly (in that case, < -1, or -1 is returned and the error has to be retrieved seperately. |
| 885 | // But in the latter case, error numbers may be positive (at least on windows...) |
| 886 | int err = bytes == -1 ? _GetError() : bytes; |
| 887 | switch(err) |
| 888 | { |
| 889 | case EWOULDBLOCK: |
| 890 | #if defined(EAGAIN) && (EWOULDBLOCK != EAGAIN) |
| 891 | case EAGAIN: // linux man pages say this can also happen instead of EWOULDBLOCK |
| 892 | #endif |
| 893 | return false; |
| 894 | |
| 895 | #ifdef MINIHTTP_USE_POLARSSL |
| 896 | case POLARSSL_ERR_NET_WANT_READ: |
| 897 | break; // Try again later |
| 898 | #endif |
| 899 | |
| 900 | default: |
| 901 | traceprint("SOCKET UPDATE ERROR: (%d): %s\n", err, _GetErrorStr(err).c_str()); |
| 902 | case ECONNRESET: |
| 903 | case ENOTCONN: |
| 904 | case ETIMEDOUT: |
| 905 | #ifdef _WIN32 |
| 906 | case WSAECONNABORTED: |
| 907 | case WSAESHUTDOWN: |
| 908 | #endif |
| 909 | close(); |
no test coverage detected