Beispiel #1
0
bool ConnectionSet::removeConnection( ConnectionPtr connection )
{
    {
        lunchbox::ScopedWrite mutex( _impl->lock );
        ConnectionsIter i = stde::find( _impl->allConnections, connection );
        if( i == _impl->allConnections.end( ))
            return false;

        if( _impl->connection == connection )
            _impl->connection = 0;

#ifdef _WIN32
        ConnectionsIter j = stde::find( _impl->connections, connection );
        if( j == _impl->connections.end( ))
        {
            Threads::iterator k = _impl->threads.begin();
            for( ; k != _impl->threads.end(); ++k )
            {
                Thread* thread = *k;
                if( thread->set.removeConnection( connection ))
                {
                    if( !thread->set.isEmpty( ))
                        return true;

                    if( thread == _impl->thread )
                        _impl->thread = 0;

                    thread->event = EVENT_NONE;
                    thread->join();
                    delete thread;
                    break;
                }
            }
            
            if ( k != _impl->threads.end() )
                _impl->threads.erase( k );
        }
        else
        {
            connection->removeListener( _impl );
            _impl->connections.erase( j );
        }
#else
        connection->removeListener( _impl );
#endif

        _impl->allConnections.erase( i );
    }

    setDirty();
    return true;
}
Beispiel #2
0
void ConnectionSet::_addConnectionToThread( ConnectionPtr connection )
{
    _needRebalance = true;
    lunchbox::ScopedWrite mutex( _impl->lock );
    if ( _impl->threads.size() > 0 ) 
    {
        Thread* minThread = *( std::min_element(_impl->threads.begin(), 
                               _impl->threads.end(), 
                               ThreadConnectionsSizeComparator()) );
        
        if ( minThread->set._impl->connections.size() < MAX_CONNECTIONS )
            minThread->set.addConnection( connection );    
        else 
        {
            Connections connections(1, connection);
            _createThread( connections );
        }
            
    }
    else 
    {
        _isThreadMode = true;
        size_t mid_connections = _impl->connections.size()/2;
        Connections connections1(_impl->connections.begin(), 
                                 _impl->connections.begin() + mid_connections );
        Connections connections2(_impl->connections.begin() + mid_connections, 
                                 _impl->connections.end() );
        connections1.push_back( connection );
        while( !_impl->connections.empty() ) 
        {
            ConnectionPtr lastConnection = _impl->connections.back();
            lastConnection->removeListener( _impl );
            _impl->connections.pop_back();
        }
        _createThread( connections1 );
        _createThread( connections2 );
    }
}