예제 #1
0
	int ProtocolManager::_BytesToTable(lua_State* L){
		// check arg
		if(lua_gettop(L) < 3){
			lua_pushnil(L);
			lua_pushfstring(L, "fail to call %s, invalid arg", __func__);
			return 2;
		}
		const int64_t group_id =luaL_checkinteger(L, 1);
		const int64_t proto_id =luaL_checkinteger(L, 2);
		Bytes* bs =0;
		if(!get_object_from_lua< Bytes* >(L, 3, bs) || !bs){
			lua_pushnil(L);
			lua_pushfstring(L, "fail to call %s, invalid arg #3", __func__);
			return 2;
		}

		// create proto
		ProtocolBase* proto =Instance()->createProtocol(group_id, proto_id);
		if(!proto){
			lua_pushnil(L);
			lua_pushfstring(L, "fail to call %s, [group_id %lld, proto_id %lld] create protocol error", __func__, (long long)group_id, (long long)proto_id);
			return 2;
		}
		if(!proto->fromBytes(bs)){
			lua_pushnil(L);
			lua_pushfstring(L, "fail to call %s, [group_id %lld, proto_id %lld] unmarshal error", __func__, (long long)group_id, (long long)proto_id);
			return 2;
		}
		if(!proto->toLua(L)){
			lua_pushnil(L);
			lua_pushfstring(L, "fail to call %s, [group_id %lld, proto_id %lld] encode to lua error", __func__, (long long)group_id, (long long)proto_id);
			return 2;
		}
		return 1;
	}
예제 #2
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;
		}
	}
예제 #3
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;
		}
	}