Ejemplo n.º 1
0
        public void ConnectToPeer(ITransportStream peerTransport)
        {
            try
            {
                lock (peersLock)
                {
                    availablePeers.Remove(peerTransport);
                    connectingPeers.Add(peerTransport);
                }

                peerTransport.Connect().ContinueWith(antecedent =>
                {
                    // TODO: run on main loop thread
                    if (antecedent.Status != TaskStatus.RanToCompletion ||
                        !peerTransport.IsConnected)
                    {
                        Log.LogInformation($"Failed to connect to peer at {peerTransport.DisplayAddress}");

                        // Connection failed
                        lock (peersLock)
                            connectingPeers.Remove(peerTransport);

                        // TODO: keep a record of failed connection peers
                        return;
                    }

                    var connectionSettings = new PeerConnectionArgs(
                        localPeerId,
                        Metainfo,
                        messageHandlerFactory(this));

                    var peer = peerInitiator.InitiateOutgoingConnection(peerTransport, connectionSettings);
                    lock (peersLock)
                        connectingPeers.Remove(peerTransport);
                    PeerConnected(peer);
                });
            }
            catch
            {
                lock (peersLock)
                {
                    if (connectingPeers.Contains(peerTransport))
                    {
                        connectingPeers.Remove(peerTransport);
                    }
                }
            }
        }
        public void ConnectToPeer(ITransportStream peerTransport)
        {
            try
            {
                lock (_peersLock)
                {
                    _availablePeers.Remove(peerTransport);
                    _connectingPeers.Add(peerTransport);
                }

                peerTransport.Connect().ContinueWith(antecedent =>
                {
                    // TODO: run on main loop thread
                    if (antecedent.Status != TaskStatus.RanToCompletion ||
                        !peerTransport.IsConnected)
                    {
                        _logger.LogInformation($"Failed to connect to peer at {peerTransport.DisplayAddress}");

                        // Connection failed
                        lock (_peersLock)
                            _connectingPeers.Remove(peerTransport);

                        // TODO: keep a record of failed connection peers
                        return;
                    }

                    var peer = _peerInitiator.InitiateOutgoingConnection(peerTransport, this);
                    lock (_peersLock)
                        _connectingPeers.Remove(peerTransport);
                    PeerConnected(peer);
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error connecting to peer");

                lock (_peersLock)
                {
                    if (_connectingPeers.Contains(peerTransport))
                    {
                        _connectingPeers.Remove(peerTransport);
                    }
                }
            }
        }