Ejemplo n.º 1
0
//----------------------------------------------------------------------
// read
//----------------------------------------------------------------------
void Connection::recvNB( void* buffer, const uint64_t bytes )
{
    EQASSERT( !_aioBuffer );
    EQASSERT( !_aioBytes );
    EQASSERT( buffer );
    EQASSERT( bytes );

    _aioBuffer = buffer;
    _aioBytes  = bytes;
    readNB( buffer, bytes );
}
Ejemplo n.º 2
0
//----------------------------------------------------------------------
// read
//----------------------------------------------------------------------
void Connection::recvNB( BufferPtr buffer, const uint64_t bytes )
{
    LBASSERT( !_impl->buffer );
    LBASSERT( _impl->bytes == 0 );
    LBASSERT( buffer );
    LBASSERT( bytes > 0 );
    LBASSERTINFO( bytes < LB_BIT48,
                  "Out-of-sync network stream: read size " << bytes << "?" );

    _impl->buffer = buffer;
    _impl->bytes = bytes;
    buffer->reserve( buffer->getSize() + bytes );
    readNB( buffer->getData() + buffer->getSize(), bytes );
}
Ejemplo n.º 3
0
bool Connection::recvSync( BufferPtr& outBuffer, const bool block )
{
    LBASSERTINFO( _impl->buffer,
                  "No pending receive on " << getDescription()->toString( ));

    // reset async IO data
    outBuffer = _impl->buffer;
    const uint64_t bytes = _impl->bytes;
    _impl->buffer = 0;
    _impl->bytes = 0;

    if( _impl->state != STATE_CONNECTED || !outBuffer || bytes == 0 )
        return false;
    LBASSERTINFO( bytes < LB_BIT48,
                  "Out-of-sync network stream: read size " << bytes << "?" );
#ifdef STATISTICS
    _impl->inBytes += bytes;
#endif

    // 'Iterators' for receive loop
    uint8_t* ptr = outBuffer->getData() + outBuffer->getSize();
    uint64_t bytesLeft = bytes;
    int64_t got = readSync( ptr, bytesLeft, block );

    // WAR: fluke notification: On Win32, we get occasionally a data
    // notification and then deadlock when reading from the connection. The
    // callee (Node::handleData) will flag the first read, the underlying
    // SocketConnection will not block and we will restore the AIO operation if
    // no data was present.
    if( got == READ_TIMEOUT )
    {
        _impl->buffer = outBuffer;
        _impl->bytes = bytes;
        outBuffer = 0;
        return true;
    }

    // From here on, receive loop until all data read or error
    while( true )
    {
        if( got < 0 ) // error
        {
            const uint64_t read = bytes - bytesLeft;
            outBuffer->resize( outBuffer->getSize() + read );
            if( bytes == bytesLeft )
                LBDEBUG << "Read on dead connection" << std::endl;
            else
                LBERROR << "Error during read after " << read << " bytes on "
                        << _impl->description << std::endl;
            return false;
        }
        else if( got == 0 )
        {
            // ConnectionSet::select may report data on an 'empty' connection.
            // If we have nothing read so far, we have hit this case.
            if( bytes == bytesLeft )
                return false;
            LBVERB << "Zero bytes read" << std::endl;
        }
        if( bytesLeft > static_cast< uint64_t >( got )) // partial read
        {
            ptr += got;
            bytesLeft -= got;

            readNB( ptr, bytesLeft );
            got = readSync( ptr, bytesLeft, true );
            continue;
        }

        // read done
        LBASSERTINFO( static_cast< uint64_t >( got ) == bytesLeft,
                      got << " != " << bytesLeft );

        outBuffer->resize( outBuffer->getSize() + bytes );
#ifndef NDEBUG
        if( bytes <= 1024 && ( lunchbox::Log::topics & LOG_PACKETS ))
        {
            ptr -= (bytes - bytesLeft); // rewind
            LBINFO << "recv:" << lunchbox::format( ptr, bytes ) << std::endl;
        }
#endif
        return true;
    }

    LBUNREACHABLE;
    return true;
}
Ejemplo n.º 4
0
bool Connection::recvSync( void** outBuffer, uint64_t* outBytes,
                           const bool block )
{
    // set up async IO data
    EQASSERT( _aioBuffer );
    EQASSERT( _aioBytes );

    if( outBuffer )
        *outBuffer = _aioBuffer;
    if( outBytes )
        *outBytes = _aioBytes;

    void* buffer( _aioBuffer );
    const uint64_t bytes( _aioBytes );
    _aioBuffer = 0;
    _aioBytes  = 0;

    if( _state != STATE_CONNECTED || !buffer || !bytes )
        return false;

    // 'Iterator' data for receive loop
    uint8_t* ptr = static_cast< uint8_t* >( buffer );
    uint64_t bytesLeft = bytes;

    // WAR: On Win32, we get occasionally a data notification and then deadlock
    // when reading from the connection. The callee (Node::handleData) will flag
    // the first read, the underlying SocketConnection will not block and we
    // will restore the AIO operation if no data was present.
    int64_t got = readSync( ptr, bytesLeft, block );
    if( got == READ_TIMEOUT ) // fluke notification
    {
        EQASSERTINFO( bytesLeft == bytes, bytesLeft << " != " << bytes );
        if( outBytes )
            *outBytes = 0;

        _aioBuffer = buffer;
        _aioBytes  = bytes;
        return true;
    }

    // From here on, blocking receive loop until all data read or error
    while( true )
    {
        if( got < 0 ) // error
        {
            if( outBytes )
                *outBytes -= bytesLeft;
            if( bytes == bytesLeft )
                EQINFO << "Read on dead connection" << std::endl;
            else
                EQERROR << "Error during read after " << bytes - bytesLeft
                        << " bytes on " << _description << std::endl;
            return false;
        }
        else if( got == 0 )
        {
            // ConnectionSet::select may report data on an 'empty' connection.
            // If we have nothing read so far, we have hit this case.
            if( bytes == bytesLeft )
            {
                if( outBytes )
                    *outBytes = 0;
                return false;
            }
            EQVERB << "Zero bytes read" << std::endl;
        }

        if( bytesLeft > static_cast< uint64_t >( got )) // partial read
        {
            ptr += got;
            bytesLeft -= got;

            readNB( ptr, bytesLeft );
            got = readSync( ptr, bytesLeft, true );
        }
        else
        {
            EQASSERTINFO( static_cast< uint64_t >( got ) == bytesLeft,
                          got << " != " << bytesLeft );

#ifndef NDEBUG
            if( bytes <= 1024 && ( base::Log::topics & LOG_PACKETS ))
            {
                ptr = static_cast< uint8_t* >( buffer );
                EQINFO << "recv:" << std::hex << base::disableFlush
                       << base::disableHeader;
                for( size_t i = 0; i < bytes; ++i )
                {
                    if( (i % 16) == 0 )
                        EQINFO << std::endl;
                    if( (i % 4) == 0 )
                        EQINFO << " 0x";
                    EQINFO << std::setfill( '0' ) << std::setw(2)
                           << static_cast< unsigned >( ptr[ i ] );
                }
                EQINFO << std::dec << base::enableFlush
                       << std::endl << base::enableHeader;
            }
#endif
            return true;
        }
    }

    EQUNREACHABLE;
    return true;
}