SessionStreamDispatcherSystem.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. namespace ET
  5. {
  6. [FriendClass(typeof(SessionStreamDispatcher))]
  7. public static class SessionStreamDispatcherSystem
  8. {
  9. [ObjectSystem]
  10. public class SessionStreamDispatcherAwakeSystem: AwakeSystem<SessionStreamDispatcher>
  11. {
  12. public override void Awake(SessionStreamDispatcher self)
  13. {
  14. SessionStreamDispatcher.Instance = self;
  15. self.Load();
  16. }
  17. }
  18. [ObjectSystem]
  19. public class SessionStreamDispatcherLoadSystem: LoadSystem<SessionStreamDispatcher>
  20. {
  21. public override void Load(SessionStreamDispatcher self)
  22. {
  23. self.Load();
  24. }
  25. }
  26. [ObjectSystem]
  27. public class SessionStreamDispatcherDestroySystem: DestroySystem<SessionStreamDispatcher>
  28. {
  29. public override void Destroy(SessionStreamDispatcher self)
  30. {
  31. SessionStreamDispatcher.Instance = null;
  32. }
  33. }
  34. public static void Load(this SessionStreamDispatcher self)
  35. {
  36. self.Dispatchers = new ISessionStreamDispatcher[100];
  37. HashSet<Type> types = Game.EventSystem.GetTypes(typeof (SessionStreamDispatcherAttribute));
  38. foreach (Type type in types)
  39. {
  40. object[] attrs = type.GetCustomAttributes(typeof (SessionStreamDispatcherAttribute), false);
  41. if (attrs.Length == 0)
  42. {
  43. continue;
  44. }
  45. SessionStreamDispatcherAttribute sessionStreamDispatcherAttribute = attrs[0] as SessionStreamDispatcherAttribute;
  46. if (sessionStreamDispatcherAttribute == null)
  47. {
  48. continue;
  49. }
  50. if (sessionStreamDispatcherAttribute.Type >= 100)
  51. {
  52. Log.Error("session dispatcher type must < 100");
  53. continue;
  54. }
  55. ISessionStreamDispatcher iSessionStreamDispatcher = Activator.CreateInstance(type) as ISessionStreamDispatcher;
  56. if (iSessionStreamDispatcher == null)
  57. {
  58. Log.Error($"sessionDispatcher {type.Name} 需要继承 ISessionDispatcher");
  59. continue;
  60. }
  61. self.Dispatchers[sessionStreamDispatcherAttribute.Type] = iSessionStreamDispatcher;
  62. }
  63. }
  64. public static void Dispatch(this SessionStreamDispatcher self, int type, Session session, MemoryStream memoryStream)
  65. {
  66. ISessionStreamDispatcher sessionStreamDispatcher = self.Dispatchers[type];
  67. if (sessionStreamDispatcher == null)
  68. {
  69. throw new Exception("maybe your NetInnerComponent or NetOuterComponent not set SessionStreamDispatcherType");
  70. }
  71. sessionStreamDispatcher.Dispatch(session, memoryStream);
  72. }
  73. }
  74. }