1
0
mirror of https://github.com/ZetaKebab/MpcNET.git synced 2025-07-02 00:57:38 +00:00

Async Connect, events removed

This commit is contained in:
glucaci
2016-11-30 22:46:32 +01:00
parent cd04213791
commit f9a6430f72
2 changed files with 16 additions and 65 deletions

View File

@ -2,16 +2,14 @@ using System;
using System.Collections.Generic;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace LibMpc
{
public interface IMpc
{
bool IsConnected { get; }
bool Connect();
event EventHandler Connected;
event EventHandler Disconnected;
Task<bool> ConnectAsync();
MpdOutput[] Outputs();
MpdStatistics Stats();
@ -33,60 +31,26 @@ namespace LibMpc
_server = server;
}
/// <summary>
/// Is fired when a connection to a MPD server is established.
/// </summary>
public event EventHandler Connected
{
add { ConnectedRelayEvent += value; }
remove { ConnectedRelayEvent -= value; }
}
private event EventHandler ConnectedRelayEvent;
/// <summary>
/// Is fired when the connection to the MPD server is closed.
/// </summary>
public event EventHandler Disconnected
{
add { DisconnectedRelayEvent += value; }
remove { DisconnectedRelayEvent -= value; }
}
private event EventHandler DisconnectedRelayEvent;
/// <summary>
/// Connection status to MPD Server.
/// </summary>
public bool IsConnected => _connection?.IsConnected ?? false;
public bool Connect()
public async Task<bool> ConnectAsync()
{
if (_connection == null)
{
_connection = new MpcConnection(_server);
_connection.Connected += OnConnected;
_connection.Disconnected += OnDisconnected;
}
if (!_connection.IsConnected)
{
_connection.Connect();
await _connection.ConnectAsync();
}
return _connection.IsConnected;
}
private void OnConnected(object sender, EventArgs e)
{
ConnectedRelayEvent?.Invoke(this, e);
}
private void OnDisconnected(object sender, EventArgs e)
{
DisconnectedRelayEvent?.Invoke(this, e);
}
#region Admin Commands
/// <summary>
/// Disables an MPD output.