Esempio n. 1
0
int BufferManager::read(UDPSocket &sock)
{
    int ret ;
    
    m_InReq.ioBuf = NULL;
    memset(&m_InReq.m_msgHeader,0,HEADER_SIZE);
    m_iLen = HEADER_SIZE;
    
    if((ret = sock.readSocket(((char *)(&m_InReq.m_msgHeader)),HEADER_SIZE,m_InReq.oppoAddr)) < 0)
    {
        handleSyscallError("BufferManager::read UDPSocket");
        return FAILED;
    }
    
    else if((unsigned int) ret < HEADER_SIZE)
    {
        handleSyscallError("BufferManager::read(UDPSocket)");
        return FAILED;
    }

    m_iLen = m_InReq.m_msgHeader.length;
    if(m_iLen == 0)
    {
        m_InReq.ioBuf = NULL;
        if(m_pAgent)
            m_pAgent->readBack(m_InReq);

        return SUCCESSFUL;
    }
    
    m_InReq.ioBuf = new char[m_iLen + 1];
    memset(m_InReq.ioBuf,0,m_iLen + 1);
    if((ret =  sock.readSocket(m_InReq.ioBuf,m_iLen,m_InReq.oppoAddr)) < 0)
    {
        handleSyscallError("BufferManager::read UDPSocket data");
        return FAILED;
    }

    if((unsigned int)ret < m_iLen)
    {
        handleError("BufferManager::read UDPSocket data incomplete");
    }
    
    if(m_pAgent)
        m_pAgent->readBack(m_InReq);
    else
        handleError("BufferManager don't be relate with a Agent");

    delete [] (char *)m_InReq.ioBuf;
    m_bInit = true;
    
    return SUCCESSFUL;
}
Esempio n. 2
0
int BufferManager::read(UDPSocket &sock)
{
    int ret;
    m_InReq.ioBuf = NULL;
    memset(&m_InReq.m_msgHeader, 0, HEADER_SIZE);
    m_iLen = 1024;
    char *buf = new char[m_iLen];

    if ((ret = sock.readSocket(buf, m_iLen, m_InReq.oppoAddr)) < 0) {
        return FAILED;

    } else if ((unsigned int)ret < HEADER_SIZE) {
        DEV_LOG_ERROR("BufferManager::read(UDPSocket &sock) Error, readNum < HEADER_SIZE");
        return FAILED;
    }

    memcpy(&m_InReq.m_msgHeader, buf, HEADER_SIZE);
    m_InReq.ioBuf = new char[ret - HEADER_SIZE + 1];
    memset(m_InReq.ioBuf, 0, ret - HEADER_SIZE + 1);

    if (m_InReq.m_msgHeader.length != (ret - HEADER_SIZE)) {
        DEV_LOG_ERROR("BufferManager::read(UDPSocket &sock) Error, date length error");
    }

    memcpy(m_InReq.ioBuf, buf + HEADER_SIZE, ret - HEADER_SIZE);
    m_Agent->readBack(m_InReq);
    m_bInit = true;
    return SUCCESSFUL;
    /*
        int ret;
        if(m_bInit)
        {
            m_InReq.ioBuf=NULL;
            memset(&m_InReq.m_msgHeader,0,HEADER_SIZE);
            m_bInit = false;
            m_iLen = 1000000;
    //        m_iLen = HEADER_SIZE;
        }
        char* buf = new char[m_iLen];
        if( (ret = sock.readSocket(buf, m_iLen, m_InReq.oppoAddr)) < 0)
        {
            return FAILED;
        }
        cout << "ret = " << ret << endl;
        memcpy(&m_InReq.m_msgHeader, buf, HEADER_SIZE);
        cout << m_InReq.m_msgHeader.length << endl;
        m_InReq.ioBuf = new char[ret - HEADER_SIZE + 1];
        memset(m_InReq.ioBuf, 0, ret - HEADER_SIZE + 1);
        memcpy(m_InReq.ioBuf, buf + HEADER_SIZE, ret - HEADER_SIZE);
        m_Agent->readBack(m_InReq);
        m_bInit = true;
        return SUCCESSFUL;

        if(m_bReadHead)
        {
            if((ret = sock.readSocket(((char*)(&m_InReq.m_msgHeader))+m_iOffset,
                                      m_iLen-m_iOffset,m_InReq.oppoAddr))<0)
            {
                if( !((EINTR == errno) || (EWOULDBLOCK == errno)))
                {
                    DEV_LOG(LERROR, OUT_BOTH, "BufferManager::readUDPSocket ");
                    return FAILED;
                }
                else
                {
                    return SUCCESSFUL;
                }
            }
            m_iOffset+=(unsigned int)ret;
            if(HEADER_SIZE == m_iOffset)
            {
                m_bReadHead = false;
                m_iOffset = 0;
                m_iLen=m_InReq.m_msgHeader.length;
                if( m_iLen > 0)
                {
                    m_InReq.ioBuf = new char[m_iLen+1];
                }
                else if(m_iLen == 0)
                {
                    if(m_Agent != NULL)
                    {
                        m_Agent->readBack(m_InReq);
                    }
                    m_bInit = true;
                    m_bReadHead = true;
                    return SUCCESSFUL;
                }
                else
                {
                    DEV_LOG(LERROR, OUT_BOTH, "the data length invalid in BufferManager:readUDP");
                    return FAILED;
                }
            }
        }
        if(!m_bReadHead)
        {
            if((ret =sock.readSocket(m_InReq.ioBuf+m_iOffset,m_iLen, m_InReq.oppoAddr))<0)
            {
                if( !((EINTR == errno) || (EWOULDBLOCK == errno)))
                {
                    DEV_LOG(LERROR, OUT_BOTH, "BufferManager::readUDPSocket ");
                    return FAILED;
                }
                return SUCCESSFUL;
            }
            m_iOffset+=(unsigned int)ret;
            if(m_iOffset == m_iLen)
            {
                m_InReq.ioBuf[m_iLen]=0;
                m_iOffset =0;
                m_iLen =0;
                m_bReadHead=true;
                m_bInit = true;
                if(m_Agent != NULL)
                {
                    m_Agent->readBack(m_InReq);
                }
                return SUCCESSFUL;
            }
        }
        return (int)(m_iLen - m_iOffset);
    */
}