LogicManager.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Common.Helper;
  5. using Common.Logger;
  6. namespace Component
  7. {
  8. public class LogicManager: ILogic
  9. {
  10. private static readonly LogicManager instance = new LogicManager();
  11. private Dictionary<int, Tuple<IHandler, Type>> handlers;
  12. private Dictionary<EventType, SortedDictionary<int, IEvent>> events;
  13. public static LogicManager Instance
  14. {
  15. get
  16. {
  17. return instance;
  18. }
  19. }
  20. private LogicManager()
  21. {
  22. this.Load();
  23. }
  24. private void Load()
  25. {
  26. string dllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logic.dll");
  27. var assembly = LoaderHelper.Load(dllPath);
  28. Type[] types = assembly.GetTypes();
  29. // 加载封包处理器
  30. var localHandlers = new Dictionary<int, Tuple<IHandler, Type>>();
  31. foreach (var type in types)
  32. {
  33. object[] attrs = type.GetCustomAttributes(typeof (HandlerAttribute), false);
  34. if (attrs.Length == 0)
  35. {
  36. continue;
  37. }
  38. var handler = (IHandler) Activator.CreateInstance(type);
  39. int opcode = ((HandlerAttribute) attrs[0]).Opcode;
  40. Type messageType = ((HandlerAttribute) attrs[0]).Type;
  41. if (opcode == 0 || messageType == null)
  42. {
  43. throw new Exception(string.Format("not set opcode or type, handler name: {0}",
  44. type.Name));
  45. }
  46. if (localHandlers.ContainsKey(opcode))
  47. {
  48. throw new Exception(string.Format(
  49. "same handler opcode, opcode: {0}, name: {1}",
  50. opcode, type.Name));
  51. }
  52. localHandlers[opcode] = new Tuple<IHandler, Type>(handler, messageType);
  53. }
  54. // 加载事件处理器
  55. var localEvents = new Dictionary<EventType, SortedDictionary<int, IEvent>>();
  56. foreach (var type in types)
  57. {
  58. object[] attrs = type.GetCustomAttributes(typeof (EventAttribute), false);
  59. if (attrs.Length == 0)
  60. {
  61. continue;
  62. }
  63. var evt = (IEvent) Activator.CreateInstance(type);
  64. EventType eventType = ((EventAttribute) attrs[0]).Type;
  65. int eventOrder = ((EventAttribute) attrs[0]).Order;
  66. if (eventOrder == 0 || eventType == EventType.DefaultEvent)
  67. {
  68. throw new Exception(string.Format("not set order or type, event name: {0}",
  69. type.Name));
  70. }
  71. if (!localEvents.ContainsKey(eventType))
  72. {
  73. localEvents[eventType] = new SortedDictionary<int, IEvent>();
  74. }
  75. if (localEvents[eventType].ContainsKey(eventOrder))
  76. {
  77. throw new Exception(
  78. string.Format("same event number, type: {0}, number: {1}, name: {2}",
  79. eventType, eventOrder, type.Name));
  80. }
  81. localEvents[eventType][eventOrder] = evt;
  82. }
  83. //
  84. this.handlers = localHandlers;
  85. this.events = localEvents;
  86. }
  87. public void Reload()
  88. {
  89. this.Load();
  90. }
  91. public void Handle(short opcode, byte[] content)
  92. {
  93. Tuple<IHandler, Type> tuple = null;
  94. if (!this.handlers.TryGetValue(opcode, out tuple))
  95. {
  96. throw new Exception(string.Format("not found handler opcode {0}", opcode));
  97. }
  98. try
  99. {
  100. object message = MongoHelper.FromBson(content, tuple.Item2);
  101. var messageEnv = new MessageEnv();
  102. messageEnv[KeyDefine.KMessage] = message;
  103. tuple.Item1.Handle(messageEnv);
  104. }
  105. catch (Exception e)
  106. {
  107. Log.Trace("message handle error: {0}", e.Message);
  108. }
  109. }
  110. public void Trigger(MessageEnv messageEnv, EventType type)
  111. {
  112. SortedDictionary<int, IEvent> iEventDict = null;
  113. if (!this.events.TryGetValue(type, out iEventDict))
  114. {
  115. return;
  116. }
  117. foreach (var iEvent in iEventDict)
  118. {
  119. iEvent.Value.Trigger(messageEnv);
  120. }
  121. }
  122. }
  123. }