Exemple #1
0
        static async Task enterGameLoop()
        {
            await HotfixMgr.ReloadModule("");//启动游戏[hotfix工程实现一个IHotfix接口]

            Settings.Ins.StartServerTime = DateTime.Now;
            Console.ForegroundColor      = ConsoleColor.DarkYellow;
            Console.WriteLine("enter game loop 使用[ctrl+C]退出程序,不要强退,否则无法回存State");
            Console.WriteLine("压力测试请将server_config.json中IsDebug改为false");
            Console.ForegroundColor = ConsoleColor.Gray;
            LOGGER.Info("enter game loop");

            int gcTime = 0;

            Settings.Ins.AppRunning = true;
            while (Settings.Ins.AppRunning)
            {
                gcTime += 1;
                if (gcTime > 1000)//定时gc一下
                {
                    gcTime = 0;
                    GC.Collect();
                }
                await Task.Delay(TimeSpan.FromSeconds(1));
            }

            Console.WriteLine("exit game loop 开服时长:" + (DateTime.Now - Settings.Ins.StartServerTime));
            LOGGER.Info("exit game loop 开服时长:" + (DateTime.Now - Settings.Ins.StartServerTime));
            await HotfixMgr.Stop();//退出游戏

            Console.WriteLine("exit game loop succeed");
            LOGGER.Info("exit game loop succeed");
        }
Exemple #2
0
        static async Task triggerTimer(long actorId, string actorType, string handlerType, Param param)
        {
            try
            {
                var handler   = HotfixMgr.GetInstance <ITimerHandler>(handlerType);
                var agentType = handler.GetType().BaseType.GenericTypeArguments[0];
                if (agentType.GetInterface(typeof(IComponentActorAgent).FullName) != null)
                {
                    //actor
                    var agent = await ActorManager.GetOrNew(agentType, actorId);

                    var actor = (ComponentActor)agent.Owner;
                    _ = actor.SendAsync(() => handler.InternalHandleTimer(agent, param), false);
                }
                else if (agentType.GetInterface(typeof(IComponentAgent).FullName) != null)
                {
                    //component
                    var actorAgentType = HotfixMgr.GetType(actorType, agentType);
                    var compType       = agentType.BaseType.GenericTypeArguments[0];
                    var agent          = await ActorManager.GetOrNew(actorAgentType, actorId);

                    var actor = (ComponentActor)agent.Owner;
                    var comp  = await actor.GetComponent(compType);

                    _ = actor.SendAsync(() => handler.InternalHandleTimer(comp.GetAgent(agentType), param), false);
                }
            }
            catch (Exception e)
            {
                LOGGER.Error(e.ToString());
            }
        }
Exemple #3
0
        static async Task enterGameLoop()
        {
            await HotfixMgr.ReloadModule("");//启动游戏[hotfix工程实现一个IHotfix接口]

            Settings.Ins.StartServerTime = DateTime.Now;
            Console.WriteLine("enter game loop");
            LOGGER.Info("enter game loop");

            int gcTime = 0;

            Settings.Ins.AppRunning = true;
            while (Settings.Ins.AppRunning)
            {
                gcTime += 1;
                if (gcTime > 1000)//定时gc一下
                {
                    gcTime = 0;
                    GC.Collect();
                }
                await Task.Delay(TimeSpan.FromSeconds(1));
            }

            Console.WriteLine("exit game loop 开服时长:" + (DateTime.Now - Settings.Ins.StartServerTime));
            LOGGER.Info("exit game loop 开服时长:" + (DateTime.Now - Settings.Ins.StartServerTime));
            await HotfixMgr.Stop();//退出游戏

            Console.WriteLine("exit game loop succeed");
            LOGGER.Info("exit game loop succeed");
        }
        public void DispatchEvent(int evtType, Func <int, TActor, Type, Task <bool> > checkDispatchFunc, Param param = null)
        {
            lineActor.SendAsync(async() =>
            {
                if (!eventHandlers.ContainsKey(evtType))
                {
                    return;
                }

                Event evt   = new Event();
                evt.EventId = evtType;
                evt.Data    = param;
                var list    = eventHandlers[evtType];
                foreach (var evtInfo in list)
                {
                    var actor = await ActorManager.Get <TActor>(evtInfo.ActorId);
                    if (actor == null)
                    {
                        continue;
                    }

                    var info = evtInfo; //需要存个临时变量
                    _        = actor.SendAsync(async() =>
                    {
                        try
                        {
                            var handler   = HotfixMgr.GetInstance <IEventListener>(info.AgentHandler);
                            var agentType = handler.GetType().BaseType.GenericTypeArguments[0];
                            if (agentType.GetInterface(typeof(IComponentActorAgent).FullName) != null)
                            {
                                //actor
                                if (checkDispatchFunc != null && !(await checkDispatchFunc(evtType, actor, null)))
                                {
                                    return;
                                }
                                await handler.InternalHandleEvent(actor.GetAgent(agentType), evt);
                            }
                            else if (agentType.GetInterface(typeof(IComponentAgent).FullName) != null)
                            {
                                //component
                                var compType = agentType.BaseType.GenericTypeArguments[0];
                                if (checkDispatchFunc != null && !(await checkDispatchFunc(evtType, actor, compType)))
                                {
                                    return;
                                }
                                var comp = await actor.GetComponent(compType);
                                await handler.InternalHandleEvent(actor.GetAgent(agentType), evt);
                            }
                        }
                        catch (Exception e)
                        {
                            LOGGER.Error(e.ToString());
                        }
                    }, false);
                }
            }, false);
        }
        public virtual async Task Deactive()
        {
            var agent = GetAgent();

            if (agent != null)
            {
                await agent.Deactive();
            }
            HotfixMgr.RemoveAgentCache(this);
        }
Exemple #6
0
        /// <summary>actor线程执行</summary>
        public async Task InitListener()
        {
            var list = HotfixMgr.GetEventListeners(owner.GetAgent().GetType());

            if (list == null)
            {
                return;
            }
            foreach (var listener in list)
            {
                await listener.InnerInitListener(owner.GetAgent());
            }
        }
        internal IComponentAgent GetAgent(Type agentAssemblyType = null)
        {
            if (cacheAgent != null && !HotfixMgr.DoingHotfix)
            {
                return(cacheAgent);
            }
            var agent = HotfixMgr.GetAgent <IComponentAgent>(this, agentAssemblyType);

            if (!HotfixMgr.DoingHotfix)
            {
                cacheAgent = agent;
            }
            return(agent);
        }
Exemple #8
0
        /// <summary>定时任务/每每天</summary>
        public static long AddDailySchedule <TH>(this IAgent agent, int hour, int minute, Param param = null, long unscheduleId = 0) where TH : ITimerHandler
        {
            if (unscheduleId > 0)
            {
                agent.Unschedule(unscheduleId);
            }
            if (HotfixMgr.IsFromHotfix(param))
            {
                LOGGER.Fatal($"不能添加hotfix工程的类型作为Schedule参数 4 {typeof(TH)} {param.GetType()}");
                return(-1);
            }
            getAgentInfo(agent, out var actorId, out var actorAgentType);
            long id = QuartzTimer.AddDailySchedule(hour, minute, actorId, actorAgentType, typeof(TH).FullName, param);

            return(id);
        }
Exemple #9
0
        /// <summary>
        /// 分发事件
        /// </summary>
        public void DispatchEvent(int evtType, Param param = null)
        {
            if (HotfixMgr.IsFromHotfix(param))
            {
                LOGGER.Fatal($"不能添加hotfix工程的类型作为参数 DispatchEvent {param.GetType()}");
                return;
            }

            Event evt = new Event();

            evt.EventId = evtType;
            evt.Data    = param;

            if (eventHandlers.ContainsKey(evtType))
            {
                var list = eventHandlers[evtType];
                foreach (var handlerType in list)
                {
                    try
                    {
                        var handler   = HotfixMgr.GetInstance <IEventListener>(handlerType);
                        var agentType = handler.GetType().BaseType.GenericTypeArguments[1];
                        if (agentType.GetInterface(typeof(IComponentActorAgent).FullName) != null)
                        {
                            //actor
                            ownerActor.SendAsync(() => handler.InternalHandleEvent(ownerActor.GetAgent(agentType), evt));
                        }
                        else if (agentType.GetInterface(typeof(IComponentAgent).FullName) != null)
                        {
                            //component
                            var compType = agentType.BaseType.GenericTypeArguments[0];
                            ownerActor.SendAsync(async() => {
                                var comp = await ownerActor.GetComponent(compType);
                                await handler.InternalHandleEvent(comp.GetAgent(agentType), evt);
                            }, false);
                        }
                    }
                    catch (Exception e)
                    {
                        LOGGER.Error(e.ToString());
                    }
                }
            }
        }
Exemple #10
0
        /// <summary>定时任务/一次性</summary>
        public static long AddOnceSchedule <TH>(this IAgent agent, DateTime dateTime, Param param = null, long unscheduleId = 0) where TH : ITimerHandler
        {
            if (unscheduleId > 0)
            {
                agent.Unschedule(unscheduleId);
            }
            if (HotfixMgr.IsFromHotfix(param))
            {
                LOGGER.Fatal($"不能添加hotfix工程的类型作为Schedule参数 3  {typeof(TH)} {param.GetType()}");
                return(-1);
            }
            if (Settings.Ins.IsDebug && !isListenerLegal <TH>(agent))
            {
                return(-1);
            }

            getAgentInfo(agent, out var actorId, out var actorAgentType);
            long id = QuartzTimer.AddOnceSchedule(dateTime, actorId, actorAgentType, typeof(TH).FullName, param);

            return(id);
        }
Exemple #11
0
        public override async Task Deactive()
        {
            await driver.DeactiveAllComps();

            HotfixMgr.RemoveAgentCache(this);
        }
        public async Task <bool> OnLoadSucceed(bool isReload)
        {
            try
            {
                HttpHandlerFactory.SetExtraHandlerGetter(HotfixMgr.GetHttpHandler);
                TcpHandlerFactory.SetExtraHandlerGetter(Geek.Server.Message.MsgFactory.Create, msgId => HotfixMgr.GetHandler <BaseTcpHandler>(msgId));

                if (isReload)
                {
                    //热更
                    LOGGER.Info("hotfix load success");
                    await ActorManager.ActorsForeach((actor) =>
                    {
                        actor.SendAsync(actor.ClearCacheAgent, false);
                        return(Task.CompletedTask);
                    });
                }
                else
                {
                    //起服
                    if (!await Start())
                    {
                        return(false);
                    }
                }
                return(true);
            }
            catch (Exception e)
            {
                LOGGER.Fatal("OnLoadSucceed执行异常");
                LOGGER.Fatal(e.ToString());
                return(false);
            }
        }