コード例 #1
0
 private void dhtSeedingTracker_DiscoveredPeers(TrackerManager sender, IEnumerable <IPEndPoint> peerEPs)
 {
     if (_dhtClient != null)
     {
         _dhtClient.AddNode(peerEPs);
     }
 }
コード例 #2
0
        private void UpdateTrackerAsync(object state)
        {
            object[] parameters = state as object[];

            TrackerClient      tracker = parameters[0] as TrackerClient;
            TrackerClientEvent @event  = (TrackerClientEvent)parameters[1];
            IPEndPoint         localEP = parameters[2] as IPEndPoint;

            try
            {
                tracker.Update(@event, localEP);

                switch (@event)
                {
                case TrackerClientEvent.Started:
                case TrackerClientEvent.Completed:
                case TrackerClientEvent.None:
                    if (_lookupOnly)
                    {
                        tracker.Update(TrackerClientEvent.Stopped, localEP);
                    }

                    if (tracker.Peers.Count > 0)
                    {
                        DiscoveredPeers?.Invoke(this, tracker.Peers);

                        if (_dhtClient != null)
                        {
                            _dhtClient.AddNode(tracker.Peers);
                        }
                    }

                    break;
                }

                tracker.CustomUpdateInterval = _customUpdateInterval;
            }
            catch
            {
                if (tracker.RetriesDone < TRACKER_MAX_RETRIES)
                {
                    tracker.CustomUpdateInterval = TRACKER_RETRY_UPDATE_INTERVAL_SECONDS;
                }
                else
                {
                    tracker.CustomUpdateInterval = TRACKER_FAILED_UPDATE_INTERVAL_SECONDS;
                }
            }
        }
コード例 #3
0
        public ConnectionManager(BitChatProfile profile)
        {
            IPEndPoint localEP;

            switch (Environment.OSVersion.Platform)
            {
            case PlatformID.Win32NT:
                if (Environment.OSVersion.Version.Major < 6)
                {
                    //below vista
                    _tcpListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    localEP      = new IPEndPoint(IPAddress.Any, profile.LocalPort);
                }
                else
                {
                    //vista & above
                    _tcpListener = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
                    _tcpListener.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
                    localEP = new IPEndPoint(IPAddress.IPv6Any, profile.LocalPort);
                }
                break;

            case PlatformID.Unix:     //mono framework
                if (Socket.OSSupportsIPv6)
                {
                    _tcpListener = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
                    localEP      = new IPEndPoint(IPAddress.IPv6Any, profile.LocalPort);
                }
                else
                {
                    _tcpListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    localEP      = new IPEndPoint(IPAddress.Any, profile.LocalPort);
                }

                break;

            default:     //unknown
                _tcpListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                localEP      = new IPEndPoint(IPAddress.Any, profile.LocalPort);
                break;
            }

            try
            {
                _tcpListener.Bind(localEP);
                _tcpListener.Listen(10);
            }
            catch
            {
                localEP.Port = 0;

                _tcpListener.Bind(localEP);
                _tcpListener.Listen(10);
            }

            _profile = profile;

            _localPort   = (_tcpListener.LocalEndPoint as IPEndPoint).Port;
            _localPeerID = BinaryID.GenerateRandomID160();

            //start dht
            _dhtClient = new DhtClient(_localPort, this);
            _dhtClient.ProxyEnabled = (_profile.Proxy != null);
            _dhtClient.AddNode(profile.BootstrapDhtNodes);

            //setup dht seeding tracker
            _dhtSeedingTracker                  = new TrackerManager(_dhtSeedingNetworkID, _localPort, null, DHT_SEED_TRACKER_UPDATE_INTERVAL);
            _dhtSeedingTracker.Proxy            = _profile.Proxy;
            _dhtSeedingTracker.DiscoveredPeers += dhtSeedingTracker_DiscoveredPeers;
            _dhtSeedingTracker.StartTracking(profile.TrackerURIs);

            //start accepting connections
            _tcpListenerThread = new Thread(AcceptTcpConnectionAsync);
            _tcpListenerThread.IsBackground = true;
            _tcpListenerThread.Start(_tcpListener);

            //start upnp process
            _connectivityCheckTimer = new Timer(ConnectivityCheckTimerCallback, null, 1000, Timeout.Infinite);
        }