Exemplo n.º 1
0
        public async Task InitAsync()
        {
            var timer = new System.Timers.Timer {
                Interval = SendStatsInterval
            };

            timer.Elapsed           += TimerOnElapsed;
            _blockTree.NewHeadBlock += BlockTreeOnNewHeadBlock;
            var exitEvent = new ManualResetEvent(false);

            using (_websocketClient = await _ethStatsClient.InitAsync())
            {
                if (_logger.IsInfo)
                {
                    _logger.Info("Initial connection, sending 'hello' message...");
                }
                await SendHelloAsync();

                _connected = true;
                _websocketClient.ReconnectionHappened.Subscribe(async reason =>
                {
                    if (_logger.IsInfo)
                    {
                        _logger.Info("ETH Stats reconnected, sending 'hello' message...");
                    }
                    await SendHelloAsync();
                    _connected = true;
                });
                _websocketClient.DisconnectionHappened.Subscribe(reason =>
                {
                    _connected = false;
                    if (_logger.IsWarn)
                    {
                        _logger.Warn($"ETH Stats disconnected, reason: {reason}");
                    }
                });

                timer.Start();
                exitEvent.WaitOne();
            }

            _connected = false;
            timer.Stop();
            timer.Dispose();
            _blockTree.NewHeadBlock -= BlockTreeOnNewHeadBlock;
            timer.Elapsed           -= TimerOnElapsed;
        }
        public async Task InitAsync()
        {
            _timer = new Timer {
                Interval = SendStatsInterval
            };
            _timer.Elapsed          += TimerOnElapsed;
            _blockTree.NewHeadBlock += BlockTreeOnNewHeadBlock;
            _websocketClient         = await _ethStatsClient.InitAsync();

            if (_logger.IsInfo)
            {
                _logger.Info("Initial connection, sending 'hello' message...");
            }
            await SendHelloAsync();

            _connected = true;

            Run(_timer);
        }
        public async Task InitAsync()
        {
            var exitEvent = new ManualResetEvent(false);

            using (var client = await _ethStatsClient.InitAsync())
            {
                if (_logger.IsInfo)
                {
                    _logger.Info("Initial connection, sending 'hello' message...");
                }
                await SendHelloAsync(client);

                _connected = true;
                client.ReconnectionHappened.Subscribe(async reason =>
                {
                    if (_logger.IsInfo)
                    {
                        _logger.Info("ETH Stats reconnected, sending 'hello' message...");
                    }
                    await SendHelloAsync(client);
                    _connected = true;
                });
                client.DisconnectionHappened.Subscribe(reason =>
                {
                    _connected = false;
                    if (_logger.IsWarn)
                    {
                        _logger.Warn($"ETH Stats disconnected, reason: {reason}");
                    }
                });

                _blockTree.NewHeadBlock += async(s, e) =>
                {
                    if (!_connected)
                    {
                        return;
                    }

                    var timestamp = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
                    if (timestamp - _lastBlockProcessedTimestamp < ThrottlingThreshold)
                    {
                        return;
                    }

                    if (_logger.IsDebug)
                    {
                        _logger.Debug("ETH Stats sending 'block', 'pending' messages...");
                    }
                    _lastBlockProcessedTimestamp = timestamp;
                    await SendBlockAsync(client, e.Block);
                    await SendPendingAsync(client, e.Block.Transactions?.Length ?? 0);
                };

                var timer = new System.Timers.Timer {
                    Interval = SendStatsInterval
                };
                timer.Elapsed += async(sender, args) =>
                {
                    if (!_connected)
                    {
                        return;
                    }

                    if (_logger.IsDebug)
                    {
                        _logger.Debug("ETH Stats sending 'stats' message...");
                    }
                    await SendStatsAsync(client);
                };
                timer.Start();

                exitEvent.WaitOne();
            }
        }