NetInnerComponentSystem.cs 4.8 KB

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