Пример #1
0
int BufferManager::write(UDPSocket& sock)
{
    std::list<iov_req>::iterator aIt,pIt;
    int ret = -1;
    
    aIt = m_lIovList.begin();
    int download = 0 ;
    
    for(;aIt != m_lIovList.end();)
    {
        pIt = aIt;
        ret = sock.writeSocket((char *)(pIt->m_Iov.iov_base),pIt->m_Iov.iov_len,pIt->m_UDPAddr);
        if(ret < 0) 
        {
            if( EINTR == errno)
                continue;
            if(errno == EWOULDBLOCK)
                return SUCCESSFUL;
            else 
            {
                handleError("BufferManager::write(UDPScoket & sock)");
                if(pIt->m_pTask)
                    pIt->m_pTask->writeBack(false);
                else 
                {
                    if(m_pAgent) 
                        m_pAgent->writeBack(false);
                    else
                        handleError("BufferManager don't be relate with a Agent");
                }
                
                return FAILED;
            }
        }
        
        download += ret;
        if((size_t)ret < pIt->m_Iov.iov_len)
        {
            pIt->m_Iov.iov_base = (void *)(((char *)(pIt->m_Iov.iov_base)) + ret);
            pIt->m_Iov.iov_len -= (size_t)ret;
            pIt->m_bComplete = false;
            m_pLastIov = pIt->m_Iov.iov_base;
            break;
        }
        else 
        {
            if(pIt->m_bComplete)
            {
                if(pIt->m_Iov.iov_base)
                {
                    delete [](char *)(pIt->m_Iov.iov_base);
                    pIt->m_Iov.iov_base = NULL;
                }
            } else {
                if(m_pLastIov)
                {
                    delete [](char *)m_pLastIov;
                    m_pLastIov = NULL;
                }
            }
            
            ++aIt;
            m_lIovList.erase(pIt);
            if(pIt->m_pTask)
                pIt->m_pTask->writeBack(true);
            else 
            {
                if(m_pAgent)
                    m_pAgent->writeBack(true);
                else
                {
                    handleError("BufferManager don't be relate with a agent");
                    return FAILED;
                }
            }
            
        }
    }
    
    return download;
}
Пример #2
0
int BufferManager::write(UDPSocket &sock)
{
    list<iov_req>::iterator aIt, pIt;
    aIt = mIovList.begin();
    int ret = -1;
    int download = 0;

    for (; aIt != mIovList.end();) {
        pIt = aIt;
        ret = sock.writeSocket((char *)((*pIt).mIov.iov_base),
                               (*pIt).mIov.iov_len, (*pIt).mUDPAddr);

        if (ret < 0) {
            if (errno == EINTR) {
                continue;

            } else if (errno == EWOULDBLOCK) {
                return SUCCESSFUL;

            } else {
                DEV_LOG(LERROR, OUT_BOTH, "BufferManager::write(UDPSocket& sock)");

                if ((*pIt).mTask != NULL) {
                    (*pIt).mTask->writeBack(false);

                } else {
                    if (m_Agent != NULL) {
                        m_Agent->writeBack(false);

                    } else {
                        DEV_LOG(LERROR, OUT_BOTH, "the BufferManager don't relate a Agent");
                    }
                }

                return FAILED;
            }
        }

        download += ret;

        if ((unsigned int)ret < (*pIt).mIov.iov_len) { //in linux, the type of iovec.iov_len is size_t, not int(in windows)
            (*pIt).mIov.iov_base = (void *)((char *)((*pIt).mIov.iov_base) + ret);
            (*pIt).mIov.iov_len -= (unsigned int)ret;
            mLastIov = (*pIt).mIov.iov_base;
            (*pIt).mComplete = false;
            break;

        } else {
            if ((*pIt).mComplete) {
                if ((*pIt).mIov.iov_base) {
                    delete [](char *)(*pIt).mIov.iov_base;
                    (*pIt).mIov.iov_base = NULL;
                }

            } else {
                if (mLastIov != NULL) {
                    delete [](char *)mLastIov;
                    mLastIov = NULL;
                }
            }

            ++aIt;
            mIovList.erase(pIt);

            if ((*pIt).mTask != NULL) {
                (*pIt).mTask->writeBack(true);

            } else {
                if (m_Agent != NULL) {
                    m_Agent->writeBack(true);

                } else {
                    DEV_LOG(LERROR, OUT_BOTH, "the BufferManager don't relate a Agent");
                    return FAILED;
                }
            }
        }

    }

    return download;
}