NetServerComponentSystem.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System.Net;
  2. namespace ET.Server
  3. {
  4. [FriendOf(typeof(NetServerComponent))]
  5. public static class NetServerComponentSystem
  6. {
  7. [ObjectSystem]
  8. public class AwakeSystem: AwakeSystem<NetServerComponent, IPEndPoint, NetworkProtocol>
  9. {
  10. protected override void Awake(NetServerComponent self, IPEndPoint address, NetworkProtocol protocol)
  11. {
  12. switch (protocol)
  13. {
  14. case NetworkProtocol.KCP:
  15. {
  16. self.ServiceId = NetServices.Instance.AddService(new KService(address, ServiceType.Outer));
  17. break;
  18. }
  19. case NetworkProtocol.TCP:
  20. {
  21. self.ServiceId = NetServices.Instance.AddService(new TService(address, ServiceType.Outer));
  22. break;
  23. }
  24. }
  25. NetServices.Instance.RegisterAcceptCallback(self.ServiceId, self.OnAccept);
  26. NetServices.Instance.RegisterReadCallback(self.ServiceId, self.OnRead);
  27. NetServices.Instance.RegisterErrorCallback(self.ServiceId, self.OnError);
  28. }
  29. }
  30. [ObjectSystem]
  31. public class NetKcpComponentDestroySystem: DestroySystem<NetServerComponent>
  32. {
  33. protected override void Destroy(NetServerComponent self)
  34. {
  35. NetServices.Instance.RemoveService(self.ServiceId);
  36. }
  37. }
  38. private static void OnError(this NetServerComponent self, long channelId, int error)
  39. {
  40. Session session = self.GetChild<Session>(channelId);
  41. if (session == null)
  42. {
  43. return;
  44. }
  45. session.Error = error;
  46. session.Dispose();
  47. }
  48. // 这个channelId是由CreateAcceptChannelId生成的
  49. private static void OnAccept(this NetServerComponent self, long channelId, IPEndPoint ipEndPoint)
  50. {
  51. Session session = self.AddChildWithId<Session, int>(channelId, self.ServiceId);
  52. session.RemoteAddress = ipEndPoint;
  53. if (self.DomainScene().SceneType == SceneType.BenchmarkServer)
  54. {
  55. return;
  56. }
  57. if (ConstValue.IsDebug == 1)
  58. {
  59. return;
  60. }
  61. // 挂上这个组件,5秒就会删除session,所以客户端验证完成要删除这个组件。该组件的作用就是防止外挂一直连接不发消息也不进行权限验证
  62. session.AddComponent<SessionAcceptTimeoutComponent>();
  63. // 客户端连接,2秒检查一次recv消息,10秒没有消息则断开
  64. session.AddComponent<SessionIdleCheckerComponent>();
  65. if (session.DomainScene().SceneType == SceneType.Game)
  66. {
  67. // 挂上心跳超时检测组件
  68. session.AddComponent<HeartBeatTimeoutComponent>();
  69. }
  70. }
  71. private static void OnRead(this NetServerComponent self, long channelId, long actorId, object message)
  72. {
  73. Session session = self.GetChild<Session>(channelId);
  74. if (session == null)
  75. {
  76. return;
  77. }
  78. session.LastRecvTime = TimeHelper.ClientNow();
  79. OpcodeHelper.LogMsg(self.DomainZone(), message);
  80. EventSystem.Instance.Publish(NetServerComponentOnRead.Clone(session, message));
  81. }
  82. }
  83. }