示例#1
0
        public void run()
        {
            ConsoleDataPresenter presenter    = new ConsoleDataPresenter();
            SampleDataAggregator dtAggregator = new SampleDataAggregator(presenter);

            _timer           = new Timer();
            _timer.Interval  = LOG_INTERVAL;
            _timer.Elapsed  += dtAggregator.OnTimedEvent;
            _timer.AutoReset = true;
            _timer.Enabled   = true;

            using (DevTest.LorumIpsumStream sr = new DevTest.LorumIpsumStream())
            {
                while (sr.CanRead && !Console.KeyAvailable)
                {
                    byte[] buffer    = new byte[BUFF_SIZE];
                    int    itemsRead = sr.Read(buffer, 0, buffer.Length);
                    for (int i = 0; i < itemsRead; i++)
                    {
                        if (buffer[i] != 0)
                        {
                            dtAggregator.AddChar(Convert.ToChar(buffer[i]));
                        }
                    }
                }
            }

            // print final results
            dtAggregator.LogStatus();
        }
示例#2
0
        private static async Task StreamLoremIpsum(int index, ILogger logger, string signalRUrl)
        {
            index++;
            var connection = new HubConnectionBuilder()
                             .WithUrl(signalRUrl)
                             .Build();

            async Task OnConnectionOnClosed(Exception error)
            {
                logger.LogError(error, "Hub connection error.");
                logger.LogWarning($"Client #{index}. connection was closed. Restarting...");
                await Task.Delay(new Random().Next(0, 5) * 1000);

                await connection.StartAsync();
            }

            connection.Closed += OnConnectionOnClosed;

            await connection.StartAsync();

            logger.LogInformation($"Client #{index}. connection opened...");

            async IAsyncEnumerable <string> ClientStreamData()
            {
                using (var loremIpsumStream = new DevTest.LorumIpsumStream())
                {
                    using (var streamReader = new StreamReader(loremIpsumStream, Encoding.Unicode, true))
                    {
                        string text = null, leftOvers = null;
                        do
                        {
                            var data = await ReadUntilNextSpace(streamReader);

                            if (!string.IsNullOrWhiteSpace(data.text))
                            {
                                // it would be easier if source stream allowed seeking, but we can workaround that issue.
                                text      = leftOvers + data.text;
                                leftOvers = data.leftOvers; //going to be appended
                                logger.LogDebug(
                                    $"Client {index}. Fetched data from the stream: {text}. Sending to the server");
                                yield return(text);

                                await Task.Delay(1000);
                            }
                        } while (!string.IsNullOrWhiteSpace(text));

                        if (!string.IsNullOrWhiteSpace(leftOvers))
                        {
                            yield return(leftOvers);
                        }
                    }
                }
            }

            await connection.SendAsync("UploadLoremIpsumStream", ClientStreamData());
        }