size_t IOStreamTCP::read()

in src/dxapi/native/io/tcp.cpp [89:133]


size_t IOStreamTCP::read(byte *to, size_t minSize, size_t maxSize)
{
    if (inputClosed_) {
        THROW_DBGLOG_IO(IOStreamException, ENOTCONN, "IOStreamTCP::read(): ERR: closed");
    }

    assert(maxSize >= minSize);
    size_t bytesRead = 0;

    while (bytesRead < minSize) {
        // TODO: unsigned chars by default!
        int readSize;
#ifdef SOCKET_READ_SIZE_LIMIT
        readSize = (int)min(maxSize - bytesRead, (size_t)SOCKET_READ_SIZE_LIMIT);
#else
        readSize = (int)(maxSize - bytesRead);
#endif
        assert(readSize >= 0 && (unsigned)readSize <= maxSize);
        int result = (int)recv(socket_, (char *)to, readSize, 0);
        if (result <= 0) {
            if (result < 0) {
                int error = socket_errno();
                if (WSAESHUTDOWN != error) {
                    std::string out;
                    int errorCode;
                    DBGLOGERR(&out, "%s", last_socket_error(&errorCode, "IOStreamTCP::read()").c_str());
                    invokeDisconnectionCallback(out, true);
                    throw IOStreamException(errorCode, out);
                }
            }
            else {
                const char *err = "read(): input stream disconnected";
                DBGLOG(err);
                //invokeDisconnectionCallback(out, true);
                throw IOStreamDisconnectException();
            }
        }

        bytesRead += (unsigned)result;
        to += (unsigned)result;
    }
    assert(bytesRead <= maxSize);
    nBytesRead_ += bytesRead;
    return bytesRead;
}