NetKcpComponentSystem.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. namespace ET
  5. {
  6. [ObjectSystem]
  7. public class NetKcpComponentAwakeSystem: AwakeSystem<NetKcpComponent>
  8. {
  9. public override void Awake(NetKcpComponent self)
  10. {
  11. self.MessageDispatcher = new OuterMessageDispatcher();
  12. self.Service = new KService(NetThreadComponent.Instance.ThreadSynchronizationContext, ServiceType.Outer);
  13. self.Service.ErrorCallback += self.OnError;
  14. self.Service.ReadCallback += self.OnRead;
  15. NetThreadComponent.Instance.Add(self.Service);
  16. }
  17. }
  18. [ObjectSystem]
  19. public class NetKcpComponentAwake1System: AwakeSystem<NetKcpComponent, IPEndPoint>
  20. {
  21. public override void Awake(NetKcpComponent self, IPEndPoint address)
  22. {
  23. self.MessageDispatcher = new OuterMessageDispatcher();
  24. self.Service = new KService(NetThreadComponent.Instance.ThreadSynchronizationContext, address, ServiceType.Outer);
  25. self.Service.ErrorCallback += self.OnError;
  26. self.Service.ReadCallback += self.OnRead;
  27. self.Service.AcceptCallback += self.OnAccept;
  28. NetThreadComponent.Instance.Add(self.Service);
  29. }
  30. }
  31. [ObjectSystem]
  32. public class NetKcpComponentLoadSystem: LoadSystem<NetKcpComponent>
  33. {
  34. public override void Load(NetKcpComponent self)
  35. {
  36. self.MessageDispatcher = new OuterMessageDispatcher();
  37. }
  38. }
  39. [ObjectSystem]
  40. public class NetKcpComponentDestroySystem: DestroySystem<NetKcpComponent>
  41. {
  42. public override void Destroy(NetKcpComponent self)
  43. {
  44. NetThreadComponent.Instance.Remove(self.Service);
  45. self.Service.Destroy();
  46. }
  47. }
  48. public static class NetKcpComponentSystem
  49. {
  50. public static void OnRead(this NetKcpComponent self, long channelId, MemoryStream memoryStream)
  51. {
  52. Session session = self.GetChild<Session>(channelId);
  53. if (session == null)
  54. {
  55. return;
  56. }
  57. session.LastRecvTime = TimeHelper.ClientNow();
  58. self.MessageDispatcher.Dispatch(session, memoryStream);
  59. }
  60. public static void OnError(this NetKcpComponent self, long channelId, int error)
  61. {
  62. Session session = self.GetChild<Session>(channelId);
  63. if (session == null)
  64. {
  65. return;
  66. }
  67. session.Error = error;
  68. session.Dispose();
  69. }
  70. // 这个channelId是由CreateAcceptChannelId生成的
  71. public static void OnAccept(this NetKcpComponent self, long channelId, IPEndPoint ipEndPoint)
  72. {
  73. Session session = EntityFactory.CreateWithParentAndId<Session, AService>(self, channelId, self.Service);
  74. session.RemoteAddress = ipEndPoint;
  75. session.AddComponent<SessionAcceptTimeoutComponent>();
  76. // 客户端连接,2秒检查一次recv消息,10秒没有消息则断开
  77. session.AddComponent<SessionIdleCheckerComponent, int>(NetThreadComponent.checkInteral);
  78. }
  79. public static Session Get(this NetKcpComponent self, long id)
  80. {
  81. Session session = self.GetChild<Session>(id);
  82. return session;
  83. }
  84. public static Session Create(this NetKcpComponent self, IPEndPoint realIPEndPoint)
  85. {
  86. long channelId = RandomHelper.RandInt64();
  87. Session session = EntityFactory.CreateWithParentAndId<Session, AService>(self, channelId, self.Service);
  88. session.RemoteAddress = realIPEndPoint;
  89. session.AddComponent<SessionIdleCheckerComponent, int>(NetThreadComponent.checkInteral);
  90. self.Service.GetOrCreate(session.Id, realIPEndPoint);
  91. return session;
  92. }
  93. }
  94. }