|
|
@@ -24,19 +24,10 @@ namespace Model
|
|
|
/// <summary>
|
|
|
/// 消息分发组件
|
|
|
/// </summary>
|
|
|
- public class MessageDispatherComponent: Component, IMessageDispather
|
|
|
+ public class MessageDispatherComponent: Component
|
|
|
{
|
|
|
- private class MessageInfo
|
|
|
- {
|
|
|
- public byte[] MessageBytes;
|
|
|
- public int Offset;
|
|
|
- public int Count;
|
|
|
- public uint RpcId;
|
|
|
- }
|
|
|
-
|
|
|
private AppType AppType;
|
|
|
- private Dictionary<ushort, List<Action<Session, MessageInfo>>> handlers;
|
|
|
- private Dictionary<ushort, Action<Session, MessageInfo>> rpcHandlers;
|
|
|
+ private Dictionary<ushort, List<IMHandler>> handlers;
|
|
|
private Dictionary<Type, MessageAttribute> messageOpcode { get; set; } = new Dictionary<Type, MessageAttribute>();
|
|
|
|
|
|
public void Awake(AppType appType)
|
|
|
@@ -47,8 +38,7 @@ namespace Model
|
|
|
|
|
|
public void Load()
|
|
|
{
|
|
|
- this.handlers = new Dictionary<ushort, List<Action<Session, MessageInfo>>>();
|
|
|
- this.rpcHandlers = new Dictionary<ushort, Action<Session, MessageInfo>>();
|
|
|
+ this.handlers = new Dictionary<ushort, List<IMHandler>>();
|
|
|
this.messageOpcode = new Dictionary<Type, MessageAttribute>();
|
|
|
|
|
|
Assembly[] assemblies = Object.ObjectManager.GetAssemblies();
|
|
|
@@ -88,12 +78,21 @@ namespace Model
|
|
|
|
|
|
object obj = Activator.CreateInstance(type);
|
|
|
|
|
|
- IMRegister iMRegister = obj as IMRegister;
|
|
|
- if (iMRegister == null)
|
|
|
+ IMHandler imHandler = obj as IMHandler;
|
|
|
+ if (imHandler == null)
|
|
|
{
|
|
|
throw new Exception($"message handler not inherit AMEvent or AMRpcEvent abstract class: {obj.GetType().FullName}");
|
|
|
}
|
|
|
- iMRegister.Register(this);
|
|
|
+
|
|
|
+ Type messageType = imHandler.GetMessageType();
|
|
|
+ ushort opcode = this.GetOpcode(messageType);
|
|
|
+ List<IMHandler> list;
|
|
|
+ if (!this.handlers.TryGetValue(opcode, out list))
|
|
|
+ {
|
|
|
+ list = new List<IMHandler>();
|
|
|
+ this.handlers.Add(opcode, list);
|
|
|
+ }
|
|
|
+ list.Add(imHandler);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -108,76 +107,27 @@ namespace Model
|
|
|
return messageAttribute.Opcode;
|
|
|
}
|
|
|
|
|
|
- public void RegisterHandler<Message>(ushort opcode, Action<Session, Message> action) where Message: AMessage
|
|
|
- {
|
|
|
- if (!this.handlers.ContainsKey(opcode))
|
|
|
- {
|
|
|
- this.handlers.Add(opcode, new List<Action<Session, MessageInfo>>());
|
|
|
- }
|
|
|
- List<Action<Session, MessageInfo>> actions = this.handlers[opcode];
|
|
|
-
|
|
|
- actions.Add((session, messageInfo) =>
|
|
|
- {
|
|
|
- Message message;
|
|
|
- try
|
|
|
- {
|
|
|
- message = MongoHelper.FromBson<Message>(messageInfo.MessageBytes, messageInfo.Offset, messageInfo.Count);
|
|
|
- Log.Info(MongoHelper.ToJson(message));
|
|
|
- }
|
|
|
- catch (Exception ex)
|
|
|
- {
|
|
|
- throw new Exception("解释消息失败:" + opcode, ex);
|
|
|
- }
|
|
|
-
|
|
|
- action(session, message);
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- public void RegisterRpcHandler<Request, Response>(ushort opcode, Action<Session, Request, Action<Response>> action)
|
|
|
- where Request: ARequest
|
|
|
- where Response: AResponse
|
|
|
- {
|
|
|
- if (this.rpcHandlers.ContainsKey(opcode))
|
|
|
- {
|
|
|
- Log.Error($"rpc消息不能注册两次! opcode: {opcode}");
|
|
|
- return;
|
|
|
- }
|
|
|
- this.rpcHandlers.Add(opcode, (session, messageInfo) =>
|
|
|
- {
|
|
|
- Request request;
|
|
|
- try
|
|
|
- {
|
|
|
- request = MongoHelper.FromBson<Request>(messageInfo.MessageBytes, messageInfo.Offset, messageInfo.Count);
|
|
|
- Log.Info(MongoHelper.ToJson(request));
|
|
|
- }
|
|
|
- catch (Exception ex)
|
|
|
- {
|
|
|
- throw new Exception("解释消息失败:" + opcode, ex);
|
|
|
- }
|
|
|
-
|
|
|
- action(session, request, response =>
|
|
|
- {
|
|
|
- session.Reply(messageInfo.RpcId, response);
|
|
|
- }
|
|
|
- );
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public void Handle(Session session, ushort opcode, byte[] messageBytes, int offset)
|
|
|
+ public void Handle(Session session, ushort opcode, byte[] messageBytes, int offset, uint rpcId)
|
|
|
{
|
|
|
- List<Action<Session, MessageInfo>> actions;
|
|
|
+ List<IMHandler> actions;
|
|
|
if (!this.handlers.TryGetValue(opcode, out actions))
|
|
|
{
|
|
|
Log.Error($"消息 {opcode} 没有处理");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- foreach (var ev in actions)
|
|
|
+ foreach (IMHandler ev in actions)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- ev(session, new MessageInfo { MessageBytes = messageBytes, Offset = offset, Count = messageBytes.Length - offset });
|
|
|
+ ev.Handle(session, opcode, new MessageInfo
|
|
|
+ {
|
|
|
+ MessageBytes = messageBytes,
|
|
|
+ Offset = offset,
|
|
|
+ Count = messageBytes.Length - offset,
|
|
|
+ RpcId = rpcId
|
|
|
+ }
|
|
|
+ );
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
@@ -185,27 +135,7 @@ namespace Model
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- public void HandleRpc(Session session, ushort opcode, byte[] messageBytes, int offset, uint rpcId)
|
|
|
- {
|
|
|
- Action<Session, MessageInfo> action;
|
|
|
- if (!this.rpcHandlers.TryGetValue(opcode, out action))
|
|
|
- {
|
|
|
- Log.Error($"Rpc消息 {opcode} 没有处理");
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- action(session, new MessageInfo { MessageBytes = messageBytes, Offset = offset, Count = messageBytes.Length - offset, RpcId = rpcId });
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- Log.Error(e.ToString());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
public override void Dispose()
|
|
|
{
|
|
|
if (this.Id == 0)
|