Ejemplo n.º 1
0
        internal async void ConnectToPeer(TorrentManager manager, Peer peer)
        {
            // Connect to the peer.
            IConnection connection = ConnectionFactory.Create(peer.ConnectionUri);

            if (connection == null)
            {
                return;
            }

            var state = new AsyncConnectState(manager, connection, ValueStopwatch.StartNew());

            PendingConnects.Add(state);
            manager.Peers.ConnectingToPeers.Add(peer);

            bool succeeded;

            try {
                await NetworkIO.ConnectAsync(connection);

                succeeded = true;
            } catch {
                succeeded = false;
            }

            PendingConnects.Remove(state);
            manager.Peers.ConnectingToPeers.Remove(peer);
            if (manager.Engine == null || !manager.Mode.CanAcceptConnections)
            {
                manager.Peers.AvailablePeers.Add(peer);
                connection.Dispose();
                return;
            }

            try {
                if (!succeeded)
                {
                    peer.FailedConnectionAttempts++;
                    connection.Dispose();
                    manager.Peers.BusyPeers.Add(peer);
                    manager.RaiseConnectionAttemptFailed(new ConnectionAttemptFailedEventArgs(peer, ConnectionFailureReason.Unreachable, manager));
                }
                else
                {
                    PeerId id = new PeerId(peer, connection, manager.Bitfield?.Clone().SetAll(false));
                    id.LastMessageReceived.Restart();
                    id.LastMessageSent.Restart();

                    Logger.Log(id.Connection, "ConnectionManager - Connection opened");

                    ProcessNewOutgoingConnection(manager, id);
                }
            } catch {
                // FIXME: Do nothing now?
            } finally {
                // Try to connect to another peer
                TryConnect();
            }
        }
Ejemplo n.º 2
0
        async void ConnectToPeer(TorrentManager manager, Peer peer)
        {
            // Connect to the peer.
            var connection = Factories.CreatePeerConnection(peer.ConnectionUri);

            if (connection == null || peer.AllowedEncryption.Count == 0)
            {
                return;
            }

            var state = new AsyncConnectState(manager, connection, ValueStopwatch.StartNew());

            PendingConnects.Add(state);
            manager.Peers.ConnectingToPeers.Add(peer);

            bool succeeded;

            try {
                await NetworkIO.ConnectAsync(connection);

                succeeded = true;
            } catch {
                succeeded = false;
            }

            PendingConnects.Remove(state);
            manager.Peers.ConnectingToPeers.Remove(peer);
            if (manager.Disposed || !manager.Mode.CanAcceptConnections)
            {
                manager.Peers.AvailablePeers.Add(peer);
                connection.Dispose();
                return;
            }

            try {
                if (!succeeded)
                {
                    peer.FailedConnectionAttempts++;
                    connection.Dispose();
                    manager.RaiseConnectionAttemptFailed(new ConnectionAttemptFailedEventArgs(peer, ConnectionFailureReason.Unreachable, manager));
                }
                else
                {
                    var id = new PeerId(peer, connection, new MutableBitField(manager.Bitfield.Length).SetAll(false));
                    id.LastMessageReceived.Restart();
                    id.LastMessageSent.Restart();

                    logger.Info(id.Connection, "Connection opened");

                    ProcessNewOutgoingConnection(manager, id);
                }
            } catch {
                // FIXME: Do nothing now?
            } finally {
                // Try to connect to another peer
                TryConnect();
            }
        }
Ejemplo n.º 3
0
        internal async void ConnectToPeer(TorrentManager manager, Peer peer)
        {
            // Connect to the peer.
            IConnection connection = ConnectionFactory.Create(peer.ConnectionUri);

            if (connection == null)
            {
                return;
            }

            var state = new AsyncConnectState(manager, connection, Stopwatch.StartNew());

            PendingConnects.Add(state);
            manager.Peers.ConnectingToPeers.Add(peer);

            bool succeeded;

            try {
                await NetworkIO.ConnectAsync(connection);

                succeeded = true;
            } catch {
                succeeded = false;
            }

            PendingConnects.Remove(state);
            if (manager.Engine == null ||
                !manager.Mode.CanAcceptConnections)
            {
                connection.Dispose();
                return;
            }

            try {
                manager.Peers.ConnectingToPeers.Remove(peer);
                if (!succeeded)
                {
                    Logger.Log(null, "ConnectionManager - Failed to connect{0}", peer);

                    manager.RaiseConnectionAttemptFailed(
                        new PeerConnectionFailedEventArgs(manager, peer, Direction.Outgoing, "EndCreateConnection"));

                    peer.FailedConnectionAttempts++;
                    connection.Dispose();
                    manager.Peers.BusyPeers.Add(peer);
                }
                else
                {
                    PeerId id = new PeerId(peer, manager);
                    id.Connection = connection;
                    manager.Peers.ActivePeers.Add(peer);

                    Logger.Log(id.Connection, "ConnectionManager - Connection opened");

                    ProcessFreshConnection(id);
                }
            } catch (Exception) {
                // FIXME: Do nothing now?
            } finally {
                // Try to connect to another peer
                TryConnect();
            }
        }