예제 #1
0
        protected bool TryMakeProxyFromAndToAddresses(TryMakeDelegate <string> @delegate, out IPAddress fromIp,
                                                      out IPAddress toIp)
        {
            fromIp = null;
            toIp   = null;

            if (TryMake(@delegate, out var fromAddress) == false)
            {
                return(false);
            }

            if (!IPAddress.TryParse(fromAddress, out fromIp))
            {
                return(false);
            }

            Enumerator.Skip(TokenKind.Space);

            if (TryMake(@delegate, out var toAddress) == false)
            {
                return(false);
            }

            if (!IPAddress.TryParse(toAddress, out toIp))
            {
                return(false);
            }

            return(true);
        }
예제 #2
0
        private static bool TryMake <T>(TokenEnumerator enumerator, TryMakeDelegate <T> make, out T found)
        {
            int index = enumerator.Index;

            if (make(enumerator, out found))
            {
                return(true);
            }
            enumerator.Index = index;
            return(false);
        }
예제 #3
0
        /// <summary>
        /// Try to make a callback in a transactional way.
        /// </summary>
        /// <param name="delegate">The callback to perform the match.</param>
        /// <returns>true if the match could be made, false if not.</returns>
        protected bool TryMake(TryMakeDelegate @delegate)
        {
            var checkpoint = Enumerator.Checkpoint();

            if (@delegate() == false)
            {
                checkpoint.Rollback();
                return(false);
            }

            checkpoint.Dispose();
            return(true);
        }
예제 #4
0
        /// <summary>
        /// Try to make a callback in a transactional way.
        /// </summary>
        /// <param name="delegate">The callback to perform the match.</param>
        /// <param name="found">The parameter that was returned from the matching function.</param>
        /// <returns>true if the match could be made, false if not.</returns>
        protected bool TryMake <TOut>(TryMakeDelegate <TOut> @delegate, out TOut found)
        {
            var checkpoint = Enumerator.Checkpoint();

            if (@delegate(out found) == false)
            {
                checkpoint.Rollback();
                return(false);
            }

            checkpoint.Dispose();
            return(true);
        }
예제 #5
0
        /// <summary>
        /// Try to match a function on the enumerator ensuring that the enumerator is restored on failure.
        /// </summary>
        /// <param name="enumerator">The enumerator to attempt to match on.</param>
        /// <param name="make">The callback function to match on the enumerator.</param>
        /// <param name="found">The out value that was found.</param>
        /// <returns>true if the matching function successfully made a match, false if not.</returns>
        static bool TryMake <T>(TokenEnumerator enumerator, TryMakeDelegate <T> make, out T found)
        {
            using (var checkpoint = enumerator.Checkpoint())
            {
                if (make(enumerator, out found))
                {
                    return(true);
                }

                checkpoint.Rollback();
            }

            return(false);
        }
예제 #6
0
        /// <summary>
        /// Try to make a callback in a transactional way.
        /// </summary>
        /// <param name="delegate">The callback to perform the match.</param>
        /// <returns>true if the match could be made, false if not.</returns>
        public bool TryMake(TryMakeDelegate @delegate)
        {
            var span         = _span;
            var spanIndex    = _spanIndex;
            var spanPosition = _spanPosition;

            if (@delegate(ref this) == false)
            {
                _span         = span;
                _spanIndex    = spanIndex;
                _spanPosition = spanPosition;

                _hasPeeked = false;

                return(false);
            }

            return(true);
        }
예제 #7
0
        /// <summary>
        /// Try to make a callback in a transactional way.
        /// </summary>
        /// <param name="delegate">The callback to perform the match.</param>
        /// <param name="value1">The first out parameter that was found during the make operation.</param>
        /// <param name="value2">The second out parameter that was found during the make operation.</param>
        /// <returns>true if the match could be made, false if not.</returns>
        public bool TryMake <TOut1, TOut2>(TryMakeDelegate <TOut1, TOut2> @delegate, out TOut1 value1, out TOut2 value2)
        {
            if (_buffer.IsSingleSegment)
            {
                var index = _spanIndex;

                if (@delegate(ref this, out value1, out value2) == false)
                {
                    _spanIndex = index;
                    _hasPeeked = false;

                    return(false);
                }

                return(true);
            }

            throw new NotImplementedException();
        }
예제 #8
0
        /// <summary>
        /// Try to make a callback in a transactional way.
        /// </summary>
        /// <param name="delegate">The callback to perform the match.</param>
        /// <param name="found">The parameter that was returned from the matching function.</param>
        /// <returns>true if the match could be made, false if not.</returns>
        public bool TryMake <TOut>(TryMakeDelegate <TOut> @delegate, out TOut found)
        {
            var span         = _span;
            var spanIndex    = _spanIndex;
            var spanPosition = _spanPosition;

            if (@delegate(ref this, out found) == false)
            {
                _span         = span;
                _spanIndex    = spanIndex;
                _spanPosition = spanPosition;

                _hasPeeked = false;

                return(false);
            }

            return(true);
        }
예제 #9
0
        /// <summary>
        /// Try to make a callback in a transactional way.
        /// </summary>
        /// <param name="delegate">The callback to perform the match.</param>
        /// <param name="buffer">The buffer that was made.</param>
        /// <returns>true if the match could be made, false if not.</returns>
        public bool TryMake(TryMakeDelegate @delegate, out ReadOnlySequence <byte> buffer)
        {
            buffer = default;

            var span         = _span;
            var spanIndex    = _spanIndex;
            var spanPosition = _spanPosition;

            if (@delegate(ref this) == false)
            {
                _span         = span;
                _spanIndex    = spanIndex;
                _spanPosition = spanPosition;

                _hasPeeked = false;

                return(false);
            }

            buffer = _buffer.Slice(spanIndex, _spanIndex - spanIndex);
            return(true);
        }
예제 #10
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="delegate">The delegate to match the input.</param>
 /// <param name="transition">The transition function to move from the previous state to the new state.</param>
 public StateTransition(TryMakeDelegate @delegate, Func <SmtpSessionContext, SmtpState> transition)
 {
     Delegate   = @delegate;
     Transition = transition;
 }
예제 #11
0
 /// <summary>
 /// Add a state action.
 /// </summary>
 /// <param name="command">The name of the SMTP command.</param>
 /// <param name="tryMake">The function callback to create the command.</param>
 /// <param name="transitionTo">The state to transition to.</param>
 public void Replace(string command, TryMakeDelegate tryMake, SmtpState?transitionTo = null)
 {
     Remove(command);
     Add(command, tryMake, transitionTo ?? StateId);
 }
예제 #12
0
 /// <summary>
 /// Add a state action.
 /// </summary>
 /// <param name="command">The name of the SMTP command.</param>
 /// <param name="tryMake">The function callback to create the command.</param>
 /// <param name="transition">The function to determine the new state.</param>
 public void Add(string command, TryMakeDelegate tryMake, Func <SmtpSessionContext, SmtpState> transition)
 {
     Transitions.Add(command, new StateTransition(tryMake, transition));
 }
예제 #13
0
 /// <summary>
 /// Add a state action.
 /// </summary>
 /// <param name="command">The name of the SMTP command.</param>
 /// <param name="tryMake">The function callback to create the command.</param>
 /// <param name="transition">The state to transition to.</param>
 public void Add(string command, TryMakeDelegate tryMake, SmtpState transition)
 {
     Add(command, tryMake, context => transition);
 }
예제 #14
0
 /// <summary>
 /// Add a state action.
 /// </summary>
 /// <param name="command">The name of the SMTP command.</param>
 /// <param name="tryMake">The function callback to create the command.</param>
 public void Add(string command, TryMakeDelegate tryMake)
 {
     Add(command, tryMake, context => StateId);
 }
예제 #15
0
 /// <summary>
 /// Add a state action.
 /// </summary>
 /// <param name="command">The name of the SMTP command.</param>
 /// <param name="tryMake">The function callback to create the command.</param>
 /// <param name="transitionTo">The state to transition to.</param>
 public void Add(string command, TryMakeDelegate tryMake, SmtpState?transitionTo = null)
 {
     Actions.Add(command, Tuple.Create(tryMake, transitionTo ?? StateId));
 }
예제 #16
0
 /// <summary>
 /// Add a state action.
 /// </summary>
 /// <param name="command">The name of the SMTP command.</param>
 /// <param name="tryMake">The function callback to create the command.</param>
 /// <param name="transitionTo">The state to transition to.</param>
 public void Add(string command, TryMakeDelegate tryMake, SmtpState? transitionTo = null)
 {
     Actions.Add(command, Tuple.Create(tryMake, transitionTo ?? StateId));
 }