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(); } }
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(); } }
private void EndCreateConnection(bool succeeded, int count, object state) { AsyncConnectState connect = (AsyncConnectState)state; pendingConnects.Remove(connect); if (connect.Manager.Engine == null || !connect.Manager.Mode.CanAcceptConnections) { connect.Connection.Dispose(); return; } try { connect.Manager.Peers.ConnectingToPeers.Remove(connect.Peer); if (!succeeded) { logger.Error("Failed to connect {0}", connect.Peer); connect.Manager.RaiseConnectionAttemptFailed( new PeerConnectionFailedEventArgs(connect.Manager, connect.Peer, Direction.Outgoing, "EndCreateConnection")); connect.Peer.FailedConnectionAttempts++; connect.Connection.Dispose(); connect.Manager.Peers.BusyPeers.Add(connect.Peer); } else { PeerId id = new PeerId(connect.Peer, connect.Manager); id.Connection = connect.Connection; connect.Manager.Peers.ActivePeers.Add(connect.Peer); logger.Info("Connection opened {0}", connect.Peer); ProcessFreshConnection(id); } } catch (Exception x) { logger.Error("Connect create error: {0}", x.Message); } finally { // Try to connect to another peer TryConnect(); } }
internal void ConnectToPeer(TorrentManager manager, Peer peer) { // Connect to the peer. IConnection connection = ConnectionFactory.Create(peer.ConnectionUri); if (connection == null) { return; } peer.LastConnectionAttempt = DateTime.Now; AsyncConnectState c = new AsyncConnectState(manager, peer, connection); pendingConnects.Add(c); manager.Peers.ConnectingToPeers.Add(peer); NetworkIO.EnqueueConnect(connection, endCreateConnectionCallback, c); }
internal void ConnectToPeer(TorrentManager manager, Peer peer) { // Connect to the peer. IConnection connection = ConnectionFactory.Create(peer.ConnectionUri); if (connection == null) return; logger.Info("Connecting to {0}", peer); peer.LastConnectionAttempt = DateTime.Now; AsyncConnectState c = new AsyncConnectState(manager, peer, connection); pendingConnects.Add(c); manager.Peers.ConnectingToPeers.Add(peer); NetworkIO.EnqueueConnect(connection, endCreateConnectionCallback, c); }
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(); } }
internal void ConnectToPeer(TorrentManager manager, Peer peer) { // Connect to the peer. IConnection connection = ConnectionFactory.Create(peer.ConnectionUri); if (connection == null) return; peer.LastConnectionAttempt = DateTime.Now; AsyncConnectState c = new AsyncConnectState(manager, peer, connection, endCreateConnectionCallback); manager.Peers.ConnectingToPeers.Add(peer); NetworkIO.EnqueueConnect(c); // Try to connect to another peer TryConnect(); }
internal static void EnqueueConnect(AsyncConnectState c) { Interlocked.Increment(ref halfOpens); try { c.Result = c.Connection.BeginConnect(EndConnect, c); ClientEngine.MainLoop.QueueTimeout(TimeSpan.FromSeconds(10), delegate { if (!c.Result.IsCompleted) c.Connection.Dispose(); return false; }); } catch { c.Callback(false, c); } }