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