Ejemplo n.º 1
0
	int64_t CallbackService::on_start_command(Command* command){
		Super::on_start_command(command);

		PACKET packet =command->getPacket();
		const int64_t who =command->getWho();
		const int64_t req_cmd =command->getRequestCommand();
		if(CallbackCommandDesc* desc =static_cast< CallbackCommandDesc* >(m_command_desc_table->get(req_cmd))){
			//// prepare
			Bytes* body =command->getBody();
			const int64_t grp_id =m_protocol_group_id;
			const int64_t res_cmd =desc->getRespondCommand();
			CallbackCommandDesc::PFN_CALLBACK callback =desc->getCallback();
			
			//// try code as protocol
			if(desc->isUseProtocol()){
				ProtocolManager* pm =ProtocolManager::Instance();
				// request
				if(!(packet.option & OPT_BODY_IS_OBJECT_POINTER)){
					ProtocolBase* req =pm->createProtocol(grp_id, req_cmd);
					if(!req){
						ERROR("service %s(%lld) who %lld fail to start command %lld, create request group %lld protocol %lld error", name(), (long long)m_id, (long long)who, (long long)req_cmd, (long long)grp_id, (long long)req_cmd);
						return Command::STATE_ERROR;
					}
					if(!req->fromBytes(body)){
						ERROR("service %s(%lld) who %lld fail to start command %lld, decode request group %lld protocol %lld error", name(), (long long)m_id, (long long)who, (long long)req_cmd, (long long)grp_id, (long long)req_cmd);
						return Command::STATE_ERROR;
					}
					command->setRequest(req);
				}

				// respond
				if(res_cmd > 0){
					ProtocolBase* res =pm->createProtocol(grp_id, res_cmd);
					if(!res){
						ERROR("service %s(%lld) who %lld fail to start command %lld, create respond group %lld protocol %lld error", name(), (long long)m_id, (long long)who, (long long)req_cmd, (long long)grp_id, (long long)res_cmd);
						return Command::STATE_ERROR;
					}
					command->setRespond(res);
					command->setRespondCommand(res->id());
				}
			}

			//// callback
			return callback(command);
		}
		else{
			ERROR("service %s(%lld) who %lld fail to start command %lld, command not found", name(), (long long)m_id, (long long)who, (long long)req_cmd);
			return Command::STATE_ERROR;
		}
	}
Ejemplo n.º 2
0
	int64_t CoroutineService::on_start_command(Command* command){
		Super::on_start_command(command);
		PACKET packet =command->getPacket();
		const int64_t grp_id =m_protocol_group_id;
		const int64_t who =command->getWho();
		const int64_t req_cmd =command->getRequestCommand();
		if(CoroutineCommandDesc* desc =static_cast< CoroutineCommandDesc* >(m_command_desc_table->get(req_cmd))){
			//// preprocess command
			const int64_t res_cmd =desc->getRespondCommand();
			Bytes* body =command->getBody();
			ProtocolManager* pm =ProtocolManager::Instance();

			//// try code as protocol
			if(desc->isUseProtocol()){
				// request
				if(!(packet.option & OPT_BODY_IS_OBJECT_POINTER)){
					ProtocolBase* req =pm->createProtocol(grp_id, req_cmd);
					if(!req){
						ERROR("service %s(%lld) who %lld fail to start command %lld, create request group %lld protocol %lld error", name(), (long long)m_id, (long long)who, (long long)req_cmd, (long long)grp_id, (long long)req_cmd);
						return Command::STATE_ERROR;
					}
					if(!req->fromBytes(body)){
						ERROR("service %s(%lld) who %lld fail to start command %lld, unmarsh request group %lld protocol %lld error", name(), (long long)m_id, (long long)who, (long long)req_cmd, (long long)grp_id, (long long)req_cmd);
						return Command::STATE_ERROR;
					}
					command->setRequest(req);
				}

				// respond
				if(res_cmd > 0){
					ProtocolBase* res =pm->createProtocol(grp_id, res_cmd);
					if(!res){
						ERROR("service %s(%lld) who %lld fail to start command %lld, create respond group %lld protocol %lld error", name(), (long long)m_id, (long long)who, (long long)req_cmd, (long long)grp_id, (long long)res_cmd);
						return Command::STATE_ERROR;
					}
					command->setRespond(res);
					command->setRespondCommand(res->id());
				}
			}

			//// go coroutine
			int64_t cr_new_id=0;
			const int64_t result =m_cr_pool->go(desc->getCallback(), command, cr_new_id);
			if(result >= 0){
				if(result == Coroutine::STATUS_IDLE){
					return Command::STATE_COMPLETE;
				}
				else if(result == Coroutine::STATUS_WAITING){
					return cr_new_id;
				}
				else{
					return Command::STATE_ERROR;
				}
			}
			else{
				ERROR("service %s(%lld) who %lld fail to start command %lld, error code %lld", name(), (long long)m_id, (long long)who, (long long)req_cmd, (long long)-result);
				return Command::STATE_ERROR;
			}
		}
		else{
			ERROR("service %s(%lld) who %lld fail to start command %lld, not config", name(), (long long)m_id, (long long)packet.who, (long long)req_cmd);
			return core::Command::STATE_ERROR;
		}
	}