Example #1
0
        public bool UpdatePeer(IPEndPoint peerEndPoint)
        {
            bool result = false;

            if (peerEndPoint == null)
            {
                return(false);
            }

            var  peerAddress = peerEndPoint.Address;
            Peer peer        = FindPeer(peerAddress);

            if (peer == null)
            {
                peer   = AddPeer(peerAddress, peerEndPoint.Port);
                result = true;
            }

            // TODO: it's bad place for peers' ping!
            // FIXME: find out which ping gets the answer! check it for a long time testing!
            //fDHTClient.SendPingQuery(peerEndPoint);
            fDHTClient.SendPingQuery(new IPEndPoint(peerAddress, fDHTClient.LocalEndPoint.Port));

            return(result);
        }
        /// <summary>
        /// Many thanks to Konstantinos Gkinis <aka raidenfreeman> for project https://github.com/raidenfreeman/ICE-Experiment,
        /// who explained in a simple way how to implement UDP Hole punching.
        /// </summary>
        private void SendPing(Peer peer, bool holePunch)
        {
            DateTime dtNow = DateTime.UtcNow;

            if (dtNow - peer.LastPingTime > PingInterval)
            {
                if (!holePunch)
                {
                    fDHTClient.SendPingQuery(peer.EndPoint, true);
                }
                else
                {
                    for (int i = 0; i < 10; i++)
                    {
                        fDHTClient.SendPingQuery(peer.EndPoint, false);
                    }
                }

                peer.PingTries   += 1;
                peer.LastPingTime = dtNow;
            }
        }