MessageDispatherComponent.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Model
  4. {
  5. /// <summary>
  6. /// 消息分发组件
  7. /// </summary>
  8. [ObjectEvent(EntityEventId.MessageDispatherComponent)]
  9. public class MessageDispatherComponent: Component, IAwake<AppType>, ILoad
  10. {
  11. private AppType AppType;
  12. private Dictionary<ushort, List<IInstanceMethod>> handlers;
  13. private DoubleMap<ushort, Type> opcodeTypes = new DoubleMap<ushort, Type>();
  14. public void Awake(AppType appType)
  15. {
  16. this.AppType = appType;
  17. this.Load();
  18. }
  19. public void Load()
  20. {
  21. this.handlers = new Dictionary<ushort, List<IInstanceMethod>>();
  22. this.opcodeTypes = new DoubleMap<ushort, Type>();
  23. Type[] monoTypes = DllHelper.GetAllTypes();
  24. foreach (Type monoType in monoTypes)
  25. {
  26. object[] attrs = monoType.GetCustomAttributes(typeof(MessageAttribute), false);
  27. if (attrs.Length == 0)
  28. {
  29. continue;
  30. }
  31. MessageAttribute messageAttribute = attrs[0] as MessageAttribute;
  32. if (messageAttribute == null)
  33. {
  34. continue;
  35. }
  36. this.opcodeTypes.Add(messageAttribute.Opcode, monoType);
  37. }
  38. Type[] types = DllHelper.GetAllTypes();
  39. foreach (Type type in types)
  40. {
  41. object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
  42. if (attrs.Length == 0)
  43. {
  44. continue;
  45. }
  46. MessageHandlerAttribute messageHandlerAttribute = (MessageHandlerAttribute)attrs[0];
  47. IInstanceMethod method = new ILInstanceMethod(type, "Handle");
  48. if (!this.handlers.ContainsKey(messageHandlerAttribute.Opcode))
  49. {
  50. this.handlers.Add(messageHandlerAttribute.Opcode, new List<IInstanceMethod>());
  51. }
  52. this.handlers[messageHandlerAttribute.Opcode].Add(method);
  53. }
  54. }
  55. public ushort GetOpcode(Type type)
  56. {
  57. return this.opcodeTypes.GetKeyByValue(type);
  58. }
  59. public void Handle(Session session, MessageInfo messageInfo)
  60. {
  61. List<IInstanceMethod> actions;
  62. if (!this.handlers.TryGetValue(messageInfo.Opcode, out actions))
  63. {
  64. Log.Error($"消息 {messageInfo.Opcode} 没有处理");
  65. return;
  66. }
  67. Type messageType = this.opcodeTypes.GetValueByKey(messageInfo.Opcode);
  68. object message = JsonHelper.FromJson(messageType, messageInfo.MessageBytes, messageInfo.Offset, messageInfo.Count);
  69. messageInfo.Message = message;
  70. foreach (IInstanceMethod ev in actions)
  71. {
  72. try
  73. {
  74. ev.Run(session, messageInfo.Message);
  75. }
  76. catch (Exception e)
  77. {
  78. Log.Error(e.ToString());
  79. }
  80. }
  81. }
  82. public override void Dispose()
  83. {
  84. if (this.Id == 0)
  85. {
  86. return;
  87. }
  88. base.Dispose();
  89. }
  90. }
  91. }