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


C++ Protocol::attach_raw_data方法代码示例

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


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

示例1: on_readable


//.........这里部分代码省略.........
	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);
		return HANDLE_ERROR;
	}
	/***** 由protocol托管raw_data的释放 ******/
	protocol->attach_raw_data(raw_data_buffer);

	//6. 调用回调函数向应用层发协议
	bool detach_protocol = false;
	int result = on_recv_protocol(fd, protocol, detach_protocol);
	if(result==-1 || detach_protocol==false)
		m_protocol_family->destroy_protocol(protocol);
	return HANDLE_OK;
}
开发者ID:GameBelial,项目名称:easynetwork,代码行数:101,代码来源:NetInterface.cpp


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