NetThreadComponentSystem.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 AwakeSystem: AwakeSystem<NetThreadComponent>
  10. {
  11. protected override void Awake(NetThreadComponent self)
  12. {
  13. NetThreadComponent.Instance = self;
  14. // 网络线程
  15. self.thread = new Thread(self.NetThreadUpdate);
  16. self.thread.Start();
  17. }
  18. }
  19. [ObjectSystem]
  20. public class LateUpdateSystem: LateUpdateSystem<NetThreadComponent>
  21. {
  22. protected override void LateUpdate(NetThreadComponent self)
  23. {
  24. self.MainThreadUpdate();
  25. }
  26. }
  27. [ObjectSystem]
  28. public class DestroySystem: DestroySystem<NetThreadComponent>
  29. {
  30. protected override void Destroy(NetThreadComponent self)
  31. {
  32. NetThreadComponent.Instance = null;
  33. self.isStop = true;
  34. self.thread.Join(1000);
  35. }
  36. }
  37. // 主线程Update
  38. private static void MainThreadUpdate(this NetThreadComponent self)
  39. {
  40. NetServices.Instance.UpdateInMainThread();
  41. }
  42. // 网络线程Update
  43. private static void NetThreadUpdate(this NetThreadComponent self)
  44. {
  45. while (!self.isStop)
  46. {
  47. NetServices.Instance.UpdateInNetThread();
  48. Thread.Sleep(1);
  49. }
  50. }
  51. }
  52. }