示例#1
0
 private void QueryNode(Node node)
 {
     this.node = node;
     message = new FindNode(engine.LocalId, node.Id);
     task = new SendQueryTask(engine, message, node);
     task.Completed += TaskComplete;
     task.Execute();
 }
示例#2
0
 private void QueryNode(Node node)
 {
     _node            = node;
     _message         = new FindNode(_engine.LocalId, node.Id);
     _task            = new SendQueryTask(_engine, _message, node);
     _task.Completed += TaskComplete;
     _task.Execute();
 }
示例#3
0
 private void QueryNode(Node node)
 {
     _node = node;
     _message = new FindNode(_engine.LocalId, node.Id);
     _task = new SendQueryTask(_engine, _message, node);
     _task.Completed += TaskComplete;
     _task.Execute();
 }
示例#4
0
 private void SendFindNode(IEnumerable <Node> newNodes)
 {
     foreach (var node in Node.CloserNodes(_engine.LocalId, _nodes, newNodes, Bucket.MaxCapacity))
     {
         _activeRequests++;
         var request = new FindNode(_engine.LocalId, _engine.LocalId);
         var task    = new SendQueryTask(_engine, request, node);
         task.Completed += FindNodeComplete;
         task.Execute();
     }
 }
示例#5
0
        private void SendGetPeers(Node n)
        {
            NodeId distance = n.Id.Xor(infoHash);

            queriedNodes.Add(distance, n);

            activeQueries++;
            GetPeers      m    = new GetPeers(engine.LocalId, infoHash);
            SendQueryTask task = new SendQueryTask(engine, m, n);

            task.Completed += GetPeersCompleted;
            task.Execute();
        }
示例#6
0
        private void SendPingToOldest()
        {
            bucket.LastChanged = DateTime.UtcNow;
            bucket.SortBySeen();

            if ((DateTime.UtcNow - bucket.Nodes[0].LastSeen) < TimeSpan.FromMinutes(3))
            {
                RaiseComplete(new TaskCompleteEventArgs(this));
            }
            else
            {
                Node oldest = bucket.Nodes[0];
                SendQueryTask task = new SendQueryTask(engine, new Ping(engine.LocalId), oldest);
                task.Completed += TaskComplete;
                task.Execute();
            }
        }
示例#7
0
        private void SendPingToOldest()
        {
            _bucket.LastChanged = DateTime.UtcNow;
            _bucket.SortBySeen();

            if ((DateTime.UtcNow - _bucket.Nodes[0].LastSeen) < TimeSpan.FromMinutes(3))
            {
                RaiseComplete(new TaskCompleteEventArgs(this));
            }
            else
            {
                var oldest = _bucket.Nodes[0];
                var task   = new SendQueryTask(_engine, new Ping(_engine.LocalId), oldest);
                task.Completed += TaskComplete;
                task.Execute();
            }
        }
示例#8
0
        private void GotPeers(object o, TaskCompleteEventArgs e)
        {
            e.Task.Completed -= GotPeers;
            GetPeersTask getpeers = (GetPeersTask)e.Task;
            foreach (Node n in getpeers.ClosestActiveNodes.Values)
            {
                if (n.Token == null)
                    continue;
                AnnouncePeer query = new AnnouncePeer(engine.LocalId, infoHash, port, n.Token);
                SendQueryTask task = new SendQueryTask(engine, query, n);
                task.Completed += SentAnnounce;
                task.Execute();
                activeAnnounces++;
            }

            if (activeAnnounces == 0)
                RaiseComplete(new TaskCompleteEventArgs(this));
        }
        public void PingTimeout()
        {
            engine.TimeOut = TimeSpan.FromHours(1);
            // Send ping
            Ping ping = new Ping(node.Id);
            ping.TransactionId = transactionId;

            ManualResetEvent handle = new ManualResetEvent(false);
            SendQueryTask task = new SendQueryTask(engine, ping, node);
            task.Completed += delegate {
                handle.Set();
            };
            task.Execute();

            // Receive response
            PingResponse response = new PingResponse(node.Id, transactionId);
            listener.RaiseMessageReceived(response, node.EndPoint);

            Assert.IsTrue(handle.WaitOne(1000, true), "#0");

            engine.TimeOut = TimeSpan.FromMilliseconds(75);
            DateTime lastSeen = node.LastSeen;

            // Time out a ping
            ping = new Ping(node.Id);
            ping.TransactionId = (BEncodedString)"ab";

            task = new SendQueryTask(engine, ping, node, 4);
            task.Completed += delegate { handle.Set(); };

            handle.Reset();
            task.Execute();
            handle.WaitOne();

            Assert.AreEqual(4, node.FailedCount, "#1");
            Assert.AreEqual(NodeState.Bad, node.State, "#2");
            Assert.AreEqual(lastSeen, node.LastSeen, "#3");
        }
示例#10
0
        private void GotPeers(object o, TaskCompleteEventArgs e)
        {
            e.Task.Completed -= GotPeers;
            GetPeersTask getpeers = (GetPeersTask)e.Task;

            foreach (Node n in getpeers.ClosestActiveNodes.Values)
            {
                if (n.Token == null)
                {
                    continue;
                }
                AnnouncePeer  query = new AnnouncePeer(engine.LocalId, infoHash, port, n.Token);
                SendQueryTask task  = new SendQueryTask(engine, query, n);
                task.Completed += SentAnnounce;
                task.Execute();
                activeAnnounces++;
            }

            if (activeAnnounces == 0)
            {
                RaiseComplete(new TaskCompleteEventArgs(this));
            }
        }
示例#11
0
        internal void Add(Node node)
        {
            if (node == null)
                throw new ArgumentNullException("node");

            SendQueryTask task = new SendQueryTask(this, new Ping(RoutingTable.LocalNode.Id), node);
            task.Execute();
        }
示例#12
0
        public void SendQueryTaskTimeout()
        {
            _engine.TimeOut = TimeSpan.FromMilliseconds(25);

            var ping = new Ping(_engine.LocalId) {TransactionId = _transactionId};
            _engine.MessageLoop.QuerySent += (o, e) =>
                                                 {
                                                     if (e.TimedOut)
                                                         _counter++;
                                                 };

            var task = new SendQueryTask(_engine, ping, _node);
            task.Completed += (sender, args) => _handle.Set();
            task.Execute();
            Assert.IsTrue(_handle.WaitOne(3000, false), "#1");
            Assert.AreEqual(task.Retries, _counter);
        }
示例#13
0
        public void SendQueryTaskSucceed()
        {
            _engine.TimeOut = TimeSpan.FromMilliseconds(25);

            var ping = new Ping(_engine.LocalId) {TransactionId = _transactionId};
            _engine.MessageLoop.QuerySent += delegate(object o, SendQueryEventArgs e)
            {
                if (!e.TimedOut)
                    return;

                _counter++;
                var response = new PingResponse(_node.Id, _transactionId);
                _listener.RaiseMessageReceived(response, _node.EndPoint);
            };

            var task = new SendQueryTask(_engine, ping, _node);
            task.Completed += delegate { _handle.Set(); };
            task.Execute();

            Assert.IsTrue(_handle.WaitOne(3000, false), "#1");
            Thread.Sleep(200);
            Assert.AreEqual(1, _counter, "#2");
            var n = _engine.RoutingTable.FindNode(_node.Id);
            Assert.IsNotNull(n, "#3");
            Assert.IsTrue(n.LastSeen > DateTime.UtcNow.AddSeconds(-2));
        }
示例#14
0
        private void SendGetPeers(Node n)
        {
            NodeId distance = n.Id.Xor(infoHash);
            queriedNodes.Add(distance, n);

            activeQueries++;
            GetPeers m = new GetPeers(engine.LocalId, infoHash);
            SendQueryTask task = new SendQueryTask(engine, m, n);
            task.Completed += GetPeersCompleted;
            task.Execute();
        }
示例#15
0
 private void SendFindNode(IEnumerable<Node> newNodes)
 {
     foreach (Node node in Node.CloserNodes(engine.LocalId, nodes, newNodes, Bucket.MaxCapacity))
     {
         activeRequests++;
         FindNode request = new FindNode(engine.LocalId, engine.LocalId);
         SendQueryTask task = new SendQueryTask(engine, request, node);
         task.Completed += FindNodeComplete;
         task.Execute();
     }
 }