Entity.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Diagnostics;
  5. using Aitex.Core.RT.Event;
  6. using Aitex.Core.RT.Log;
  7. namespace Aitex.Core.RT.Fsm
  8. {
  9. public class Entity
  10. {
  11. protected Thread thread = null;
  12. protected IStateMachine fsm = null;
  13. public bool Running { get; protected set;}
  14. public Entity()
  15. {
  16. }
  17. public bool Initialize()
  18. {
  19. Debug.Assert(fsm != null);
  20. Init();
  21. fsm.Start();
  22. thread = new Thread(new ThreadStart(fsm.Loop));
  23. thread.Name = fsm.Name;
  24. thread.Start();
  25. while (!thread.IsAlive)
  26. Thread.Sleep(1);
  27. return true;
  28. }
  29. public virtual void Terminate()
  30. {
  31. fsm.Stop();
  32. Term();
  33. if (thread != null && thread.IsAlive)
  34. {
  35. Thread.Sleep(300);
  36. if (thread.IsAlive)
  37. {
  38. try
  39. {
  40. thread.Abort();
  41. }
  42. catch (Exception ex)
  43. {
  44. LOG.Error(String.Format("Entity terminate has exception."), ex);
  45. }
  46. }
  47. }
  48. //Term();
  49. }
  50. protected virtual bool Init()
  51. {
  52. return true;
  53. }
  54. protected virtual void Term()
  55. {
  56. }
  57. protected void Transition<T, V>(T state, V msg, FsmFunc func, T next)
  58. {
  59. Debug.Assert(typeof(T).IsEnum && typeof(V).IsEnum);
  60. int _state = Convert.ToInt32(state);
  61. int _next = Convert.ToInt32(next);
  62. int _msg = Convert.ToInt32(msg);
  63. Transition(_state, _msg, func, _next);
  64. }
  65. protected void Transition(int state, int msg, FsmFunc func, int next)
  66. {
  67. if (fsm != null)
  68. fsm.Transition(state, msg, func, next);
  69. }
  70. protected void AnyStateTransition(int msg, FsmFunc func, int next)
  71. {
  72. if (fsm != null)
  73. fsm.AnyStateTransition(msg, func, next);
  74. }
  75. protected void AnyStateTransition<T, V>(V msg, FsmFunc func, T next)
  76. {
  77. Debug.Assert(typeof(T).IsEnum && typeof(V).IsEnum);
  78. int _next = Convert.ToInt32(next);
  79. int _msg = Convert.ToInt32(msg);
  80. AnyStateTransition(_msg, func, _next);
  81. }
  82. protected void EnterExitTransition<T, V>(T state, FsmFunc enter, Nullable<V> msg, FsmFunc exit) where V : struct
  83. {
  84. Debug.Assert(typeof(T).IsEnum && ((msg == null) || typeof(V).IsEnum));
  85. int _state = Convert.ToInt32(state);
  86. int _msg = msg == null ? (int)FSM_MSG.NONE : Convert.ToInt32(msg);
  87. EnterExitTransition(_state, enter, _msg, exit);
  88. }
  89. protected void EnterExitTransition(int state, FsmFunc enter, int msg, FsmFunc exit)
  90. {
  91. if (fsm != null)
  92. fsm.EnterExitTransition(state, enter, msg, exit);
  93. }
  94. public void PostMsg<T>(T msg, params object[] args) where T : struct
  95. {
  96. Debug.Assert(typeof(T).IsEnum);
  97. int id = Convert.ToInt32(msg);
  98. PostMsg(id, args);
  99. }
  100. public void PostMsg(int msg, params object[] args)
  101. {
  102. if (fsm == null)
  103. {
  104. LOG.Error($"fsm is null, post msg {msg}");
  105. return;
  106. }
  107. fsm.PostMsg(msg, args);
  108. }
  109. }
  110. }