Beispiel #1
0
        private PeerEndPoint[] QueryAnnounce(NodeContact[] initialContacts, BinaryNumber networkID, ushort servicePort)
        {
            NodeContact[] contacts = QueryFindNode(initialContacts, networkID);

            if ((contacts == null) || (contacts.Length == 0))
            {
                return(null);
            }

            List <PeerEndPoint> peers = new List <PeerEndPoint>();

            lock (peers)
            {
                foreach (NodeContact contact in contacts)
                {
                    Thread t = new Thread(delegate(object state)
                    {
                        DhtRpcPacket response = Query(DhtRpcPacket.CreateAnnouncePeerPacketQuery(_currentNode, networkID, servicePort), contact);
                        if ((response != null) && (response.Type == DhtRpcType.ANNOUNCE_PEER) && (response.Peers.Length > 0))
                        {
                            lock (peers)
                            {
                                foreach (PeerEndPoint peer in response.Peers)
                                {
                                    if (!peers.Contains(peer))
                                    {
                                        peers.Add(peer);
                                    }
                                }

                                Monitor.Pulse(peers);
                            }
                        }
                    });

                    t.IsBackground = true;
                    t.Start();
                }

                if (Monitor.Wait(peers, QUERY_TIMEOUT))
                {
                    return(peers.ToArray());
                }

                return(null);
            }
        }
Beispiel #2
0
        private void QueryAnnounceAsync(object state)
        {
            try
            {
                object[] parameters = state as object[];

                NodeContact         contact   = parameters[0] as NodeContact;
                BinaryID            networkID = parameters[1] as BinaryID;
                List <PeerEndPoint> peers     = parameters[2] as List <PeerEndPoint>;
                ushort servicePort            = (ushort)parameters[3];

                DhtRpcPacket responsePacket = Query(DhtRpcPacket.CreateFindPeersPacketQuery(_currentNode, networkID), contact);

                if ((responsePacket != null) && (responsePacket.QueryType == RpcQueryType.FIND_PEERS))
                {
                    if (responsePacket.Peers.Length > 0)
                    {
                        lock (peers)
                        {
                            foreach (PeerEndPoint peer in responsePacket.Peers)
                            {
                                if (!peers.Contains(peer))
                                {
                                    peers.Add(peer);
                                }
                            }

                            //Monitor.Pulse(peers); //removed so that response from multiple nodes is collected till query times out
                        }
                    }

                    Query(DhtRpcPacket.CreateAnnouncePeerPacketQuery(_currentNode, networkID, servicePort, responsePacket.Token), contact);
                }
            }
            catch
            { }
        }