1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- using Common;
- using Communicator;
- using log4net;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace HengShan_Pump_NonIC
- {
- public class StateMachineMessageCutter : IMessageCutter<byte[]>
- {
- public byte[] Message { get; private set; }
- public event EventHandler OnMessageCut;
- static ILog innerLogger = log4net.LogManager.GetLogger("StateMachine");
- private string loggerAppendix = "HengShan_Pump_NonIC msgCutter ";
- private readonly SizableWindow<byte> window;
- private State nextState = State.Uninitialized;
- /// <summary>
- ///
- /// </summary>
- public StateMachineMessageCutter()
- {
- //this.initWindowSize = initWindowSize;
- this.window = new SizableWindow<byte>();
- this.window.OnWindowFull += (data) =>
- {
- switch (nextState)
- {
- case State.Uninitialized:
- if (data.First() == 0xFF)
- {
- //前导码(0FFH) +长度(0-20H)
- this.window.NewSize = 2;
- this.nextState = State.LengthReady;
- innerLogger.Debug(this.loggerAppendix + " state is State.Uninitialized and next is 0xFF, switch to LengthReady");
- }
- else
- this.window.Clear();
- break;
- case State.LengthReady:
- //this.DumpWindowToQueue();
- this.window.NewSize = this.window.Skip(1).First() + 2;
- this.nextState = State.BodyReady;
- innerLogger.Debug(this.loggerAppendix + " MsgBodyLen caculated with: " + this.window.NewSize);
- break;
- case State.BodyReady:
- //innerLogger.Debug(this.loggerAppendix + " Fire OnMessageConstructed with innerQueue: " + this.buffer.ToHexLogString());
- this.Message = this.window.ToArray();
- var safe = this.OnMessageCut;
- safe?.Invoke(this, null);
- this.nextState = State.Uninitialized;
- this.window.Clear();
- //this.window.NewSize = this.initWindowSize;
- break;
- default:
- throw new ArgumentOutOfRangeException();
- }
- };
- }
- private enum State
- {
- Uninitialized,
- //HeaderSeeking,
- HeaderReady,
- LengthReady,
- BodyReady,
- }
- public void Feed(byte[] next)
- {
- //innerLogger.Debug(this.loggerAppendix + " " + next.ToHexLogString() + " is feed in Window in state: " + nextState);
- for (int i = 0; i < next.Length; i++)
- this.window.Add(next[i]);
- }
- }
- }
|