Пример #1
0
int HttpListener::addConnection( struct conn_data * pCur, int *iCount )
{
    int fd = pCur->fd;
    if ( checkAccess( pCur ))
    {
        no_timewait( fd );
        close( fd );
        --(*iCount);
        return 0;
    }
    HttpConnection* pConn = HttpConnPool::getConnection();
    if ( !pConn )
    {
        ERR_NO_MEM( "HttpConnPool::getConnection()" );
        close( fd );
        --(*iCount);
        return -1;
    }
    VHostMap * pMap;
    if ( m_pSubIpMap )
    {
        pMap = getSubMap( fd );
    }
    else
        pMap = getVHostMap();
    pConn->setVHostMap( pMap );
    pConn->setLogger( getLogger());
    pConn->setRemotePort( ntohs( ((sockaddr_in *)(pCur->achPeerAddr))->sin_port) );
    if ( pConn->setLink( pCur->fd, pCur->pInfo, pMap->getSSLContext() ) )
    {
        HttpConnPool::recycle( pConn );
        close( fd );
        --(*iCount);
        return -1;
    }
    fcntl( fd, F_SETFD, FD_CLOEXEC );
    fcntl( fd, F_SETFL, HttpGlobals::getMultiplexer()->getFLTag() );
    //pConn->tryRead();
    return 0;
}
Пример #2
0
int HttpListener::batchAddConn( struct conn_data * pBegin,
                            struct conn_data *pEnd, int *iCount )
{
    struct conn_data * pCur = pBegin;
    int n = pEnd - pBegin;
    while( pCur < pEnd )
    {
        if ( checkAccess( pCur))
        {
            no_timewait( pCur->fd );
            close( pCur->fd );
            pCur->fd = -1;
            --(*iCount);
            --n;
        }
        ++pCur;
    }
    if ( n <= 0 )
        return 0;
    HttpConnection* pConns[CONN_BATCH_SIZE];
    int ret = HttpConnPool::getConnections( pConns, n);
    pCur = pBegin;
    if ( ret <= 0 )
    {
        ERR_NO_MEM( "HttpConnPool::getConnections()" );
        LOG_ERR(( "need %d connections, allocated %d connections!", n, ret ));
        while( pCur < pEnd )
        {
            if ( pCur->fd != -1 )
            {
                close( pCur->fd );
                --(*iCount);
            }
            ++pCur;
        }
        return -1;
    }
    HttpConnection** pConnEnd = &pConns[ret];
    HttpConnection** pConnCur = pConns;
    VHostMap * pMap;
    int flag = HttpGlobals::getMultiplexer()->getFLTag();
    while( pCur < pEnd )
    {
        register int fd = pCur->fd;
        if ( fd != -1 )
        {
            assert( pConnCur < pConnEnd );
            HttpConnection * pConn = *pConnCur;

            if ( m_pSubIpMap )
            {
                pMap = getSubMap( fd );
            }
            else
                pMap = getVHostMap();

            pConn->setVHostMap( pMap );
            pConn->setLogger( getLogger());
            pConn->setRemotePort( ntohs( ((sockaddr_in *)(pCur->achPeerAddr))->sin_port) );

        //    if ( getDedicated() )
        //    {
        //        //pConn->accessGranted();
        //    }
            if ( !pConn->setLink( fd, pCur->pInfo, pMap->getSSLContext() ) )
            {
                fcntl( fd, F_SETFD, FD_CLOEXEC );
                fcntl( fd, F_SETFL, flag );
                ++pConnCur;
                //pConn->tryRead();
            }
            else
            {
                close( fd );
                --(*iCount);
            }
        }
        ++pCur;
    }
    if ( pConnCur < pConnEnd )
    {
        HttpConnPool::recycle( pConnCur, pConnEnd - pConnCur);
    }

    return 0;
}