Beispiel #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);
        }
Beispiel #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);
            }
        }
Beispiel #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);
            }
        }
Beispiel #4
0
 private static void UnregisterConnectionEvents(PeerConnection connection)
 {
     connection.Disconnected -= OnConnectionDisconnected;
     connection.Handshaked   -= OnConnectionHandshaked;
 }