NetKcpComponentSystem.cs 4.1 KB

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