ActorComponent.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. namespace Model
  5. {
  6. public struct ActorMessageInfo
  7. {
  8. public Session Session;
  9. public ActorRequest Message;
  10. }
  11. [ObjectEvent]
  12. public class ActorComponentEvent : ObjectEvent<ActorComponent>, IAwake, IAwake<IEntityActorHandler>, IStart
  13. {
  14. public void Awake()
  15. {
  16. this.Get().Awake();
  17. }
  18. public void Awake(IEntityActorHandler iEntityActorHandler)
  19. {
  20. this.Get().Awake(iEntityActorHandler);
  21. }
  22. public void Start()
  23. {
  24. this.Get().Start();
  25. }
  26. }
  27. /// <summary>
  28. /// 挂上这个组件表示该Entity是一个Actor, 它会将Entity位置注册到Location Server, 接收的消息将会队列处理
  29. /// </summary>
  30. public class ActorComponent: Component
  31. {
  32. private IEntityActorHandler entityActorHandler;
  33. private long actorId;
  34. // 队列处理消息
  35. private readonly Queue<ActorMessageInfo> queue = new Queue<ActorMessageInfo>();
  36. private TaskCompletionSource<ActorMessageInfo> tcs;
  37. public void Awake()
  38. {
  39. this.entityActorHandler = new CommonEntityActorHandler();
  40. }
  41. public void Awake(IEntityActorHandler iEntityActorHandler)
  42. {
  43. this.entityActorHandler = iEntityActorHandler;
  44. }
  45. public async void Start()
  46. {
  47. this.actorId = this.Entity.Id;
  48. Game.Scene.GetComponent<ActorManagerComponent>().Add(this.Entity);
  49. await Game.Scene.GetComponent<LocationProxyComponent>().Add(this.actorId);
  50. this.HandleAsync();
  51. }
  52. public void Add(ActorMessageInfo info)
  53. {
  54. this.queue.Enqueue(info);
  55. if (this.tcs == null)
  56. {
  57. return;
  58. }
  59. var t = this.tcs;
  60. this.tcs = null;
  61. t.SetResult(this.queue.Dequeue());
  62. }
  63. private Task<ActorMessageInfo> GetAsync()
  64. {
  65. if (this.queue.Count > 0)
  66. {
  67. return Task.FromResult(this.queue.Dequeue());
  68. }
  69. this.tcs = new TaskCompletionSource<ActorMessageInfo>();
  70. return this.tcs.Task;
  71. }
  72. private async void HandleAsync()
  73. {
  74. while (true)
  75. {
  76. try
  77. {
  78. ActorMessageInfo info = await this.GetAsync();
  79. await this.entityActorHandler.Handle(info.Session, this.Entity, info.Message);
  80. }
  81. catch (Exception e)
  82. {
  83. Log.Error(e.ToString());
  84. }
  85. }
  86. }
  87. public override async void Dispose()
  88. {
  89. try
  90. {
  91. if (this.Id == 0)
  92. {
  93. return;
  94. }
  95. base.Dispose();
  96. Game.Scene.GetComponent<ActorManagerComponent>().Remove(actorId);
  97. await Game.Scene.GetComponent<LocationProxyComponent>().Remove(this.actorId);
  98. }
  99. catch (Exception)
  100. {
  101. Log.Error($"unregister actor fail: {this.actorId}");
  102. }
  103. }
  104. }
  105. }