Entity.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 void Terminate()
  30. {
  31. fsm.Stop();
  32. Term();
  33. if (thread.IsAlive)
  34. {
  35. Thread.Sleep(100);
  36. if (thread.IsAlive)
  37. {
  38. try
  39. {
  40. thread.Abort();
  41. }
  42. catch (Exception ex)
  43. {
  44. LOG.WriteExeption(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(int msg, FsmFunc func, int next, FsmFunc monitorFunc)
  76. //{
  77. // if (fsm != null)
  78. // fsm.AnyStateTransition(msg, func, next,monitorFunc);
  79. //}
  80. protected void AnyStateTransition<T, V>(V msg, FsmFunc func, T next)
  81. {
  82. Debug.Assert(typeof(T).IsEnum && typeof(V).IsEnum);
  83. int _next = Convert.ToInt32(next);
  84. int _msg = Convert.ToInt32(msg);
  85. AnyStateTransition(_msg, func, _next);
  86. }
  87. protected void AnyStateTransition<T, V>(V msg, FsmFunc func, T next, FsmFunc monitorFunc)
  88. {
  89. Debug.Assert(typeof(T).IsEnum && typeof(V).IsEnum);
  90. int _next = Convert.ToInt32(next);
  91. int _msg = Convert.ToInt32(msg);
  92. AnyStateTransition(_msg, func, _next, monitorFunc);
  93. }
  94. protected void EnterExitTransition<T, V>(T state, FsmFunc enter, Nullable<V> msg, FsmFunc exit) where V : struct
  95. {
  96. Debug.Assert(typeof(T).IsEnum && ((msg == null) || typeof(V).IsEnum));
  97. int _state = Convert.ToInt32(state);
  98. int _msg = msg == null ? (int)FSM_MSG.NONE : Convert.ToInt32(msg);
  99. EnterExitTransition(_state, enter, _msg, exit);
  100. }
  101. protected void EnterExitTransition(int state, FsmFunc enter, int msg, FsmFunc exit)
  102. {
  103. if (fsm != null)
  104. fsm.EnterExitTransition(state, enter, msg, exit);
  105. }
  106. public void PostMsg<T>(T msg, params object[] args) where T : struct
  107. {
  108. Debug.Assert(typeof(T).IsEnum);
  109. int id = Convert.ToInt32(msg);
  110. PostMsg(id, args);
  111. }
  112. public void PostMsg(int msg, params object[] args)
  113. {
  114. if (fsm == null)
  115. {
  116. //LOG.Error($"fsm is null, post msg {msg}");
  117. return;
  118. }
  119. fsm.PostMsg(msg, args);
  120. }
  121. }
  122. }