Пример #1
0
void ConnectionSet::addConnection( ConnectionPtr connection )
{
    LBASSERT( connection->isConnected() || connection->isListening( ));

    {
        lunchbox::ScopedWrite mutex( _impl->lock );
        _impl->allConnections.push_back( connection );

#ifdef _WIN32
        LBASSERT( _impl->allConnections.size() <
                  MAX_CONNECTIONS * MAX_CONNECTIONS );
        if ( !_isThreadMode && _impl->connections.size() < MAX_CONNECTIONS )
        {
            _impl->connections.push_back( connection );
        }
        else 
        {
            _addConnectionToThread( connection );
        } 
#else
        connection->addListener( _impl );

        LBASSERT( _impl->allConnections.size() < MAX_CONNECTIONS );
#endif // _WIN32
    }

    setDirty();
}
Пример #2
0
bool LocalNode::_connect( NodePtr node, ConnectionPtr connection )
{
    EQASSERT( connection.isValid( ));
    EQASSERT( node->getNodeID() != getNodeID( ));

    if( !node.isValid() || _state != STATE_LISTENING ||
        !connection->isConnected() || node->_state != STATE_CLOSED )
    {
        return false;
    }

    _addConnection( connection );

    // send connect packet to peer
    NodeConnectPacket packet;
    packet.requestID = registerRequest( node.get( ));
    packet.nodeID    = _id;
    packet.nodeType  = getType();
    connection->send( packet, serialize( ));

    bool connected = false;
    if( !waitRequest( packet.requestID, connected, 10000 /*ms*/ ))
    {
        EQWARN << "Node connection handshake timeout - peer not a Collage node?"
               << std::endl;
        return false;
    }
    if( !connected )
        return false;

    EQASSERT( node->_id != NodeID::ZERO );
    EQASSERTINFO( node->_id != _id, _id );
    EQINFO << node << " connected to " << *(Node*)this << std::endl;
    return true;
}
Пример #3
0
void ConnectionSet::addConnection( ConnectionPtr connection )
{
    LBASSERT( connection->isConnected() || connection->isListening( ));

    { 
        lunchbox::ScopedWrite mutex( _impl->lock );
        _impl->allConnections.push_back( connection );

#ifdef _WIN32
        LBASSERT( _impl->allConnections.size() <
                  MAX_CONNECTIONS * MAX_CONNECTIONS );
        if( _impl->connections.size() < MAX_CONNECTIONS - _impl->threads.size())
        {
            // can handle it ourself
            _impl->connections.push_back( connection );
            connection->addListener( this );
        }
        else
        {
            // add to existing thread
            for( ThreadsCIter i = _impl->threads.begin();
                 i != _impl->threads.end(); ++i )
            {
                Thread* thread = *i;
                if( thread->set->getSize() > MAX_CONNECTIONS )
                    continue;

                thread->set->addConnection( connection );
                return;
            }

            // add to new thread
            Thread* thread = new Thread( this );
            thread->set->addConnection( connection );
            thread->set->addConnection( _impl->connections.back( ));
            _impl->connections.pop_back();

            _impl->threads.push_back( thread );
            thread->start();
        }
#else
        _impl->connections.push_back( connection );
        connection->addListener( this );

        LBASSERT( _impl->connections.size() < MAX_CONNECTIONS );
#endif // _WIN32
    }

    setDirty();
}
Пример #4
0
void LocalNode::_removeConnection( ConnectionPtr connection )
{
    EQASSERT( connection.isValid( ));

    _incoming.removeConnection( connection );

    void* buffer( 0 );
    uint64_t bytes( 0 );
    connection->getRecvData( &buffer, &bytes );
    EQASSERTINFO( !connection->isConnected() || buffer, *connection );
    EQASSERT( !buffer || bytes == sizeof( uint64_t ));

    if( !connection->isClosed( ))
        connection->close(); // cancels pending IO's
    delete reinterpret_cast< uint64_t* >( buffer );
}