Inheritance: System.EventArgs
        private void TaskComplete(object sender, TaskCompleteEventArgs e)
        {
            e.Task.Completed -= TaskComplete;

            // I should raise the event with some eventargs saying which node was dead
            var args = (SendQueryEventArgs) e;

            if (args.TimedOut)
            {
                // If the node didn't respond and it's no longer in our bucket,
                // we need to send a ping to the oldest node in the bucket
                // Otherwise if we have a non-responder and it's still there, replace it!
                var index = _bucket.Nodes.IndexOf(((SendQueryTask) e.Task).Target);
                if (index < 0)
                {
                    SendPingToOldest();
                }
                else
                {
                    _bucket.Nodes[index] = _newNode;
                    RaiseComplete(new TaskCompleteEventArgs(this));
                }
            }
            else
            {
                SendPingToOldest();
            }
        }
        private void SentAnnounce(object o, TaskCompleteEventArgs e)
        {
            e.Task.Completed -= SentAnnounce;
            _activeAnnounces--;

            if (_activeAnnounces == 0)
                RaiseComplete(new TaskCompleteEventArgs(this));
        }
        private void GetPeersCompleted(object o, TaskCompleteEventArgs e)
        {
            try
            {
                _activeQueries--;
                e.Task.Completed -= GetPeersCompleted;

                var args = (SendQueryEventArgs) e;

                // We want to keep a list of the top (K) closest nodes which have responded
                var target = ((SendQueryTask) args.Task).Target;
                
                //var index = ClosestActiveNodes.Values.IndexOf(target);

                var key = target.Id.Xor(_infoHash);
                int index = 0;
                foreach (var obj3 in this.ClosestActiveNodes.Keys)
                  {
                    if (!(obj3 == key))
                        ++index;
                    else
                        break;
                }

                if (index >= Bucket.MaxCapacity || args.TimedOut)
                    ClosestActiveNodes.RemoveAt(index);

                if (args.TimedOut)
                    return;

                var response = (GetPeersResponse) args.Response;

                // Ensure that the local Node object has the token. There may/may not be
                // an additional copy in the routing table depending on whether or not
                // it was able to fit into the table.
                target.Token = response.Token;
                if (response.Values != null)
                {
                    // We have actual peers!
                    Debug.WriteLine("Found peers");
                    _engine.RaisePeersFound(_infoHash, Peer.Decode(response.Values));
                }
                else if (response.Nodes != null)
                {
                    if (!Active)
                        return;
                    // We got a list of nodes which are closer
                    var newNodes = Node.FromCompactNode(response.Nodes);
                    foreach (var n in Node.CloserNodes(_infoHash, _closestNodes, newNodes, Bucket.MaxCapacity))
                        SendGetPeers(n);
                }
            }
            finally
            {
                if (_activeQueries == 0)
                    RaiseComplete(new TaskCompleteEventArgs(this));
            }
        }
        private void FindNodeComplete(object sender, TaskCompleteEventArgs e)
        {
            e.Task.Completed -= FindNodeComplete;
            _activeRequests--;

            var args = (SendQueryEventArgs) e;
            if (!args.TimedOut)
            {
                var response = (FindNodeResponse) args.Response;
                SendFindNode(Node.FromCompactNode(response.Nodes));
            }
            
            if (_activeRequests == 0)
                RaiseComplete(new TaskCompleteEventArgs(this));
        }
        private void GotPeers(object o, TaskCompleteEventArgs e)
        {
            e.Task.Completed -= GotPeers;
            var getpeers = (GetPeersTask) e.Task;
            foreach (var task in from n in getpeers.ClosestActiveNodes.Values
                where n.Token != null
                let query = new AnnouncePeer(_engine.LocalId, _infoHash, _port, n.Token)
                select new SendQueryTask(_engine, query, n))
            {
                task.Completed += SentAnnounce;
                task.Execute();
                _activeAnnounces++;
            }

            if (_activeAnnounces == 0)
                RaiseComplete(new TaskCompleteEventArgs(this));
        }
        protected override void RaiseComplete(TaskCompleteEventArgs e)
        {
            if (!Active)
                return;

            // If we were given a list of initial nodes and they were all dead,
            // initialise again except use the utorrent router.
            if (_initialNodes.Count > 0 && _engine.RoutingTable.CountNodes() < 10)
            {
                new InitialiseTask(_engine).Execute();
            }
            else
            {
                _engine.RaiseStateChanged(DhtState.Ready);
            }

            Active = false;
            base.RaiseComplete(e);
        }
Beispiel #7
0
 protected virtual void RaiseComplete(TaskCompleteEventArgs e)
 {
     Completed?.Invoke(this, e);
 }
 protected override void RaiseComplete(TaskCompleteEventArgs e)
 {
     Unhook();
     e.Task = this;
     base.RaiseComplete(e);
 }
        protected override void RaiseComplete(TaskCompleteEventArgs e)
        {
            if (!Active)
                return;

            Active = false;
            base.RaiseComplete(e);
        }