SessionStreamDispatcherSystem.cs 2.9 KB

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