NetWSComponentAwakeSystem.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace ET
  9. {
  10. [ObjectSystem]
  11. public class NetWSComponentAwakeSystem : AwakeSystem<NetWSComponent, int>
  12. {
  13. public override void Awake(NetWSComponent self, int sessionStreamDispatcherType)
  14. {
  15. self.SessionStreamDispatcherType = sessionStreamDispatcherType;
  16. self.AService = new WService();
  17. self.AService.ReadCallback = self.OnRead;
  18. self.AService.ErrorCallback = self.OnError;
  19. }
  20. }
  21. [ObjectSystem]
  22. public class NetWSComponentAwake1System : AwakeSystem<NetWSComponent, IEnumerable<string>>
  23. {
  24. public override void Awake(NetWSComponent self, IEnumerable<string> prefixs)
  25. {
  26. self.AService = new WService(prefixs);
  27. self.AService.ErrorCallback += (channelId, error) => self.OnError(channelId, error);
  28. self.AService.ReadCallback += (channelId, Memory) => self.OnRead(channelId, Memory);
  29. self.AService.AcceptCallback += (channelId, IPAddress) => self.OnAccept(channelId, IPAddress);
  30. NetThreadComponent.Instance.Add(self.AService);
  31. }
  32. }
  33. [ObjectSystem]
  34. public class NetWSComponentDestroySystem : DestroySystem<NetWSComponent>
  35. {
  36. public override void Destroy(NetWSComponent self)
  37. {
  38. self.AService.Dispose();
  39. }
  40. }
  41. public static class NetWSComponentSystem
  42. {
  43. public static void OnRead(this NetWSComponent 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 NetWSComponent 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 NetWSComponent self, long channelId, IPEndPoint ipEndPoint)
  65. {
  66. Session session = self.AddChildWithId<Session, AService>(channelId, self.AService);
  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 NetWSComponent self, long id)
  74. {
  75. Session session = self.GetChild<Session>(id);
  76. return session;
  77. }
  78. public static Session Create(this NetWSComponent self, IPEndPoint realIPEndPoint, string address)
  79. {
  80. long channelId = RandomHelper.RandInt64();
  81. Session session = self.AddChildWithId<Session, AService>(channelId, self.AService);
  82. session.RemoteAddress = new IPEndPoint(IPAddress.Parse("0.0.0.0"), 0);
  83. session.AddComponent<SessionIdleCheckerComponent, int>(NetThreadComponent.checkInteral);
  84. self.AService.Create(session.Id, realIPEndPoint, address);
  85. return session;
  86. }
  87. }
  88. }