NetInnerComponentSystem.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System.IO;
  2. using System.Net;
  3. namespace ET.Server
  4. {
  5. [FriendOf(typeof(NetInnerComponent))]
  6. [FriendOf(typeof(NetThreadComponent))]
  7. public static class NetInnerComponentSystem
  8. {
  9. [ObjectSystem]
  10. public class NetInnerComponentAwakeSystem: AwakeSystem<NetInnerComponent, int>
  11. {
  12. protected override void Awake(NetInnerComponent self, int sessionStreamDispatcherType)
  13. {
  14. NetInnerComponent.Instance = self;
  15. self.SessionStreamDispatcherType = sessionStreamDispatcherType;
  16. self.Service = new TService(NetThreadComponent.Instance.ThreadSynchronizationContext, ServiceType.Inner);
  17. self.Service.ErrorCallback += self.OnError;
  18. self.Service.ReadCallback += self.OnRead;
  19. NetThreadComponent.Instance.Add(self.Service);
  20. }
  21. }
  22. [ObjectSystem]
  23. public class NetInnerComponentAwake1System: AwakeSystem<NetInnerComponent, IPEndPoint, int>
  24. {
  25. protected override void Awake(NetInnerComponent self, IPEndPoint address, int sessionStreamDispatcherType)
  26. {
  27. NetInnerComponent.Instance = self;
  28. self.SessionStreamDispatcherType = sessionStreamDispatcherType;
  29. self.Service = new TService(NetThreadComponent.Instance.ThreadSynchronizationContext, address, ServiceType.Inner);
  30. self.Service.ErrorCallback += self.OnError;
  31. self.Service.ReadCallback += self.OnRead;
  32. self.Service.AcceptCallback += self.OnAccept;
  33. NetThreadComponent.Instance.Add(self.Service);
  34. }
  35. }
  36. [ObjectSystem]
  37. public class NetInnerComponentDestroySystem: DestroySystem<NetInnerComponent>
  38. {
  39. protected override void Destroy(NetInnerComponent self)
  40. {
  41. NetThreadComponent.Instance.Remove(self.Service);
  42. self.Service.Destroy();
  43. }
  44. }
  45. public static void OnRead(this NetInnerComponent self, long channelId, MemoryStream memoryStream)
  46. {
  47. Session session = self.GetChild<Session>(channelId);
  48. if (session == null)
  49. {
  50. return;
  51. }
  52. session.LastRecvTime = TimeHelper.ClientNow();
  53. Game.EventSystem.Callback(self.SessionStreamDispatcherType, session, memoryStream);
  54. }
  55. public static void OnError(this NetInnerComponent self, long channelId, int error)
  56. {
  57. Session session = self.GetChild<Session>(channelId);
  58. if (session == null)
  59. {
  60. return;
  61. }
  62. session.Error = error;
  63. session.Dispose();
  64. }
  65. // 这个channelId是由CreateAcceptChannelId生成的
  66. public static void OnAccept(this NetInnerComponent self, long channelId, IPEndPoint ipEndPoint)
  67. {
  68. Session session = self.AddChildWithId<Session, AService>(channelId, self.Service);
  69. session.RemoteAddress = ipEndPoint;
  70. //session.AddComponent<SessionIdleCheckerComponent, int, int, int>(NetThreadComponent.checkInteral, NetThreadComponent.recvMaxIdleTime, NetThreadComponent.sendMaxIdleTime);
  71. }
  72. // 这个channelId是由CreateConnectChannelId生成的
  73. public static Session Create(this NetInnerComponent self, IPEndPoint ipEndPoint)
  74. {
  75. uint localConn = self.Service.CreateRandomLocalConn();
  76. long channelId = self.Service.CreateConnectChannelId(localConn);
  77. Session session = self.CreateInner(channelId, ipEndPoint);
  78. return session;
  79. }
  80. private static Session CreateInner(this NetInnerComponent self, long channelId, IPEndPoint ipEndPoint)
  81. {
  82. Session session = self.AddChildWithId<Session, AService>(channelId, self.Service);
  83. session.RemoteAddress = ipEndPoint;
  84. self.Service.GetOrCreate(channelId, ipEndPoint);
  85. //session.AddComponent<InnerPingComponent>();
  86. //session.AddComponent<SessionIdleCheckerComponent, int, int, int>(NetThreadComponent.checkInteral, NetThreadComponent.recvMaxIdleTime, NetThreadComponent.sendMaxIdleTime);
  87. return session;
  88. }
  89. // 内网actor session,channelId是进程号
  90. public static Session Get(this NetInnerComponent self, long channelId)
  91. {
  92. Session session = self.GetChild<Session>(channelId);
  93. if (session == null)
  94. {
  95. IPEndPoint ipEndPoint = StartProcessConfigCategory.Instance.Get((int) channelId).InnerIPPort;
  96. session = self.CreateInner(channelId, ipEndPoint);
  97. }
  98. return session;
  99. }
  100. }
  101. }