当前位置: 首页>>代码示例>>C++>>正文


C++ TCPSocket::readSocket方法代码示例

本文整理汇总了C++中TCPSocket::readSocket方法的典型用法代码示例。如果您正苦于以下问题:C++ TCPSocket::readSocket方法的具体用法?C++ TCPSocket::readSocket怎么用?C++ TCPSocket::readSocket使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TCPSocket的用法示例。


在下文中一共展示了TCPSocket::readSocket方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: read

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);
    }
}
开发者ID:ytaoWang,项目名称:CSLogChecker,代码行数:99,代码来源:BufferManager.cpp

示例2: read

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);
}
开发者ID:Thirdhuku,项目名称:CloudStore,代码行数:92,代码来源:BufferManager.cpp


注:本文中的TCPSocket::readSocket方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。