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


C++ Coroutine::canYield方法代码示例

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


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

示例1: rpc

	/** rpc **/
	Object* CoroutineService::rpc(const int64_t who, const int64_t to, const int64_t cmd, const int64_t res_proto_grp_id, Object* req_param){
		if(to == getId()){
			ERROR("service %s(%lld) can't request self", name(), (long long)m_id);
			return 0;
		}
		// prepare
		Coroutine* cr =Coroutine::Running();
		if(!cr){
			WARN("service %s(%lld) %lld fail to rpc to %lld, in main thread", name(), (long long)m_id, (long long)who, (long long)to);
			return 0;
		}
		ASSERT(cr->canYield());
		const int64_t cr_id =cr->getId();

		// set rpc info
		CoroutineRpcInfo* rpc_info =SafeNew<CoroutineRpcInfo>(res_proto_grp_id, cr_id);
		if(!set_rpc(rpc_info)){
			return 0;
		}
		const int64_t rpc_id =rpc_info->getId();

		// prepare packet
		PACKET packet;
		packet.from =getId();
		packet.to =to;
		packet.who =who;
		packet.sn =rpc_id;
		packet.command =cmd;
		packet.option =0;

		// request
		if(!DispatcherManager::RequestByObject(this, packet, req_param)){
			WARN("service %s(%lld) %lld fail to rpc to %lld, service not ready", name(), (long long)m_id, (long long)who, (long long)to);
			return 0;
		}
		rpc_info->set(rpc_id, this);
		ENSURE(cr->yield(0, rpc_id));

		// process respond
		if(Command* respond =dynamic_cast< Command* >(cr->getResumeParam())){
			// prepare
			const PACKET res_packet =respond->getPacket();
			if(res_packet.sn != static_cast<uint64_t>(rpc_id)){
				WARN("service %s(%lld) %lld fail to rpc to %lld, rpc id mismatch", name(), (long long)m_id, (long long)who, (long long)to);
				return 0;
			}
			// body is object pointer
			if(res_packet.option & OPT_BODY_IS_OBJECT_POINTER){
				return respond;
			}
			// body is protocol
			if(res_proto_grp_id>0){
				Bytes* res_bs =respond->getBody();
				const int64_t res_cmd =static_cast<int64_t>(res_packet.command);
				const int64_t group_id =res_proto_grp_id;
				const int64_t protocol_id =res_cmd;
				ProtocolBase* res_proto = ProtocolManager::CreateProtocol(group_id, protocol_id);
				if(!res_proto){
					ERROR("service %s(%lld) %lld fail to rpc to %lld, create protocol group %lld id %lld error", name(), (long long)m_id, (long long)who, (long long)to, (long long)group_id, (long long)protocol_id);
					return 0;
				}
				if(!res_proto->fromBytes(res_bs)){
					ERROR("service %s(%lld) %lld fail to rpc to %lld, unmarshal protocol name %s group %lld id %lld error", name(), (long long)m_id, (long long)who, (long long)to, res_proto->name(), (long long)group_id, (long long)protocol_id);
					return 0;
				}
				respond->setRequest(res_proto);
			}
			// body is not special
			return respond;
		}
		else{
			return cr->getResumeParam();
		}
	}
开发者ID:fast01,项目名称:winner,代码行数:75,代码来源:CoroutineService.cpp


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