提交 67e4883c 编写于 作者: T tanghai

重构了一下Actor消息,把拦截器的概念去掉了,改成MailboxType,不同的mailbox类型有相应的处理,比如:

gate session类型的就交给MailboxGateSessionHandler处理,它会把actor消息转发给客户端,
其它的基本上默认是MessageDispatcher类型的mailbox,交给MailboxMessageDispatcherHandler处理,它会对actor消息进行分发处理
上级 278f2769
......@@ -51,7 +51,8 @@ namespace App
Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
break;
case AppType.Realm:
Game.Scene.AddComponent<ActorMessageDispatherComponent>();
Game.Scene.AddComponent<MailboxDispatcherComponent>();
Game.Scene.AddComponent<ActorMessageDispatcherComponent>();
Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
Game.Scene.AddComponent<LocationProxyComponent>();
......@@ -59,7 +60,8 @@ namespace App
break;
case AppType.Gate:
Game.Scene.AddComponent<PlayerComponent>();
Game.Scene.AddComponent<ActorMessageDispatherComponent>();
Game.Scene.AddComponent<MailboxDispatcherComponent>();
Game.Scene.AddComponent<ActorMessageDispatcherComponent>();
Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
Game.Scene.AddComponent<LocationProxyComponent>();
......@@ -77,27 +79,51 @@ namespace App
Game.Scene.AddComponent<LocationProxyComponent>();
Game.Scene.AddComponent<ActorMessageSenderComponent>();
Game.Scene.AddComponent<ActorLocationSenderComponent>();
Game.Scene.AddComponent<ActorMessageDispatherComponent>();
Game.Scene.AddComponent<MailboxDispatcherComponent>();
Game.Scene.AddComponent<ActorMessageDispatcherComponent>();
Game.Scene.AddComponent<PathfindingComponent>();
break;
case AppType.AllServer:
// 发送普通actor消息
Game.Scene.AddComponent<ActorMessageSenderComponent>();
// 发送location actor消息
Game.Scene.AddComponent<ActorLocationSenderComponent>();
Game.Scene.AddComponent<PlayerComponent>();
Game.Scene.AddComponent<UnitComponent>();
//Game.Scene.AddComponent<DBComponent>();
//Game.Scene.AddComponent<DBProxyComponent>();
//Game.Scene.AddComponent<DBCacheComponent>();
// location server需要的组件
Game.Scene.AddComponent<LocationComponent>();
Game.Scene.AddComponent<ActorMessageDispatherComponent>();
// 访问location server的组件
Game.Scene.AddComponent<LocationProxyComponent>();
// 这两个组件是处理actor消息使用的
Game.Scene.AddComponent<MailboxDispatcherComponent>();
Game.Scene.AddComponent<ActorMessageDispatcherComponent>();
// 内网消息组件
Game.Scene.AddComponent<NetInnerComponent, string>(innerConfig.Address);
// 外网消息组件
Game.Scene.AddComponent<NetOuterComponent, string>(outerConfig.Address);
Game.Scene.AddComponent<LocationProxyComponent>();
// manager server组件,用来管理其它进程使用
Game.Scene.AddComponent<AppManagerComponent>();
Game.Scene.AddComponent<RealmGateAddressComponent>();
Game.Scene.AddComponent<GateSessionKeyComponent>();
// 配置管理
Game.Scene.AddComponent<ConfigComponent>();
// recast寻路组件
Game.Scene.AddComponent<PathfindingComponent>();
Game.Scene.AddComponent<PlayerComponent>();
Game.Scene.AddComponent<UnitComponent>();
// Game.Scene.AddComponent<HttpComponent>();
break;
case AppType.Benchmark:
......
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ETModel;
namespace ETHotfix
{
[ObjectSystem]
public class ActorMessageDispatherComponentStartSystem: AwakeSystem<ActorMessageDispatherComponent>
public class ActorMessageDispatcherComponentStartSystem: AwakeSystem<ActorMessageDispatcherComponent>
{
public override void Awake(ActorMessageDispatherComponent self)
public override void Awake(ActorMessageDispatcherComponent self)
{
self.Awake();
}
}
[ObjectSystem]
public class ActorMessageDispatherComponentLoadSystem: LoadSystem<ActorMessageDispatherComponent>
public class ActorMessageDispatcherComponentLoadSystem: LoadSystem<ActorMessageDispatcherComponent>
{
public override void Load(ActorMessageDispatherComponent self)
public override void Load(ActorMessageDispatcherComponent self)
{
self.Load();
}
......@@ -26,46 +25,20 @@ namespace ETHotfix
/// <summary>
/// Actor消息分发组件
/// </summary>
public static class ActorMessageDispatherComponentHelper
public static class ActorMessageDispatcherComponentHelper
{
public static void Awake(this ActorMessageDispatherComponent self)
public static void Awake(this ActorMessageDispatcherComponent self)
{
self.Load();
}
public static void Load(this ActorMessageDispatherComponent self)
public static void Load(this ActorMessageDispatcherComponent self)
{
AppType appType = StartConfigComponent.Instance.StartConfig.AppType;
self.ActorMessageHandlers.Clear();
self.ActorTypeHandlers.Clear();
List<Type> types = Game.EventSystem.GetTypes(typeof(ActorInterceptTypeHandlerAttribute));
foreach (Type type in types)
{
object[] attrs = type.GetCustomAttributes(typeof(ActorInterceptTypeHandlerAttribute), false);
if (attrs.Length == 0)
{
continue;
}
ActorInterceptTypeHandlerAttribute actorInterceptTypeHandlerAttribute = (ActorInterceptTypeHandlerAttribute) attrs[0];
if (!actorInterceptTypeHandlerAttribute.Type.Is(appType))
{
continue;
}
object obj = Activator.CreateInstance(type);
IActorInterceptTypeHandler iActorInterceptTypeHandler = obj as IActorInterceptTypeHandler;
if (iActorInterceptTypeHandler == null)
{
throw new Exception($"actor handler not inherit IEntityActorHandler: {obj.GetType().FullName}");
}
self.ActorTypeHandlers.Add(actorInterceptTypeHandlerAttribute.ActorType, iActorInterceptTypeHandler);
}
List<Type> types = Game.EventSystem.GetTypes(typeof(ActorMessageHandlerAttribute));
types = Game.EventSystem.GetTypes(typeof (ActorMessageHandlerAttribute));
foreach (Type type in types)
......@@ -95,24 +68,18 @@ namespace ETHotfix
}
}
/// <summary>
/// 分发actor消息
/// </summary>
public static async ETTask Handle(
this ActorMessageDispatherComponent self, MailBoxComponent mailBoxComponent, ActorMessageInfo actorMessageInfo)
this ActorMessageDispatcherComponent self, Entity entity, ActorMessageInfo actorMessageInfo)
{
// 有拦截器使用拦截器处理
IActorInterceptTypeHandler iActorInterceptTypeHandler;
if (self.ActorTypeHandlers.TryGetValue(mailBoxComponent.ActorInterceptType, out iActorInterceptTypeHandler))
{
await iActorInterceptTypeHandler.Handle(actorMessageInfo.Session, mailBoxComponent.Entity, actorMessageInfo.Message);
return;
}
// 没有拦截器就用IMActorHandler处理
if (!self.ActorMessageHandlers.TryGetValue(actorMessageInfo.Message.GetType(), out IMActorHandler handler))
{
throw new Exception($"not found message handler: {MongoHelper.ToJson(actorMessageInfo.Message)}");
}
await handler.Handle(actorMessageInfo.Session, mailBoxComponent.Entity, actorMessageInfo.Message);
await handler.Handle(actorMessageInfo.Session, entity, actorMessageInfo.Message);
}
}
}
using System.Threading.Tasks;
using ETModel;
using ETModel;
namespace ETHotfix
{
......
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ETModel;
namespace ETHotfix
......@@ -10,7 +8,7 @@ namespace ETHotfix
{
public override void Awake(MailBoxComponent self)
{
self.ActorInterceptType = ActorInterceptType.None;
self.MailboxType = MailboxType.MessageDispatcher;
self.Queue.Clear();
}
}
......@@ -18,9 +16,9 @@ namespace ETHotfix
[ObjectSystem]
public class MailBoxComponentAwake1System : AwakeSystem<MailBoxComponent, string>
{
public override void Awake(MailBoxComponent self, string actorInterceptType)
public override void Awake(MailBoxComponent self, string mailboxType)
{
self.ActorInterceptType = actorInterceptType;
self.MailboxType = mailboxType;
self.Queue.Clear();
}
}
......@@ -76,7 +74,7 @@ namespace ETHotfix
public static async ETVoid HandleAsync(this MailBoxComponent self)
{
ActorMessageDispatherComponent actorMessageDispatherComponent = Game.Scene.GetComponent<ActorMessageDispatherComponent>();
MailboxDispatcherComponent mailboxDispatcherComponent = Game.Scene.GetComponent<MailboxDispatcherComponent>();
long instanceId = self.InstanceId;
......@@ -95,8 +93,8 @@ namespace ETHotfix
return;
}
// 根据这个actor的类型分发给相应的ActorHandler处理
await actorMessageDispatherComponent.Handle(self, info);
// 根据这个mailbox类型分发给相应的处理
await mailboxDispatcherComponent.Handle(self, info);
}
catch (Exception e)
{
......
using System;
using System.Collections.Generic;
using ETModel;
namespace ETHotfix
{
[ObjectSystem]
public class MailboxDispatcherComponentStartSystem: AwakeSystem<MailboxDispatcherComponent>
{
public override void Awake(MailboxDispatcherComponent self)
{
self.Awake();
}
}
[ObjectSystem]
public class MailboxDispatcherComponentLoadSystem: LoadSystem<MailboxDispatcherComponent>
{
public override void Load(MailboxDispatcherComponent self)
{
self.Load();
}
}
public static class MailboxDispatcherComponentHelper
{
public static void Awake(this MailboxDispatcherComponent self)
{
self.Load();
}
public static void Load(this MailboxDispatcherComponent self)
{
AppType appType = StartConfigComponent.Instance.StartConfig.AppType;
self.MailboxHandlers.Clear();
List<Type> types = Game.EventSystem.GetTypes(typeof(MailboxHandlerAttribute));
foreach (Type type in types)
{
object[] attrs = type.GetCustomAttributes(typeof(MailboxHandlerAttribute), false);
if (attrs.Length == 0)
{
continue;
}
MailboxHandlerAttribute mailboxHandlerAttribute = (MailboxHandlerAttribute) attrs[0];
if (!mailboxHandlerAttribute.Type.Is(appType))
{
continue;
}
object obj = Activator.CreateInstance(type);
IMailboxHandler iMailboxHandler = obj as IMailboxHandler;
if (iMailboxHandler == null)
{
throw new Exception($"actor handler not inherit IEntityActorHandler: {obj.GetType().FullName}");
}
self.MailboxHandlers.Add(mailboxHandlerAttribute.MailboxType, iMailboxHandler);
}
}
/// <summary>
/// 根据mailbox类型做不同的处理
/// </summary>
public static async ETTask Handle(
this MailboxDispatcherComponent self, MailBoxComponent mailBoxComponent, ActorMessageInfo actorMessageInfo)
{
IMailboxHandler iMailboxHandler;
if (self.MailboxHandlers.TryGetValue(mailBoxComponent.MailboxType, out iMailboxHandler))
{
await iMailboxHandler.Handle(actorMessageInfo.Session, mailBoxComponent.Entity, actorMessageInfo.Message);
}
}
}
}
using System;
using System.Threading.Tasks;
using ETModel;
namespace ETHotfix
{
/// <summary>
/// gate session 拦截器,收到的actor消息直接转发给客户端
/// gate session类型的Mailbox,收到的actor消息直接转发给客户端
/// </summary>
[ActorInterceptTypeHandler(AppType.Gate, ActorInterceptType.GateSession)]
public class GateSessionActorInterceptInterceptTypeHandler : IActorInterceptTypeHandler
[MailboxHandler(AppType.Gate, MailboxType.GateSession)]
public class MailboxGateSessionHandler : IMailboxHandler
{
public async ETTask Handle(Session session, Entity entity, object actorMessage)
{
......
using System;
using ETModel;
namespace ETHotfix
{
/// <summary>
/// 消息分发类型的Mailbox,对mailbox中的消息进行分发处理
/// </summary>
[MailboxHandler(AppType.AllServer, MailboxType.MessageDispatcher)]
public class MailboxMessageDispatcherHandler : IMailboxHandler
{
public async ETTask Handle(Session session, Entity entity, object actorMessage)
{
try
{
await Game.Scene.GetComponent<ActorMessageDispatcherComponent>().Handle(
entity, new ActorMessageInfo() { Session = session, Message = actorMessage });
}
catch (Exception e)
{
Log.Error(e);
}
}
}
}
......@@ -22,7 +22,7 @@ namespace ETHotfix
Player player = ComponentFactory.Create<Player, string>(account);
Game.Scene.GetComponent<PlayerComponent>().Add(player);
session.AddComponent<SessionPlayerComponent>().Player = player;
session.AddComponent<MailBoxComponent, string>(ActorInterceptType.GateSession);
session.AddComponent<MailBoxComponent, string>(MailboxType.GateSession);
response.PlayerId = player.Id;
reply(response);
......
using ETModel;
using PF;
namespace ETHotfix
{
......
using System;
using System.Collections.Generic;
using System.IO;
using System.IO;
using System.Reflection;
namespace ETModel
......
using System;
using System.Threading.Tasks;
namespace ETModel
{
......
using System;
using System.Threading.Tasks;
namespace ETModel
{
......
using System;
namespace ETModel
{
public class ActorInterceptTypeHandlerAttribute : BaseAttribute
{
public AppType Type { get; }
public string ActorType { get; }
public ActorInterceptTypeHandlerAttribute(AppType appType, string actorType)
{
this.Type = appType;
this.ActorType = actorType;
}
}
}
\ No newline at end of file
......@@ -6,10 +6,8 @@ namespace ETModel
/// <summary>
/// Actor消息分发组件
/// </summary>
public class ActorMessageDispatherComponent : Component
public class ActorMessageDispatcherComponent : Component
{
public readonly Dictionary<string, IActorInterceptTypeHandler> ActorTypeHandlers = new Dictionary<string, IActorInterceptTypeHandler>();
public readonly Dictionary<Type, IMActorHandler> ActorMessageHandlers = new Dictionary<Type, IMActorHandler>();
public override void Dispose()
......@@ -21,7 +19,6 @@ namespace ETModel
base.Dispose();
this.ActorMessageHandlers.Clear();
this.ActorTypeHandlers.Clear();
}
}
}
\ No newline at end of file
using System;
namespace ETModel
namespace ETModel
{
public class ActorMessageHandlerAttribute : BaseAttribute
{
......
using System;
using System.Threading.Tasks;
namespace ETModel
{
......
namespace ETModel
{
public interface IActorInterceptTypeHandler
public interface IMailboxHandler
{
ETTask Handle(Session session, Entity entity, object actorMessage);
}
......
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ETModel
{
......@@ -14,8 +13,8 @@ namespace ETModel
/// </summary>
public class MailBoxComponent: Component
{
// 拦截器类型,默认没有拦截器
public string ActorInterceptType;
// Mailbox的类型
public string MailboxType;
// 队列处理消息
public Queue<ActorMessageInfo> Queue = new Queue<ActorMessageInfo>();
......
using System.Collections.Generic;
namespace ETModel
{
/// <summary>
/// mailbox分发组件,不同类型的mailbox交给不同的MailboxHandle处理
/// </summary>
public class MailboxDispatcherComponent : Component
{
public readonly Dictionary<string, IMailboxHandler> MailboxHandlers = new Dictionary<string, IMailboxHandler>();
public override void Dispose()
{
if (this.IsDisposed)
{
return;
}
base.Dispose();
this.MailboxHandlers.Clear();
}
}
}
\ No newline at end of file
namespace ETModel
{
public class MailboxHandlerAttribute : BaseAttribute
{
public AppType Type { get; }
public string MailboxType { get; }
public MailboxHandlerAttribute(AppType appType, string mailboxType)
{
this.Type = appType;
this.MailboxType = mailboxType;
}
}
}
\ No newline at end of file
namespace ETModel
{
public static partial class ActorInterceptType
public static partial class MailboxType
{
public const string None = "None";
public const string MessageDispatcher = "Dispatcher";
public const string GateSession = "GateSession";
}
}
using System;
using System.Threading.Tasks;
namespace ETModel
{
......
using System;
using System.Threading.Tasks;
namespace ETModel
{
......
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
namespace ETModel
{
......
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using MongoDB.Bson.Serialization.Attributes;
namespace ETModel
{
......
using System.Net;
using System.Threading.Tasks;
namespace ETModel
{
......
using System;
using System.Net;
using System.Threading.Tasks;
namespace ETModel
namespace ETModel
{
public class BenchmarkComponent: Component
{
......
using System;
using System.Net;
using System.Threading.Tasks;
namespace ETModel
namespace ETModel
{
public class WebSocketBenchmarkComponent: Component
{
......
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
namespace ETModel
{
......
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
namespace ETModel
{
......
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace ETModel
{
......
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
namespace ETModel
......
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using MongoDB.Driver;
namespace ETModel
......
using System;
using System.Threading.Tasks;
using MongoDB.Driver;
namespace ETModel
......
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
......
using System;
using System.Threading.Tasks;
using MongoDB.Driver;
namespace ETModel
......
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ETModel
{
......
......@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reflection;
using System.Threading.Tasks;
namespace ETModel
{
......
using ETModel;
using System.Collections.Generic;
namespace ETModel
{
......
using System;
using System.Collections.Generic;
#if !SERVER
using System.Collections.Generic;
#endif
namespace ETHotfix
{
......
using System;
using System.Text;
using System.Text;
namespace ETModel
{
......
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace ETModel
{
......
using System;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization.Attributes;
namespace ETModel
{
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace ETModel
......
using System;
using System.Globalization;
using PF;
namespace PF
{
......
using System.Net;
using MongoDB.Bson.Serialization.Attributes;
namespace ETModel
namespace ETModel
{
public class ClientConfig: AConfigComponent
{
......
using System.Net;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization.Attributes;
namespace ETModel
{
......
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace ETModel
{
......
using System;
namespace ETModel
namespace ETModel
{
public class MessageAttribute: BaseAttribute
{
......
using System;
namespace ETModel
namespace ETModel
{
public class MessageHandlerAttribute : BaseAttribute
{
......
using System;
using System.Collections.Generic;
#if !SERVER
using System.Collections.Generic;
#endif
namespace ETModel
{
......
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace ETModel
{
......
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using Microsoft.IO;
namespace ETModel
{
......
......@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using Microsoft.IO;
namespace ETModel
......
using ETModel;
namespace ETModel
{
[Message(OuterOpcode.Actor_Test)]
......
......@@ -3,9 +3,7 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
namespace ETModel
{
......
using System;
namespace ETModel
namespace ETModel
{
public class NumericWatcherAttribute : BaseAttribute
{
......
using PF;
using System.Collections.Generic;
using System;
......
//#define ASTARDEBUG //"BBTree Debug" If enables, some queries to the tree will show debug lines. Turn off multithreading when using this since DrawLine calls cannot be called from a different thread
using System;
using PF;
namespace PF {
......
using PF;
using System.Collections.Generic;
namespace PF {
......
using PF;
namespace PF {
/** Transforms to and from world space to a 2D movement plane.
* The transformation is guaranteed to be purely a rotation
......
using PF;
namespace PF {
/** Holds a coordinate in integers */
public struct Int3 : System.IEquatable<Int3> {
......
using PF;
using System.Collections.Generic;
namespace PF {
......
//#define ASTAR_POOL_DEBUG //@SHOWINEDITOR Enables debugging of path pooling. Will log warnings and info messages about paths not beeing pooled correctly.
using PF;
using System.Collections;
using System.Collections.Generic;
......
using PF;
namespace PF
{
/** What data to draw the graph debugging with */
......
#define DECREASE_KEY
using System.Collections.Generic;
namespace PF {
/** Stores temporary node data for a single pathfinding request.
* Every node has one PathNode per thread used.
......
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using PF;
namespace PF {
#if NETFX_CORE
......
using System.Collections.Generic;
using ETModel;
#if UNITY_5_5_OR_NEWER
using UnityEngine.Profiling;
#endif
......
using PF;
namespace PF {
/** Basic path, finds the shortest path from A to B.
* \ingroup paths
......
//#define ASTARDEBUG //Draws a ray for each node visited
using PF;
using System;
using System.Collections.Generic;
......
using PF;
namespace PF {
/** Returns a path heading away from a specified point to avoid.
* The search will terminate when G \> \a length (passed to the constructor) + FleePath.spread.\n
......
using System;
using PF;
using System.Collections.Generic;
namespace PF {
......
using PF;
namespace PF {
/** Restrict suitable nodes by if they have been searched by a FloodPath.
*
......
using PF;
namespace PF {
/** Finds a path in a random direction from the start node.
* \ingroup paths
......
using PF;
namespace PF {
/** Extended Path.
* \ingroup paths
......
using System;
using System.IO;
using System.Collections.Generic;
using PF;
#if ASTAR_NO_ZIP
using Pathfinding.Serialization.Zip;
#elif NETFX_CORE
......
using System.Collections.Generic;
using System;
using PF;
using Guid = PF.Guid;
#if NETFX_CORE
using System.Linq;
......
using System.Threading;
using ETModel;
using PF;
namespace PF {
/** Queue of paths to be processed by the system */
class ThreadControlQueue
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册