Ejemplo n.º 1
0
        private async Task ExecuteRequest(CancellationToken token, StateSyncBatch batch)
        {
            var peer = batch.AssignedPeer?.Current?.SyncPeer;

            if (peer != null)
            {
                var             hashes          = batch.RequestedNodes.Select(r => r.Hash).ToArray();
                Task <byte[][]> getNodeDataTask = peer.GetNodeData(hashes, token);
                await getNodeDataTask.ContinueWith(
                    t =>
                {
                    if (t.IsCompletedSuccessfully)
                    {
                        batch.Responses = getNodeDataTask.Result;
                    }
                }
                    );
            }

            (NodeDataHandlerResult Result, int NodesConsumed)result = (NodeDataHandlerResult.InvalidFormat, 0);
            try
            {
                if (batch.IsAdditionalDataConsumer)
                {
                    result = (NodeDataHandlerResult.OK, _additionalConsumer.HandleResponse(new DataConsumerRequest(batch.ConsumerId, batch.RequestedNodes.Select(r => r.Hash).ToArray()), batch.Responses));
                }
                else
                {
                    result = _feed.HandleResponse(batch);
                }

                if (result.Result == NodeDataHandlerResult.BadQuality)
                {
                    _syncPeerPool.ReportWeakPeer(batch.AssignedPeer);
                }
            }
            catch (Exception e)
            {
                if (_logger.IsError)
                {
                    _logger.Error($"Error when handling response", e);
                }
            }

            Interlocked.Add(ref _consumedNodesCount, result.NodesConsumed);
            if (result.NodesConsumed == 0 && peer != null)
            {
                _syncPeerPool.ReportNoSyncProgress(batch.AssignedPeer, !batch.IsAdditionalDataConsumer);
            }

            if (batch.AssignedPeer != null)
            {
                _syncPeerPool.Free(batch.AssignedPeer);
            }
        }