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


C++ ConnectionPtr::GetReadBufferLen方法代码示例

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


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

示例1: OnReadEvent

void EventProcess::OnReadEvent(const ConnectionPtr& conn) {
    if (conn->robot_->GetRobotId() != headerRobotId_) {
        WeakEntryPtr weakEntry(boost::any_cast<WeakEntryPtr>(conn->getContext()));
        EntryPtr entry(weakEntry.lock());
        if (entry) {
            connectionBuckets_.back().insert(entry);
            DEBUG("Robot %d received a msg.", conn->robot_->GetRobotId());
        }
    }
    char msgLen[4] = {0};
    size_t len = 0;
    int iMsgLen = 0;
    int msgId = 0;
    int dataLength = conn->GetReadBufferLen();
    while (dataLength > 0) {
        memset(msgLen, '\0', 4);//读取msg length
        len = conn->GetReadBufferData(msgLen, 4);
        if (4 != len) {
            ERROR("Doesn't has 4 byte len info.");
            break;
        }
        int *pMsgLen = (int*)msgLen;
        iMsgLen = ntohl(*pMsgLen);
        if (0 == iMsgLen) {
            ERROR("Error! Convent data length failed.");
            break;
        }
        
        memset(msgLen, '\0', 4);//读取msgid
        len = conn->GetReadBufferData(msgLen, 4);
        if (4 != len) {
            ERROR("Doesn't has 4 byte MsgId info.");
            break;
        }
        pMsgLen = (int*)msgLen;
        msgId = ntohl(*pMsgLen);

        char* msg = new char[iMsgLen + 1];//读取消息体
        len = conn->GetReadBufferData(msg, iMsgLen);
        DEBUG("Receive %d byte from server for robot %d in message %d.", len, conn->robot_->GetRobotId(), msgId);
        
        dataLength = conn->GetReadBufferLen();//本次剩余未读字节
        DEBUG("Data still has length %d.", dataLength);

        string strMsg;
        strMsg.append(msg, iMsgLen);
        delete [] msg;
        MessagePackagePtr msgNode(new MsgNode);
        msgNode->SetMsg(strMsg);
        msgNode->SetMsgId(msgId);
        msgNode->SetConn(conn);
        int result = conn->robot_->RobotProcess(msgNode);
        if (robot::CLOSE_CONNECTION == result) {
            DEBUG("One robot disconnected, break this turn, still has %d byte in buffer.", dataLength);
            break;
        }
    }
}
开发者ID:ShiboBrady,项目名称:robot_ddz,代码行数:58,代码来源:EventProcess.cpp


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