NetKcpComponentSystem.cs 4.9 KB

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