Exemplo n.º 1
0
        private void OnConnectionDisconnected(object sender, EventArgs e)
        {
            Log.LogInfo("[Peer] Disconnected from {0}", endPoint);

            if (connection != null)
            {
                UninitializeConnection(connection);
                connection.Dispose();
                connection = null;
            }

            if (torrent != null)
            {
                torrent.OnPeerDisconnected(this);
            }

            Disconnected.SafeInvoke(this, e);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Attempts to connect to this peer synchronously.
        /// </summary>
        /// <returns>If the connection was successfully established.</returns>
        public bool Connect()
        {
            if (connection != null)
            {
                return(false);
            }

            try
            {
                connection = new PeerConnectionTCP(torrent, this, endPoint);
                InitializeConnection(connection);
                connection.Connect();
                return(connection.IsConnected);
            }
            catch
            {
                return(false);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Attempts to connect to this peer asynchronously.
        /// </summary>
        /// <returns>If the connection was successfully established.</returns>
        public async Task <bool> ConnectAsync()
        {
            if (connection != null)
            {
                return(false);
            }

            try
            {
                connection = new PeerConnectionTCP(torrent, this, endPoint);
                InitializeConnection(connection);
                await connection.ConnectAsync();

                return(connection.IsConnected);
            }
            catch
            {
                return(false);
            }
        }
Exemplo n.º 4
0
 private static void UnregisterConnectionEvents(PeerConnection connection)
 {
     connection.Disconnected -= OnConnectionDisconnected;
     connection.Handshaked   -= OnConnectionHandshaked;
 }