private void CheckPeers()
        {
            DateTime dtNow           = DateTime.UtcNow;
            bool     changed         = false;
            bool     hasCheckedPeers = false;

            for (int i = fPeers.Count - 1; i >= 0; i--)
            {
                var peer = fPeers[i];
                if (peer.IsLocal)
                {
                    continue;
                }

                if (peer.IsUnknown && (peer.PingTries >= 10 || dtNow - peer.LastUpdateTime > TimeSpan.FromMinutes(10)))
                {
                    fPeers.Remove(peer);
                    changed = true;
                    continue;
                }

                // always send ping
                SendPing(peer, (peer.State < PeerState.Checked));

                if (peer.State >= PeerState.Checked)
                {
                    hasCheckedPeers = true;
                }

                if (peer.State == PeerState.Unknown)
                {
                    fDHTClient.FindUnkPeer(peer);
                }

                SendData(peer.EndPoint, ProtocolHelper.CreateHandshakeQuery(DHTTransactions.GetNextId(), fDHTClient.LocalID));
            }

            if (hasCheckedPeers)
            {
                if (fConnectionState == ConnectionState.Connection)
                {
                    fConnectionState = ConnectionState.Connected;
                    changed          = true;
                }
            }
            else
            {
                if (fConnectionState == ConnectionState.Connected)
                {
                    fConnectionState = ConnectionState.Connection;
                    changed          = true;
                }
            }

            if (changed)
            {
                fForm.OnPeersListChanged();
            }
        }
        public Peer AddPeer(IPAddress peerAddress, int port)
        {
            fLogger.WriteInfo(string.Format("Found new peer: {0}", peerAddress));

            lock (fPeers) {
                Peer peer = new Peer(peerAddress, port);
                fPeers.Add(peer);
                fForm.OnPeersListChanged();
                return(peer);
            }
        }
        public CommunicatorCore(IChatForm form)
        {
            if (form == null)
            {
                throw new ArgumentNullException("form");
            }

            fConnected = false;
            fForm      = form;
            fLogger    = LogManager.GetLogger(ProtocolHelper.LOG_FILE, ProtocolHelper.LOG_LEVEL, "ChatDHTCP");
            fProfile   = new UserProfile();
            fParser    = new BencodeParser();
            fPeers     = new List <Peer>();
            fSTUNInfo  = null;

            int dhtPort = DHTClient.PublicDHTPort;

            fDHTClient             = new DHTClient(DHTClient.IPAnyAddress, dhtPort, this, CLIENT_VER);
            fDHTClient.PeersFound += delegate(object sender, PeersFoundEventArgs e) {
                fLogger.WriteInfo(string.Format("Found DHT peers: {0}", e.Peers.Count));

                bool changed = false;
                foreach (var p in e.Peers)
                {
                    changed = UpdatePeer(p);
                }

                if (changed)
                {
                    fForm.OnPeersListChanged();
                }
            };
            fDHTClient.PeerPinged += delegate(object sender, PeerPingedEventArgs e) {
                fLogger.WriteInfo(string.Format("Peer pinged: {0}", e.EndPoint));

                bool changed = CheckPeer(e.EndPoint);

                if (changed)
                {
                    fForm.OnPeersListChanged();
                    SendData(e.EndPoint, ProtocolHelper.CreateGetPeerInfoQuery());
                }
            };
            fDHTClient.QueryReceived    += OnQueryReceive;
            fDHTClient.ResponseReceived += OnResponseReceive;

            NATHolePunching();

            fTCPClient              = new TCPDuplexClient();
            fTCPClient.DataReceive += OnDataReceive;
        }
Beispiel #4
0
        private void OnPeersFound(object sender, PeersFoundEventArgs e)
        {
            bool changed    = false;
            int  newFounded = 0;

            foreach (var p in e.Peers)
            {
                changed = UpdatePeer(p);
                if (changed)
                {
                    newFounded += 1;
                }
            }

            if (changed)
            {
                fLogger.WriteDebug("Found DHT peers: {0}", newFounded);
                fForm.OnPeersListChanged();
            }
        }