예제 #1
0
        public async Task Run()
        {
            stop = new ManualResetEvent(false);

            if (!string.IsNullOrEmpty(Url))
            {
                var client = new HttpClient();
                client.Timeout = TimeSpan.FromMilliseconds(Timeout);
                client.DefaultRequestHeaders.Add("Accept", "application/xml");

                try
                {
                    // Raise Started Event
                    Started?.Invoke();

                    var stream = await client.GetStreamAsync(Url);

                    using (var reader = new StreamReader(stream))
                    {
                        string prevLine = null;

                        while (!stop.WaitOne(0, true) && !reader.EndOfStream)
                        {
                            var line = reader.ReadLine();
                            if (!string.IsNullOrEmpty(line))
                            {
                                if (IsXml(line) && IsXml(prevLine))
                                {
                                    var xml = $"{prevLine}{line}";
                                    XmlReceived?.Invoke(xml);
                                }

                                prevLine = line;
                            }
                        }
                    }
                }
                catch (HttpRequestException e)
                {
                    ConnectionError?.Invoke(e);
                }
            }

            Stopped?.Invoke();
        }
예제 #2
0
        private void Worker()
        {
            try
            {
                var request = (HttpWebRequest)WebRequest.Create(Url);
                using (var response = (HttpWebResponse)request.GetResponse())
                    using (var stream = response.GetResponseStream())
                        using (var reader = new StreamReader(stream, Encoding.GetEncoding("utf-8")))
                        {
                            // Set Read Buffer
                            var buffer = new char[1048576]; // 1 MB
                            int i      = reader.Read(buffer, 0, buffer.Length);

                            string xml = "";

                            while (i > 0 && !stop.WaitOne(0, true))
                            {
                                // Get string from buffer
                                var s = new string(buffer, 0, i);

                                // Find Beginning of XML (exclude HTTP header)
                                int b = s.IndexOf("<?xml ");
                                if (b >= 0)
                                {
                                    s = s.Substring(b);
                                }

                                // Add buffer to XML
                                xml += s;

                                // Find Closing Tag
                                int c = FindClosingTag(xml);

                                // Test if End of XML
                                if (c >= 0)
                                {
                                    b = xml.LastIndexOf("<?xml ");
                                    if (c > b)
                                    {
                                        // Raise XML Received Event and pass XML
                                        XmlReceived?.Invoke(xml.Substring(b, c - b));

                                        // Remove MTConnect document from xml
                                        xml = xml.Remove(b, c - b).Trim();
                                    }

                                    if (!string.IsNullOrEmpty(xml))
                                    {
                                        // Raise XML Error and break from while
                                        XmlError?.Invoke(xml);
                                        break;
                                    }

                                    // Reset XML
                                    xml = "";
                                }

                                // Clear Buffer
                                Array.Clear(buffer, 0, buffer.Length);

                                // Read Next Chunk
                                i = reader.Read(buffer, 0, buffer.Length);
                            }
                        }
            }
            catch (Exception ex)
            {
                ConnectionError?.Invoke(ex);
            }
        }
예제 #3
0
        public async Task Run()
        {
            stop = new CancellationTokenSource();

            if (!string.IsNullOrEmpty(Url))
            {
                var client = new HttpClient();
                client.Timeout = TimeSpan.FromMilliseconds(Timeout);
                client.DefaultRequestHeaders.Add("Accept", "application/xml");

                try
                {
                    // Raise Started Event
                    Started?.Invoke();

                    var stream = await client.GetStreamAsync(Url);

                    using (var reader = new StreamReader(stream))
                    {
                        string prevLine = null;
                        string doc      = null;
                        var    xmlFound = false;

                        while (!stop.IsCancellationRequested && !reader.EndOfStream)
                        {
                            var line = reader.ReadLine();
                            if (!string.IsNullOrEmpty(line))
                            {
                                // Test if Previous Line was beginning of XML Document
                                if (IsXml(prevLine))
                                {
                                    xmlFound = true;
                                    doc      = prevLine;
                                }

                                // Test if Current Line is a HTML Header
                                if (IsHeader(line))
                                {
                                    xmlFound = false;
                                }

                                // Build XML Document
                                if (xmlFound)
                                {
                                    doc += line;
                                }

                                // Test if End of XML Document
                                if (IsEnd(line) && !string.IsNullOrEmpty(doc))
                                {
                                    XmlReceived?.Invoke(doc);
                                    doc = null;
                                }

                                prevLine = line;
                            }
                        }
                    }
                }
                catch (HttpRequestException e)
                {
                    ConnectionError?.Invoke(e);
                }
            }

            Stopped?.Invoke();
        }