Esempio n. 1
0
int BufferManager::read(TCPSocket &sock)
{
    int ret;
    
    //while(true) 
    {
        if(m_bInit)
        {
            m_InReq.ioBuf = NULL;
            memset(&m_InReq.m_msgHeader,0,HEADER_SIZE);
            m_iLen = HEADER_SIZE;
            m_bInit = false;
        }
        if(m_bReadHead)
        {
            if((ret = sock.readSocket(((char *)(&m_InReq.m_msgHeader)) + m_iOffset,HEADER_SIZE - m_iOffset)) < 0)
            {
                if(!((errno == EWOULDBLOCK)) || (errno == EINTR))
                {
                    handleSyscallError("BufferManager::read()");
                    return FAILED;
                }
                return SUCCESSFUL;
            }
            else if(ret == 0)
            {
                return FAILED;
            }
        
            m_iOffset += (unsigned int)ret;
            if(m_iOffset == HEADER_SIZE)
            {
                m_iOffset = 0;
                m_iLen = m_InReq.m_msgHeader.length;
                if(m_iLen > 0) 
                {
                    m_InReq.ioBuf = new char[m_iLen + 1];
                    memset(m_InReq.ioBuf,0,m_iLen + 1);
                    m_bReadHead = false;
                } else if(m_iLen == 0)
                {
                    m_InReq.ioBuf = NULL;
                    if(m_pAgent)
                    {
                        m_pAgent->readBack(m_InReq);
                        this->m_bInit = true;
                    }
                    this->m_bReadHead = true;
                    return SUCCESSFUL;
                } else {
                    m_bReadHead = true;
                    handleError("the data length is illegal.");
                    return FAILED;
                }
            
            }
        
        } else {
            if((ret = sock.readSocket(m_InReq.ioBuf + m_iOffset,m_iLen - m_iOffset)) < 0)
            {
                if(!((errno == EWOULDBLOCK) || (errno == EINTR))) 
                {
                    handleSyscallError("BufferManger::readSocket");
                    return FAILED;
                }
                
            } else if(ret == 0)
            {
                handleSyscallError("the peer close the connection");
                return FAILED;
            } 
            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;
                if(m_pAgent)
                {
                    #ifdef DEBUG
                    std::cout << "pAgent:"<<typeid(m_pAgent).name()\
                              <<",addr:"<<m_pAgent<<std::endl;
                    #endif
                    m_pAgent->readBack(m_InReq);
                    this->m_bInit = true;
                }
                if(m_InReq.ioBuf)
                {
                    delete []m_InReq.ioBuf;
                    m_InReq.ioBuf = NULL;
                }
                return SUCCESSFUL;
            }
        }
        
        return (int)(m_iLen - m_iOffset);
    }
}
Esempio n. 2
0
int BufferManager::read(TCPSocket &sock)
{
    int ret;

    if (m_bInit) {
        m_InReq.ioBuf = NULL;
        memset(&m_InReq.m_msgHeader, 0, HEADER_SIZE);
        m_bInit = false;
        m_iLen = HEADER_SIZE;
    }

    if (m_bReadHead) {
        if ((ret = sock.readSocket(((char *)(&(m_InReq.m_msgHeader))) + m_iOffset, \
                                   HEADER_SIZE - m_iOffset)) < 0) {
            if (!((errno == EWOULDBLOCK) || (errno == EINTR))) {
                DEV_LOG(LERROR, OUT_BOTH, "BufferManager::read()");
                return FAILED;
            }

            return SUCCESSFUL;

        } else if (ret == 0) {
            DEV_LOG(LERROR, OUT_BOTH, "the opposit close the connection");
            return FAILED;
        }

        m_iOffset += (unsigned int)ret;

        if (m_iOffset == HEADER_SIZE) {
            m_iOffset = 0;
            m_iLen = m_InReq.m_msgHeader.length;

            if (m_iLen > 0) {
                m_InReq.ioBuf = new char[m_iLen + 1];
                m_bReadHead = false;

            } else if (m_iLen == 0) {
                if (m_Agent != NULL) {
                    m_Agent->readBack(m_InReq);
                    this->m_bInit = true;
                }

                this->m_bReadHead = true;
                return SUCCESSFUL;

            } else {
                m_bReadHead = true;
                DEV_LOG(LERROR, OUT_BOTH, "the data length is not illegal");
                return FAILED;
            }
        }
    }

    if (!m_bReadHead) {
        if ((ret = sock.readSocket(m_InReq.ioBuf + m_iOffset, \
                                   m_iLen - m_iOffset)) < 0) {
            if (!((errno == EWOULDBLOCK) || (errno == EINTR))) {
                DEV_LOG(LERROR, OUT_BOTH, "readSocket in BufferMananger");
                return FAILED;
            }

            return SUCCESSFUL;

        } else if (ret == 0) {
            DEV_LOG(LERROR, OUT_BOTH, "the opposit close the connection in readData");
            return FAILED;
        }

        m_iOffset += (unsigned int)ret;

        if (m_iOffset == m_iLen) {
            m_InReq.ioBuf[m_iLen] = 0;
            m_iOffset = 0;
            m_iLen = 0;
            this->m_bReadHead = true;

            if (m_Agent != NULL) {
                m_Agent->readBack(m_InReq);
                this->m_bInit = true;
            }

            if (m_InReq.ioBuf != NULL) {
                delete [] m_InReq.ioBuf;
                m_InReq.ioBuf = NULL;
            }

            return SUCCESSFUL;
        }
    }

    return (int)(m_iLen - m_iOffset);
}