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