/// <summary>
 /// Creates a new instance using the specified <see cref="IDecodingState"/>.
 /// </summary>
 /// <param name="state">the <see cref="IDecodingState"/></param>
 /// <exception cref="ArgumentNullException">if the specified state is <code>null</code></exception>
 public DecodingStateProtocolDecoder(IDecodingState state)
 {
     if (state == null)
     {
         throw new ArgumentNullException("state");
     }
     _state = state;
 }
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            IDecodingState state = CurrentState;

            Int32 limit = input.Limit, pos = input.Position;

            try
            {
                while (true)
                {
                    // Wait for more data if all data is consumed.
                    if (pos == limit)
                    {
                        break;
                    }

                    IDecodingState oldState = state;
                    state = state.Decode(input, _childOutput);

                    // If finished, call finishDecode
                    if (state == null)
                    {
                        return(FinishDecode(_childProducts, output));
                    }

                    Int32 newPos = input.Position;

                    // Wait for more data if nothing is consumed and state didn't change.
                    if (newPos == pos && oldState == state)
                    {
                        break;
                    }

                    pos = newPos;
                }

                return(this);
            }
            catch (Exception)
            {
                state = null;
                throw;
            }
            finally
            {
                _currentState = state;

                // Destroy if decoding is finished or failed.
                if (state == null)
                {
                    Cleanup();
                }
            }
        }
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            IDecodingState state = CurrentState;

            Int32 limit = input.Limit, pos = input.Position;

            try
            {
                while (true)
                {
                    // Wait for more data if all data is consumed.
                    if (pos == limit)
                        break;

                    IDecodingState oldState = state;
                    state = state.Decode(input, _childOutput);

                    // If finished, call finishDecode
                    if (state == null)
                        return FinishDecode(_childProducts, output);

                    Int32 newPos = input.Position;

                    // Wait for more data if nothing is consumed and state didn't change.
                    if (newPos == pos && oldState == state)
                        break;

                    pos = newPos;
                }

                return this;
            }
            catch (Exception)
            {
                state = null;
                throw;
            }
            finally
            {
                _currentState = state;

                // Destroy if decoding is finished or failed.
                if (state == null)
                    Cleanup();
            }
        }
        public IDecodingState FinishDecode(IProtocolDecoderOutput output)
        {
            IDecodingState nextState;
            IDecodingState state = CurrentState;

            try
            {
                while (true)
                {
                    IDecodingState oldState = state;
                    state = state.FinishDecode(_childOutput);
                    if (state == null)
                    {
                        // Finished
                        break;
                    }

                    // Exit if state didn't change.
                    if (oldState == state)
                    {
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                state = null;
                if (log.IsDebugEnabled)
                {
                    log.Debug("Ignoring the exception caused by a closed session.", ex);
                }
            }
            finally
            {
                _currentState = state;
                nextState     = FinishDecode(_childProducts, output);
                if (state == null)
                {
                    Cleanup();
                }
            }
            return(nextState);
        }
        public IDecodingState FinishDecode(IProtocolDecoderOutput output)
        {
            IDecodingState nextState;
            IDecodingState state = CurrentState;

            try
            {
                while (true)
                {
                    IDecodingState oldState = state;
                    state = state.FinishDecode(_childOutput);
                    if (state == null)
                        // Finished
                        break;

                    // Exit if state didn't change.
                    if (oldState == state)
                        break;
                }
            }
            catch (Exception ex)
            {
                state = null;
                Debug.WriteLine("Ignoring the exception caused by a closed session. {0}", ex);
            }
            finally
            {
                _currentState = state;
                nextState = FinishDecode(_childProducts, output);
                if (state == null)
                    Cleanup();
            }
            return nextState;
        }
 public DecodingStateProtocolDecoder(IDecodingState state)
 {
     if (state == null)
         throw new ArgumentNullException("state");
     _state = state;
 }