Exemple #1
0
        static IEnumerable <XElement> StreamiRadioNet(ITestableNetworkStream netStream)
        {
            var settings = new XmlReaderSettings {
                ConformanceLevel = ConformanceLevel.Fragment, CheckCharacters = false
            };
            XmlParserContext context = new XmlParserContext(null, null, null, XmlSpace.None, Encoding.GetEncoding("ISO-8859-1"));  // needed to avoid exception "WDR 3 zum Nachhören"

            string[] waiting = new string[] { @" \ ", " | ", " / ", " - " };
            int      waited  = 0;

            using (reader = XmlReader.Create(netStream.GetStream(), settings, context))                                             //                                           ^---
            {
                while (true)
                {
                    if (reader.EOF)
                    {
                        Thread.Sleep(200);                                                                                  // need to re-open netstream, but how?
                        string waitingForSignal = "     waiting for signal  " + waiting[waited++ % 4] + "                "; // + "connected=" + netStream.Socket.connected;
                        ConsoleShow.Status(new XElement("value", waitingForSignal), Lines.Waiting);
                        if (waited > 5 * 1000 / 200)                                                                        // 60s
                        {
                            XElement el = new XElement("CloseStream");
                            yield return(el);
                        }
                    }
                    // reader.MoveToContent();
                    while (!reader.EOF)
                    {
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            XElement el;
                            try
                            {
                                el = XElement.ReadFrom(reader) as XElement;  // can ReadFrom() forever, if iRadio = "Nicht verfügbar" or "NOXON"
                            }
                            catch
                            {
                                el = new XElement("ConsoleStreamiRadioExceptionXElementAfterReadFromFails");
                            }
                            if (el != null)
                            {
                                yield return(el);
                            }
                        }
                        else
                        {
                            try
                            {
                                reader.Read();
                            }
                            catch
                            {
                                // continue
                            }
                        }
                    }
                }
            }
        }
        public static IEnumerable <XElement> StreamiRadioNet(ITestableNetworkStream netStream)
        {
            var settings = new XmlReaderSettings {
                ConformanceLevel = ConformanceLevel.Fragment, CheckCharacters = false, Async = true
            };
            XmlParserContext        context      = new XmlParserContext(null, null, null, XmlSpace.None, Encoding.GetEncoding("ISO-8859-1")); // needed to avoid exception on strings like "WDR 3 zum Nachhören"
            CancellationTokenSource cancellation = new CancellationTokenSource();                                                             // must not be (static) class variable

            System.Timers.Timer timeoutTimer = new System.Timers.Timer(Properties.Settings.Default.ReadTimeout);                              // check if ReadFrom(reader) times out
            timeoutTimer.Elapsed += (sender, e) => ParseTimeout(sender, e, cancellation);

            using (reader = XmlReader.Create(netStream.GetStream(), settings, context))
            {
                while (true)
                {
                    while (!reader.EOF)
                    {
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            XElement           el;
                            TaskStatus         tstat = TaskStatus.Created;
                            AggregateException tex   = null;

                            try
                            {
                                if (!FormShow.Browsing)
                                {
                                    timeoutTimer.Start();
                                }
                                //  https://docs.microsoft.com/de-de/dotnet/core/porting/
                                Task <XNode> t = XNode.ReadFromAsync(reader, cancellation.Token);
                                el    = t.Result as XElement; // ToDo: if iRadio = "Nicht verfügbar" or "NOXON" ==> ReadFromAsync() is canceled (OK!) but does not resume normal reading
                                tstat = t.Status;             // also: no more data received if <browse> menu
                                tex   = t.Exception;
                                if (!FormShow.Browsing)
                                {
                                    timeoutTimer.Stop();
                                }
                            }
                            catch (Exception ex)
                            {
                                el = new XElement("CloseStream", "FormStreamiRadioExceptionXElementAfterReadFromFails" + ex.Message + "=" + tex?.Message);
                            }
                            if (el != null)
                            {
                                yield return(el);
                            }
                        }
                        else
                        {
                            try
                            {
                                reader.Read();
                            }
                            catch
                            {
                                // continue
                            }
                        }
                    }
                }
            }
            // cancellation.Dispose();
        }