本文整理汇总了C++中Protocol::decode_body方法的典型用法代码示例。如果您正苦于以下问题:C++ Protocol::decode_body方法的具体用法?C++ Protocol::decode_body怎么用?C++ Protocol::decode_body使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Protocol
的用法示例。
在下文中一共展示了Protocol::decode_body方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: on_readable
//要求fd是非阻塞的
HANDLE_RESULT NetInterface::on_readable(int fd)
{
SLOG_TRACE("socket on_readable. fd=%d", fd);
TransSocket* trans_socket = (TransSocket*)m_socket_manager->find_trans_socket(fd);
if(trans_socket == NULL)
{
SLOG_ERROR("can't find trans socket in socket manager. fd=%d", fd);
return HANDLE_ERROR;
}
ByteBuffer *raw_data_buffer = NULL;
unsigned int header_length = 0;
ProtocolHeader *header = m_protocol_family->create_protocol_header();
assert(header != NULL);
header_length = header->get_header_length();
assert(header_length > 0);
//1. 检查是否有未处理数据
raw_data_buffer = trans_socket->pop_recv_buffer();
if(raw_data_buffer == NULL) //新的协议包
raw_data_buffer = new ByteBuffer();
unsigned int raw_data_size = raw_data_buffer->size();
if(raw_data_size < header_length) //读协议头数据
{
int need_size = header_length-raw_data_size;
int recv_size = trans_socket->recv_buffer(raw_data_buffer, need_size, false);
if(recv_size == TRANS_ERROR)
{
SLOG_ERROR("receive protocol header error. fd=%d", fd);
delete raw_data_buffer;
m_protocol_family->destroy_protocol_header(header);
return HANDLE_ERROR;
}
else if(recv_size < need_size) //还有部分协议头数据未接收
{
SLOG_DEBUG("protocol header incomplete[need=%d,recv=%d]. waiting for the remaining data.", need_size, recv_size);
trans_socket->push_recv_buffer(raw_data_buffer);
m_protocol_family->destroy_protocol_header(header);
return HANDLE_OK;
}
raw_data_size += recv_size;
}
//2. 解码协议头
int body_length = 0;
char *header_buffer = raw_data_buffer->get_data(0, header_length);
assert(header_buffer != NULL);
if(header->decode(header_buffer, body_length) == false)
{
SLOG_ERROR("decode protocol header error. fd=%d", fd);
delete raw_data_buffer;
m_protocol_family->destroy_protocol_header(header);
return HANDLE_ERROR;
}
//3. 接收协议体数据
if(body_length > 0) //允许空协议体
{
int need_size = body_length+header_length-raw_data_size;
int recv_size = trans_socket->recv_buffer(raw_data_buffer, need_size, false);
if(recv_size == TRANS_ERROR)
{
SLOG_ERROR("receive protocol body error. fd=%d", fd);
delete raw_data_buffer;
m_protocol_family->destroy_protocol_header(header);
return HANDLE_ERROR;
}
else if(recv_size < need_size) //还有部分协议体数据未接收
{
SLOG_DEBUG("protocol body incomplete[need=%d,recv=%d]. waiting for the remaining data.", need_size, recv_size);
trans_socket->push_recv_buffer(raw_data_buffer);
m_protocol_family->destroy_protocol_header(header);
return HANDLE_OK;
}
}
//4. 解码协议体
Protocol *protocol = m_protocol_family->create_protocol_by_header(header);
if(protocol == NULL)
{
SLOG_ERROR("create protocol error. fd=%d", fd);
delete raw_data_buffer;
m_protocol_family->destroy_protocol_header(header);
return HANDLE_ERROR;
}
protocol->set_protocol_family(m_protocol_family);
/*****由protocol托管header到释放******/
protocol->attach_protocol_header(header);
char *body_buffer = NULL;
if(body_length > 0)
body_buffer = raw_data_buffer->get_data(header_length, body_length);
if(protocol->decode_body(body_buffer, body_length) == false)
{
SLOG_ERROR("decode protocol body error. fd=%d", fd);
delete raw_data_buffer;
m_protocol_family->destroy_protocol(protocol);
//.........这里部分代码省略.........