NetKcpComponentSystem.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. namespace ET
  5. {
  6. [FriendClass(typeof(NetKcpComponent))]
  7. public static class NetKcpComponentSystem
  8. {
  9. [ObjectSystem]
  10. public class NetKcpComponentAwakeSystem: AwakeSystem<NetKcpComponent, int>
  11. {
  12. public override void Awake(NetKcpComponent self, int sessionStreamDispatcherType)
  13. {
  14. self.SessionStreamDispatcherType = sessionStreamDispatcherType;
  15. self.Service = new KService(NetThreadComponent.Instance.ThreadSynchronizationContext, ServiceType.Outer);
  16. self.Service.ErrorCallback += (channelId, error) => self.OnError(channelId, error);
  17. self.Service.ReadCallback += (channelId, Memory) => self.OnRead(channelId, Memory);
  18. NetThreadComponent.Instance.Add(self.Service);
  19. }
  20. }
  21. [ObjectSystem]
  22. public class NetKcpComponentAwake1System: AwakeSystem<NetKcpComponent, IPEndPoint, int>
  23. {
  24. public override void Awake(NetKcpComponent self, IPEndPoint address, int sessionStreamDispatcherType)
  25. {
  26. self.SessionStreamDispatcherType = sessionStreamDispatcherType;
  27. self.Service = new KService(NetThreadComponent.Instance.ThreadSynchronizationContext, address, ServiceType.Outer);
  28. self.Service.ErrorCallback += (channelId, error) => self.OnError(channelId, error);
  29. self.Service.ReadCallback += (channelId, Memory) => self.OnRead(channelId, Memory);
  30. self.Service.AcceptCallback += (channelId, IPAddress) => self.OnAccept(channelId, IPAddress);
  31. NetThreadComponent.Instance.Add(self.Service);
  32. }
  33. }
  34. [ObjectSystem]
  35. public class NetKcpComponentDestroySystem: DestroySystem<NetKcpComponent>
  36. {
  37. public override void Destroy(NetKcpComponent self)
  38. {
  39. NetThreadComponent.Instance.Remove(self.Service);
  40. self.Service.Destroy();
  41. }
  42. }
  43. public static void OnRead(this NetKcpComponent self, long channelId, MemoryStream memoryStream)
  44. {
  45. Session session = self.GetChild<Session>(channelId);
  46. if (session == null)
  47. {
  48. return;
  49. }
  50. session.LastRecvTime = TimeHelper.ClientNow();
  51. SessionStreamDispatcher.Instance.Dispatch(self.SessionStreamDispatcherType, session, memoryStream);
  52. }
  53. public static void OnError(this NetKcpComponent self, long channelId, int error)
  54. {
  55. Session session = self.GetChild<Session>(channelId);
  56. if (session == null)
  57. {
  58. return;
  59. }
  60. session.Error = error;
  61. session.Dispose();
  62. }
  63. // 这个channelId是由CreateAcceptChannelId生成的
  64. public static void OnAccept(this NetKcpComponent self, long channelId, IPEndPoint ipEndPoint)
  65. {
  66. Session session = self.AddChildWithId<Session, AService>(channelId, self.Service);
  67. session.RemoteAddress = ipEndPoint;
  68. // 挂上这个组件,5秒就会删除session,所以客户端验证完成要删除这个组件。该组件的作用就是防止外挂一直连接不发消息也不进行权限验证
  69. session.AddComponent<SessionAcceptTimeoutComponent>();
  70. // 客户端连接,2秒检查一次recv消息,10秒没有消息则断开
  71. session.AddComponent<SessionIdleCheckerComponent, int>(NetThreadComponent.checkInteral);
  72. }
  73. public static Session Get(this NetKcpComponent self, long id)
  74. {
  75. Session session = self.GetChild<Session>(id);
  76. return session;
  77. }
  78. public static Session Create(this NetKcpComponent self, IPEndPoint realIPEndPoint)
  79. {
  80. long channelId = RandomHelper.RandInt64();
  81. Session session = self.AddChildWithId<Session, AService>(channelId, self.Service);
  82. session.RemoteAddress = realIPEndPoint;
  83. session.AddComponent<SessionIdleCheckerComponent, int>(NetThreadComponent.checkInteral);
  84. self.Service.GetOrCreate(session.Id, realIPEndPoint);
  85. return session;
  86. }
  87. public static Session Create(this NetKcpComponent self, IPEndPoint routerIPEndPoint, IPEndPoint realIPEndPoint, uint localConn)
  88. {
  89. long channelId = self.Service.CreateConnectChannelId(localConn);
  90. Session session = self.AddChildWithId<Session, AService>(channelId, self.Service);
  91. session.RemoteAddress = realIPEndPoint;
  92. session.AddComponent<SessionIdleCheckerComponent, int>(NetThreadComponent.checkInteral);
  93. self.Service.GetOrCreate(session.Id, routerIPEndPoint);
  94. return session;
  95. }
  96. }
  97. }