MessageComponent.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Threading.Tasks;
  5. using Common.Base;
  6. namespace Model
  7. {
  8. public class MessageComponent: Component<World>, IAssemblyLoader
  9. {
  10. private Dictionary<ushort, List<IEventSync>> eventSyncs;
  11. private Dictionary<ushort, List<IEventAsync>> eventAsyncs;
  12. private Dictionary<ushort, Type> typeClassType;
  13. public void Load(Assembly assembly)
  14. {
  15. this.eventSyncs = new Dictionary<ushort, List<IEventSync>>();
  16. this.eventAsyncs = new Dictionary<ushort, List<IEventAsync>>();
  17. this.typeClassType = new Dictionary<ushort, Type>();
  18. ServerType serverType = World.Instance.Options.ServerType;
  19. Type[] types = assembly.GetTypes();
  20. foreach (Type t in types)
  21. {
  22. object[] attrs = t.GetCustomAttributes(typeof (MessageAttribute), false);
  23. if (attrs.Length == 0)
  24. {
  25. continue;
  26. }
  27. MessageAttribute messageAttribute = (MessageAttribute)attrs[0];
  28. if (!messageAttribute.Contains(serverType))
  29. {
  30. continue;
  31. }
  32. this.typeClassType[messageAttribute.Opcode] = messageAttribute.ClassType;
  33. object obj = Activator.CreateInstance(t);
  34. IEventSync iEventSync = obj as IEventSync;
  35. if (iEventSync != null)
  36. {
  37. if (!this.eventSyncs.ContainsKey(messageAttribute.Opcode))
  38. {
  39. this.eventSyncs.Add(messageAttribute.Opcode, new List<IEventSync>());
  40. }
  41. this.eventSyncs[messageAttribute.Opcode].Add(iEventSync);
  42. continue;
  43. }
  44. IEventAsync iEventAsync = obj as IEventAsync;
  45. if (iEventAsync != null)
  46. {
  47. if (!this.eventAsyncs.ContainsKey(messageAttribute.Opcode))
  48. {
  49. this.eventAsyncs.Add(messageAttribute.Opcode, new List<IEventAsync>());
  50. }
  51. this.eventAsyncs[messageAttribute.Opcode].Add(iEventAsync);
  52. continue;
  53. }
  54. throw new Exception(string.Format("message handler not inherit IEventSync or IEventAsync interface: {0}",
  55. obj.GetType().FullName));
  56. }
  57. }
  58. public Type GetClassType(ushort opcode)
  59. {
  60. return this.typeClassType[opcode];
  61. }
  62. public void Run(ushort opcode, Env env)
  63. {
  64. List<IEventSync> iEventSyncs = null;
  65. if (!this.eventSyncs.TryGetValue(opcode, out iEventSyncs))
  66. {
  67. throw new Exception(string.Format("no message handler, MessageAttribute: {0} opcode: {1}",
  68. typeof(MessageAttribute).Name, opcode));
  69. }
  70. foreach (IEventSync iEventSync in iEventSyncs)
  71. {
  72. iEventSync.Run(env);
  73. }
  74. }
  75. public async Task RunAsync(ushort opcode, Env env)
  76. {
  77. List<IEventSync> iEventSyncs = null;
  78. this.eventSyncs.TryGetValue(opcode, out iEventSyncs);
  79. List<IEventAsync> iEventAsyncs = null;
  80. this.eventAsyncs.TryGetValue(opcode, out iEventAsyncs);
  81. if (iEventSyncs == null && iEventAsyncs == null)
  82. {
  83. throw new Exception(string.Format("no message handler, MessageAttribute: {0} opcode: {1}",
  84. typeof(MessageAttribute).Name, opcode));
  85. }
  86. if (iEventSyncs != null)
  87. {
  88. foreach (IEventSync iEventSync in iEventSyncs)
  89. {
  90. iEventSync.Run(env);
  91. }
  92. }
  93. if (iEventAsyncs != null)
  94. {
  95. foreach (IEventAsync iEventAsync in iEventAsyncs)
  96. {
  97. await iEventAsync.RunAsync(env);
  98. }
  99. }
  100. }
  101. }
  102. }