NetInnerComponentSystem.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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, int>
  10. {
  11. public override void Awake(NetInnerComponent self, int sessionStreamDispatcherType)
  12. {
  13. NetInnerComponent.Instance = self;
  14. self.SessionStreamDispatcherType = sessionStreamDispatcherType;
  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, int>
  23. {
  24. public override void Awake(NetInnerComponent self, IPEndPoint address, int sessionStreamDispatcherType)
  25. {
  26. NetInnerComponent.Instance = self;
  27. self.SessionStreamDispatcherType = sessionStreamDispatcherType;
  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 NetInnerComponentDestroySystem: DestroySystem<NetInnerComponent>
  37. {
  38. public override void Destroy(NetInnerComponent self)
  39. {
  40. NetThreadComponent.Instance.Remove(self.Service);
  41. self.Service.Destroy();
  42. }
  43. }
  44. public static class NetInnerComponentSystem
  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. SessionStreamDispatcher.Instance.Dispatch(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. }