NetInnerComponentSystem.cs 4.7 KB

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