NetThreadComponentSystem.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Threading;
  3. namespace ET
  4. {
  5. [ObjectSystem]
  6. public class NetThreadComponentAwakeSystem: AwakeSystem<NetThreadComponent>
  7. {
  8. public override void Awake(NetThreadComponent self)
  9. {
  10. NetThreadComponent.Instance = self;
  11. self.ThreadSynchronizationContext = ThreadSynchronizationContext.Instance;
  12. }
  13. }
  14. [ObjectSystem]
  15. public class NetThreadComponentUpdateSystem: LateUpdateSystem<NetThreadComponent>
  16. {
  17. public override void LateUpdate(NetThreadComponent self)
  18. {
  19. foreach (AService service in self.Services)
  20. {
  21. service.Update();
  22. }
  23. }
  24. }
  25. [ObjectSystem]
  26. public class NetThreadComponentDestroySystem: DestroySystem<NetThreadComponent>
  27. {
  28. public override void Destroy(NetThreadComponent self)
  29. {
  30. self.Stop();
  31. }
  32. }
  33. public static class NetThreadComponentSystem
  34. {
  35. public static void Stop(this NetThreadComponent self)
  36. {
  37. }
  38. public static void Add(this NetThreadComponent self, AService kService)
  39. {
  40. // 这里要去下一帧添加,避免foreach错误
  41. self.ThreadSynchronizationContext.PostNext(() =>
  42. {
  43. if (kService.IsDisposed())
  44. {
  45. return;
  46. }
  47. self.Services.Add(kService);
  48. });
  49. }
  50. public static void Remove(this NetThreadComponent self, AService kService)
  51. {
  52. // 这里要去下一帧删除,避免foreach错误
  53. self.ThreadSynchronizationContext.PostNext(() =>
  54. {
  55. if (kService.IsDisposed())
  56. {
  57. return;
  58. }
  59. self.Services.Remove(kService);
  60. });
  61. }
  62. }
  63. }