Esempio n. 1
0
        /// <summary>
        /// Reads asynchronously from the stream, terminating as soon as the <paramref name="regex"/> is matched.
        /// </summary>
        /// <param name="regex">The regex to match.</param>
        /// <param name="timeout">The maximum time to wait.</param>
        /// <param name="millisecondSpin">The millisecond spin between each read from the stream.</param>
        /// <returns>Any text read from the stream.</returns>
        public async Task <string> TerminatedReadAsync(Regex regex, TimeSpan timeout, int millisecondSpin)
        {
            Func <string, bool> isTerminated = (x) => Client.IsRegexLocated(regex, x);
            string s = await this.TerminatedReadAsync(isTerminated, timeout, millisecondSpin);

            if (!isTerminated(s))
            {
                System.Diagnostics.Debug.WriteLine(string.Format("Failed to match '{0}' with '{1}'", s, regex.ToString()));
            }

            return(s);
        }
Esempio n. 2
0
        /// <summary>
        /// Reads asynchronously from the stream, terminating as soon as the <paramref name="regex"/> is matched.
        /// </summary>
        /// <param name="regex">The regex to match.</param>
        /// <param name="timeout">The maximum time to wait.</param>
        /// <param name="millisecondSpin">The millisecond spin between each read from the stream.</param>
        /// <returns>Any text read from the stream.</returns>
        public async Task <string> TerminatedReadAsync(Regex regex, TimeSpan timeout, int millisecondSpin)
        {
            Func <string, bool> isTerminated = (x) => Client.IsRegexLocated(regex, x);
            string s = await this.TerminatedReadAsync(isTerminated, timeout, millisecondSpin);

            if (!isTerminated(s))
            {
                System.Console.WriteLine($"Failed to match '{s}' with '{regex.ToString()}'");
            }

            return(s);
        }
Esempio n. 3
0
        /// <summary>
        /// Reads synchronously from the stream, terminating as soon as the <paramref name="regex"/> is matched.
        /// </summary>
        /// <param name="regex">The regex to match.</param>
        /// <param name="timeout">The maximum time to wait.</param>
        /// <param name="millisecondSpin">The millisecond spin between each read from the stream.</param>
        /// <returns>Any text read from the stream.</returns>
        public string TerminatedRead(Regex regex, TimeSpan timeout, int millisecondSpin)
        {
            var endTimeout = DateTime.Now.Add(timeout);
            var s          = string.Empty;

            while (!Client.IsRegexLocated(regex, s) && endTimeout >= DateTime.Now)
            {
                s += this.Read(TimeSpan.FromMilliseconds(1));
            }

            if (!Client.IsRegexLocated(regex, s))
            {
                System.Diagnostics.Debug.Print(string.Format("Failed to match '{0}' with '{1}'", s, regex.ToString()));
            }

            return(s);
        }