123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- #region --------------- Copyright Dresser Wayne Pignone -------------
- #endregion
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Wayne.Lib.StateEngine.Generic
- {
-
-
-
-
- public abstract class TimeoutState<TMain> : State<TMain>
- {
- #region Fields
- Wayne.Lib.StateEngine.Timer timer;
- object myToken;
- #endregion
-
- #region Methods
-
-
-
-
-
- protected override void Enter(Wayne.Lib.StateEngine.StateEntry stateEntry, ref Wayne.Lib.StateEngine.Transition transition)
- {
- base.Enter(stateEntry, ref transition);
- myToken = new object();
- var timeoutInterval = TimeoutInterval;
- if (timeoutInterval > 0)
- {
- timer = new Wayne.Lib.StateEngine.Timer(this, GenericEventType.Timeout, timeoutInterval, myToken);
- ActivateTimer(timer);
- }
- }
-
-
-
-
-
- protected sealed override void HandleEvent(Wayne.Lib.StateEngine.StateEngineEvent stateEngineEvent, ref Wayne.Lib.StateEngine.Transition transition)
- {
- base.HandleEvent(stateEngineEvent, ref transition);
- if (stateEngineEvent.Type.Equals(GenericEventType.Timeout))
- {
- Wayne.Lib.StateEngine.TimerEvent timerEvent = stateEngineEvent as Wayne.Lib.StateEngine.TimerEvent;
- if (timerEvent != null)
- {
- if (timerEvent.UserToken.Equals(myToken))
- {
- Timeout(ref transition);
- timerEvent.Handled = true;
- timer = null;
- }
- }
- }
- if (!stateEngineEvent.Handled)
- HandleNonTimeoutEvent(stateEngineEvent, ref transition);
- }
-
-
-
- protected override void Exit()
- {
- base.Exit();
- myToken = null;
- if (this.timer != null)
- {
- this.timer.Disable();
- this.timer = null;
- }
- }
-
-
-
-
-
- protected abstract void HandleNonTimeoutEvent(Wayne.Lib.StateEngine.StateEngineEvent stateEngineEvent, ref Wayne.Lib.StateEngine.Transition transition);
-
-
-
-
- protected abstract int TimeoutInterval { get;}
-
-
-
-
- protected abstract void Timeout(ref Wayne.Lib.StateEngine.Transition transition);
-
-
-
- protected void CancelTimer()
- {
- if (this.timer != null)
- {
- this.timer.Disable();
- this.timer = null;
- }
- }
-
-
-
- protected void ResetTimer()
- {
- CancelTimer();
- timer = new Wayne.Lib.StateEngine.Timer(this, GenericEventType.Timeout, TimeoutInterval, myToken);
- ActivateTimer(timer);
- }
- #endregion
- }
- }
|