Blocking function read of StreamingSocket


#1

hi jules read function of StreamingSocket being locked.
When parameter blockUntilSpecifiedAmountHasArrived = false
and should not.

A solution I found was.

add two lines of code to the function SocketHelpers :: readSocket

change:

static int readSocket (const int handle,
                           void* const destBuffer, const int maxBytesToRead,
                           bool volatile& connected,
                           const bool blockUntilSpecifiedAmountHasArrived) noexcept
    {
        int bytesRead = 0;
		setSocketBlockingState (handle, false); //line #1 add 
        while (bytesRead < maxBytesToRead)
        {
            int bytesThisTime;

           #if JUCE_WINDOWS
            bytesThisTime = recv (handle, static_cast<char*> (destBuffer) + bytesRead, maxBytesToRead - bytesRead, 0);
           #else
            while ((bytesThisTime = (int) ::read (handle, addBytesToPointer (destBuffer, bytesRead), (size_t) (maxBytesToRead - bytesRead))) < 0
                     && errno == EINTR
                     && connected)
            {
            }
           #endif

            if (bytesThisTime <= 0 || ! connected)
            {
                if (bytesRead == 0)
                    bytesRead = -1;

                break;
            }

            bytesRead += bytesThisTime;

            if (! blockUntilSpecifiedAmountHasArrived)
                break;
        }
		setSocketBlockingState (handle, true); //line # 2 add
        return bytesRead;
    }

#2

The above solution is not as effective as this.

do not post the above.
just change the read function. by the following:

int StreamingSocket::read (void* destBuffer, const int maxBytesToRead, const bool blockUntilSpecifiedAmountHasArrived)
{
	SocketHelpers::setSocketBlockingState (handle, false);
	int ret= (connected && ! isListener) ? SocketHelpers::readSocket (handle, destBuffer, maxBytesToRead, connected, blockUntilSpecifiedAmountHasArrived)
                                       : -1;
	return ret;
}

#3

That’s poor code.
You don’t check the previous blocking state of the socket, yet, you impose your own.
You know about select right (in Juce’s words, it’s Socket::waitFor… ) ?