NetThreadComponentSystem.cs 2.0 KB

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