コード例 #1
0
        public async Task <bool> SendPing()
        {
            if (!_isInitialized)
            {
                return(true);
            }

            if (_pongCompletionSource != null)
            {
                if (Logger.IsWarn)
                {
                    Logger.Warn($"Another ping request in process: {P2PSession.RemoteNodeId}");
                }
                return(true);
            }

            _pongCompletionSource = new TaskCompletionSource <Packet>();
            var pongTask = _pongCompletionSource.Task;

            if (Logger.IsTrace)
            {
                Logger.Trace($"{P2PSession.RemoteNodeId} P2P sending ping on {P2PSession.RemotePort} ({RemoteClientId})");
            }
            Send(PingMessage.Instance);
            P2PSession?.NodeStats.AddNodeStatsEvent(NodeStatsEventType.P2PPingOut);
            var pingPerfCalcId = PerfService.StartPerfCalc();

            var firstTask = await Task.WhenAny(pongTask, Task.Delay(Timeouts.P2PPing));

            _pongCompletionSource = null;
            if (firstTask != pongTask)
            {
                return(false);
            }

            var latency = PerfService.EndPerfCalc(pingPerfCalcId);

            if (latency.HasValue)
            {
                P2PSession?.NodeStats.AddLatencyCaptureEvent(NodeLatencyStatType.P2PPingPong, latency.Value);
            }

            return(true);
        }
コード例 #2
0
        private async Task <BlockHeader[]> SendRequest(GetBlockHeadersMessage message, CancellationToken token)
        {
            if (Logger.IsTrace)
            {
                Logger.Trace("Sending headers request:");
                Logger.Trace($"Starting blockhash: {message.StartingBlockHash}");
                Logger.Trace($"Starting number: {message.StartingBlockNumber}");
                Logger.Trace($"Skip: {message.Skip}");
                Logger.Trace($"Reverse: {message.Reverse}");
                Logger.Trace($"Max headers: {message.MaxHeaders}");
            }

            var request = new Request <GetBlockHeadersMessage, BlockHeader[]>(message);

            _headersRequests.Add(request, token);
            var perfCalcId = PerfService.StartPerfCalc();

            Send(request.Message);
            Task <BlockHeader[]> task = request.CompletionSource.Task;
            var firstTask             = await Task.WhenAny(task, Task.Delay(Timeouts.Eth62, token));

            if (firstTask.IsCanceled)
            {
                token.ThrowIfCancellationRequested();
            }

            if (firstTask == task)
            {
                var latency = PerfService.EndPerfCalc(perfCalcId);
                if (latency.HasValue)
                {
                    P2PSession?.NodeStats.AddLatencyCaptureEvent(NodeLatencyStatType.BlockHeaders, latency.Value);
                }
                return(task.Result);
            }

            // TODO: work in progress
            throw new TimeoutException($"{P2PSession.RemoteNodeId} Request timeout in {nameof(GetBlockHeadersMessage)}");
        }