NetInnerComponentSystem.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System.Net;
  2. using System.Net.Sockets;
  3. namespace ET.Server
  4. {
  5. [FriendOf(typeof(NetInnerComponent))]
  6. public static class NetInnerComponentSystem
  7. {
  8. [ObjectSystem]
  9. public class NetInnerComponentAwakeSystem: AwakeSystem<NetInnerComponent, NetworkProtocol>
  10. {
  11. protected override void Awake(NetInnerComponent self, NetworkProtocol protocol)
  12. {
  13. self.InnerProtocol = protocol;
  14. NetInnerComponent.Instance = self;
  15. switch (self.InnerProtocol)
  16. {
  17. case NetworkProtocol.TCP:
  18. {
  19. self.ServiceId = NetServices.Instance.AddService(new TService(AddressFamily.InterNetwork, ServiceType.Inner));
  20. break;
  21. }
  22. case NetworkProtocol.KCP:
  23. {
  24. self.ServiceId = NetServices.Instance.AddService(new KService(AddressFamily.InterNetwork, ServiceType.Inner));
  25. break;
  26. }
  27. }
  28. NetServices.Instance.RegisterReadCallback(self.ServiceId, self.OnRead);
  29. NetServices.Instance.RegisterErrorCallback(self.ServiceId, self.OnError);
  30. }
  31. }
  32. [ObjectSystem]
  33. public class NetInnerComponentAwake1System: AwakeSystem<NetInnerComponent, IPEndPoint, NetworkProtocol>
  34. {
  35. protected override void Awake(NetInnerComponent self, IPEndPoint address, NetworkProtocol protocol)
  36. {
  37. self.InnerProtocol = protocol;
  38. NetInnerComponent.Instance = self;
  39. switch (self.InnerProtocol)
  40. {
  41. case NetworkProtocol.TCP:
  42. {
  43. self.ServiceId = NetServices.Instance.AddService(new TService(address, ServiceType.Inner));
  44. break;
  45. }
  46. case NetworkProtocol.KCP:
  47. {
  48. self.ServiceId = NetServices.Instance.AddService(new KService(address, ServiceType.Inner));
  49. break;
  50. }
  51. }
  52. NetServices.Instance.RegisterAcceptCallback(self.ServiceId, self.OnAccept);
  53. NetServices.Instance.RegisterReadCallback(self.ServiceId, self.OnRead);
  54. NetServices.Instance.RegisterErrorCallback(self.ServiceId, self.OnError);
  55. }
  56. }
  57. [ObjectSystem]
  58. public class NetInnerComponentDestroySystem: DestroySystem<NetInnerComponent>
  59. {
  60. protected override void Destroy(NetInnerComponent self)
  61. {
  62. NetServices.Instance.RemoveService(self.ServiceId);
  63. }
  64. }
  65. private static void OnRead(this NetInnerComponent self, long channelId, long actorId, object message)
  66. {
  67. Session session = self.GetChild<Session>(channelId);
  68. if (session == null)
  69. {
  70. return;
  71. }
  72. session.LastRecvTime = TimeHelper.ClientFrameTime();
  73. self.HandleMessage(actorId, message);
  74. }
  75. public static void HandleMessage(this NetInnerComponent self, long actorId, object message)
  76. {
  77. EventSystem.Instance.Publish(NetInnerComponentOnRead.Clone(actorId, message));
  78. }
  79. private static void OnError(this NetInnerComponent self, long channelId, int error)
  80. {
  81. Session session = self.GetChild<Session>(channelId);
  82. if (session == null)
  83. {
  84. return;
  85. }
  86. session.Error = error;
  87. session.Dispose();
  88. }
  89. // 这个channelId是由CreateAcceptChannelId生成的
  90. private static void OnAccept(this NetInnerComponent self, long channelId, IPEndPoint ipEndPoint)
  91. {
  92. Session session = self.AddChildWithId<Session, int>(channelId, self.ServiceId);
  93. session.RemoteAddress = ipEndPoint;
  94. //session.AddComponent<SessionIdleCheckerComponent, int, int, int>(NetThreadComponent.checkInteral, NetThreadComponent.recvMaxIdleTime, NetThreadComponent.sendMaxIdleTime);
  95. }
  96. private static Session CreateInner(this NetInnerComponent self, long channelId, IPEndPoint ipEndPoint)
  97. {
  98. Session session = self.AddChildWithId<Session, int>(channelId, self.ServiceId);
  99. session.RemoteAddress = ipEndPoint;
  100. NetServices.Instance.CreateChannel(self.ServiceId, channelId, ipEndPoint);
  101. //session.AddComponent<InnerPingComponent>();
  102. //session.AddComponent<SessionIdleCheckerComponent, int, int, int>(NetThreadComponent.checkInteral, NetThreadComponent.recvMaxIdleTime, NetThreadComponent.sendMaxIdleTime);
  103. return session;
  104. }
  105. // 内网actor session,channelId是进程号
  106. public static Session Get(this NetInnerComponent self, long channelId)
  107. {
  108. Session session = self.GetChild<Session>(channelId);
  109. if (session != null)
  110. {
  111. return session;
  112. }
  113. IPEndPoint ipEndPoint = StartProcessConfigCategory.Instance.Get((int) channelId).InnerIPPort;
  114. session = self.CreateInner(channelId, ipEndPoint);
  115. return session;
  116. }
  117. public static NetworkProtocol GetProtocol(this NetInnerComponent self)
  118. {
  119. return self.InnerProtocol;
  120. }
  121. }
  122. }