Skip to content

Commit

Permalink
调整EventDispatcher组织结构,以便热更新
Browse files Browse the repository at this point in the history
  • Loading branch information
leeveel committed Nov 18, 2022
1 parent 6ee1320 commit 2561ea3
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public enum EventID
{
#region role event
//玩家事件
SessionRemove = 1000,
RoleLevelUp = 1001, //玩家等级提升
RoleVipChange, //玩家vip改变
OnRoleOnline, //玩家上线
Expand Down
5 changes: 4 additions & 1 deletion Geek.Server.App/Logic/Server/ServerComp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ public class ServerState : CacheState
[Comp(ActorType.Server)]
public class ServerComp : StateComp<ServerState>
{

/// <summary>
/// 存放在此处的数据不会回存到数据库
/// </summary>
public HashSet<long> OnlineSet = new();
}

}
37 changes: 37 additions & 0 deletions Geek.Server.Core/Events/EventDispatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

using NLog;

namespace Geek.Server
{
public static class EventDispatcher
{
public static void Dispatch(long id, int evtId, Param args = null)
{
var actor = ActorMgr.GetActor(id);
if (actor != null)
{
var evt = new Event
{
EventId = evtId,
Data = args
};

actor.Tell(async () =>
{
// 事件需要在本actor内执行,不可多线程执行,所以不能使用Task.WhenAll来处理
var listeners = HotfixMgr.FindListeners(actor.Type, evtId);
if (listeners.IsNullOrEmpty())
{
// Log.Warn($"事件:{(EventID)evtId} 没有找到任何监听者");
return;
}
foreach (var listener in listeners)
{
var comp = await actor.GetCompAgent(listener.AgentType);
await listener.HandleEvent(comp, evt);
}
});
}
}
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

using Server.Logic.Logic;

namespace Geek.Server
{
public static class EventDispatcher
public static class EventDispatcherExtensions
{

private static readonly Logger Log = LogManager.GetCurrentClassLogger();
Expand All @@ -20,9 +21,9 @@ public static void Dispatch(this ICompAgent agent, int evtId, Param args = null)
if ((EventID)evtId > EventID.RoleSeparator && agent.OwnerType > ActorType.Separator)
{
// 全局非玩家事件,抛给所有玩家
// agent.Tell(()
// => ServerCompAgent.OnlineRoleForeach(role
// => role.Dispatch(evtId, args)));
agent.Tell(()
=> ServerCompAgent.OnlineRoleForeach(role
=> role.Dispatch(evtId, args)));
}

static void SelfHandle(ICompAgent agent, int evtId, Event evt)
Expand Down
14 changes: 14 additions & 0 deletions Geek.Server.Hotfix/Logic/Server/ServerCompAgent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

using Geek.Server.Role;
using Geek.Server.Server;

namespace Server.Logic.Logic
Expand Down Expand Up @@ -29,6 +30,19 @@ public override void Active()
Schedule<ScheduleTimer>(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(3));
}

public static async Task OnlineRoleForeach(Action<RoleCompAgent> func)
{
var serverComp = await ActorMgr.GetCompAgent<ServerCompAgent>();
serverComp.Tell(async () =>
{
foreach (var roleId in serverComp.Comp.OnlineSet)
{
var roleComp = await ActorMgr.GetCompAgent<RoleCompAgent>(roleId);
roleComp.Tell(() => func(roleComp));
}
});
}

private Task TestDelayTimer()
{
LOGGER.Debug("ServerCompAgent.TestDelayTimer.延时2秒执行.执行一次");
Expand Down

0 comments on commit 2561ea3

Please sign in to comment.