Entity.cs 3.4 KB

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