mirror of
https://github.com/ZetaKebab/MpcNET.git
synced 2025-01-14 22:18:43 +00:00
Async Connect, events removed
This commit is contained in:
parent
cd04213791
commit
f9a6430f72
@ -2,16 +2,14 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace LibMpc
|
namespace LibMpc
|
||||||
{
|
{
|
||||||
public interface IMpc
|
public interface IMpc
|
||||||
{
|
{
|
||||||
bool IsConnected { get; }
|
bool IsConnected { get; }
|
||||||
bool Connect();
|
Task<bool> ConnectAsync();
|
||||||
|
|
||||||
event EventHandler Connected;
|
|
||||||
event EventHandler Disconnected;
|
|
||||||
|
|
||||||
MpdOutput[] Outputs();
|
MpdOutput[] Outputs();
|
||||||
MpdStatistics Stats();
|
MpdStatistics Stats();
|
||||||
@ -33,60 +31,26 @@ namespace LibMpc
|
|||||||
_server = server;
|
_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>
|
/// <summary>
|
||||||
/// Connection status to MPD Server.
|
/// Connection status to MPD Server.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsConnected => _connection?.IsConnected ?? false;
|
public bool IsConnected => _connection?.IsConnected ?? false;
|
||||||
|
|
||||||
public bool Connect()
|
public async Task<bool> ConnectAsync()
|
||||||
{
|
{
|
||||||
if (_connection == null)
|
if (_connection == null)
|
||||||
{
|
{
|
||||||
_connection = new MpcConnection(_server);
|
_connection = new MpcConnection(_server);
|
||||||
_connection.Connected += OnConnected;
|
|
||||||
_connection.Disconnected += OnDisconnected;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_connection.IsConnected)
|
if (!_connection.IsConnected)
|
||||||
{
|
{
|
||||||
_connection.Connect();
|
await _connection.ConnectAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
return _connection.IsConnected;
|
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
|
#region Admin Commands
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Disables an MPD output.
|
/// Disables an MPD output.
|
||||||
|
@ -6,6 +6,7 @@ using System.Net;
|
|||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace LibMpc
|
namespace LibMpc
|
||||||
{
|
{
|
||||||
@ -16,15 +17,6 @@ namespace LibMpc
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class MpcConnection
|
public class MpcConnection
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Is fired when a connection to a MPD server is established.
|
|
||||||
/// </summary>
|
|
||||||
public event EventHandler Connected;
|
|
||||||
/// <summary>
|
|
||||||
/// Is fired when the connection to the MPD server is closed.
|
|
||||||
/// </summary>
|
|
||||||
public event EventHandler Disconnected;
|
|
||||||
|
|
||||||
private static readonly string FIRST_LINE_PREFIX = "OK MPD ";
|
private static readonly string FIRST_LINE_PREFIX = "OK MPD ";
|
||||||
|
|
||||||
private static readonly string OK = "OK";
|
private static readonly string OK = "OK";
|
||||||
@ -91,7 +83,7 @@ namespace LibMpc
|
|||||||
/// Connects to the MPD server who's IPEndPoint was set in the Server property.
|
/// Connects to the MPD server who's IPEndPoint was set in the Server property.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <exception cref="InvalidOperationException">If no IPEndPoint was set to the Server property.</exception>
|
/// <exception cref="InvalidOperationException">If no IPEndPoint was set to the Server property.</exception>
|
||||||
public void Connect()
|
public async Task ConnectAsync()
|
||||||
{
|
{
|
||||||
if (_ipEndPoint == null)
|
if (_ipEndPoint == null)
|
||||||
throw new InvalidOperationException("Server IPEndPoint not set.");
|
throw new InvalidOperationException("Server IPEndPoint not set.");
|
||||||
@ -101,43 +93,38 @@ namespace LibMpc
|
|||||||
|
|
||||||
|
|
||||||
_tcpClient = new TcpClient();
|
_tcpClient = new TcpClient();
|
||||||
var connection = _tcpClient.ConnectAsync(_ipEndPoint.Address, _ipEndPoint.Port);
|
await _tcpClient.ConnectAsync(_ipEndPoint.Address, _ipEndPoint.Port);
|
||||||
connection.Wait();
|
|
||||||
|
|
||||||
_networkStream = _tcpClient.GetStream();
|
_networkStream = _tcpClient.GetStream();
|
||||||
|
|
||||||
_reader = new StreamReader(_networkStream, Encoding.UTF8);
|
_reader = new StreamReader(_networkStream, Encoding.UTF8);
|
||||||
_writer = new StreamWriter(_networkStream, Encoding.UTF8);
|
_writer = new StreamWriter(_networkStream, Encoding.UTF8) { NewLine = "\n" };
|
||||||
_writer.NewLine = "\n";
|
|
||||||
|
|
||||||
string firstLine = _reader.ReadLine();
|
var firstLine = _reader.ReadLine();
|
||||||
if( !firstLine.StartsWith( FIRST_LINE_PREFIX ) )
|
if (!firstLine.StartsWith(FIRST_LINE_PREFIX))
|
||||||
{
|
{
|
||||||
Disconnect();
|
await Disconnect();
|
||||||
throw new InvalidDataException("Response of mpd does not start with \"" + FIRST_LINE_PREFIX + "\"." );
|
throw new InvalidDataException("Response of mpd does not start with \"" + FIRST_LINE_PREFIX + "\"." );
|
||||||
}
|
}
|
||||||
_version = firstLine.Substring(FIRST_LINE_PREFIX.Length);
|
_version = firstLine.Substring(FIRST_LINE_PREFIX.Length);
|
||||||
|
|
||||||
_writer.WriteLine();
|
await _writer.WriteLineAsync();
|
||||||
_writer.Flush();
|
_writer.Flush();
|
||||||
|
|
||||||
ReadResponse();
|
ReadResponse();
|
||||||
|
|
||||||
Connected?.Invoke(this, EventArgs.Empty);
|
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Disconnects from the current MPD server.
|
/// Disconnects from the current MPD server.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Disconnect()
|
public Task Disconnect()
|
||||||
{
|
{
|
||||||
if (_tcpClient == null)
|
if (_tcpClient == null)
|
||||||
return;
|
return Task.CompletedTask;
|
||||||
|
|
||||||
_networkStream.Dispose();
|
_networkStream.Dispose();
|
||||||
|
|
||||||
ClearConnectionFields();
|
ClearConnectionFields();
|
||||||
|
|
||||||
Disconnected?.Invoke(this, EventArgs.Empty);
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Executes a simple command without arguments on the MPD server and returns the response.
|
/// Executes a simple command without arguments on the MPD server and returns the response.
|
||||||
@ -223,7 +210,7 @@ namespace LibMpc
|
|||||||
if (!IsConnected)
|
if (!IsConnected)
|
||||||
{
|
{
|
||||||
if (_autoConnect)
|
if (_autoConnect)
|
||||||
Connect();
|
ConnectAsync();
|
||||||
else
|
else
|
||||||
throw new NotConnectedException();
|
throw new NotConnectedException();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user