From f9a6430f726a7ac3c44f723780209cfac6c93a3e Mon Sep 17 00:00:00 2001 From: glucaci Date: Wed, 30 Nov 2016 22:46:32 +0100 Subject: [PATCH] Async Connect, events removed --- LibMpc/Mpc.cs | 44 ++++------------------------------------- LibMpc/MpcConnection.cs | 37 +++++++++++----------------------- 2 files changed, 16 insertions(+), 65 deletions(-) diff --git a/LibMpc/Mpc.cs b/LibMpc/Mpc.cs index b44009f..0928bcc 100644 --- a/LibMpc/Mpc.cs +++ b/LibMpc/Mpc.cs @@ -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 ConnectAsync(); MpdOutput[] Outputs(); MpdStatistics Stats(); @@ -33,60 +31,26 @@ namespace LibMpc _server = server; } - /// - /// Is fired when a connection to a MPD server is established. - /// - public event EventHandler Connected - { - add { ConnectedRelayEvent += value; } - remove { ConnectedRelayEvent -= value; } - } - - private event EventHandler ConnectedRelayEvent; - - /// - /// Is fired when the connection to the MPD server is closed. - /// - public event EventHandler Disconnected - { - add { DisconnectedRelayEvent += value; } - remove { DisconnectedRelayEvent -= value; } - } - - private event EventHandler DisconnectedRelayEvent; - /// /// Connection status to MPD Server. /// public bool IsConnected => _connection?.IsConnected ?? false; - public bool Connect() + public async Task 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 /// /// Disables an MPD output. diff --git a/LibMpc/MpcConnection.cs b/LibMpc/MpcConnection.cs index 2e4c7a8..2584ae0 100644 --- a/LibMpc/MpcConnection.cs +++ b/LibMpc/MpcConnection.cs @@ -6,6 +6,7 @@ using System.Net; using System.Net.Sockets; using System.Text; using System.Text.RegularExpressions; +using System.Threading.Tasks; namespace LibMpc { @@ -16,15 +17,6 @@ namespace LibMpc /// public class MpcConnection { - /// - /// Is fired when a connection to a MPD server is established. - /// - public event EventHandler Connected; - /// - /// Is fired when the connection to the MPD server is closed. - /// - public event EventHandler Disconnected; - private static readonly string FIRST_LINE_PREFIX = "OK MPD "; 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. /// /// If no IPEndPoint was set to the Server property. - public void Connect() + public async Task ConnectAsync() { if (_ipEndPoint == null) throw new InvalidOperationException("Server IPEndPoint not set."); @@ -101,43 +93,38 @@ namespace LibMpc _tcpClient = new TcpClient(); - var connection = _tcpClient.ConnectAsync(_ipEndPoint.Address, _ipEndPoint.Port); - connection.Wait(); + await _tcpClient.ConnectAsync(_ipEndPoint.Address, _ipEndPoint.Port); _networkStream = _tcpClient.GetStream(); _reader = new StreamReader(_networkStream, Encoding.UTF8); - _writer = new StreamWriter(_networkStream, Encoding.UTF8); - _writer.NewLine = "\n"; + _writer = new StreamWriter(_networkStream, Encoding.UTF8) { NewLine = "\n" }; - string firstLine = _reader.ReadLine(); - if( !firstLine.StartsWith( FIRST_LINE_PREFIX ) ) + var firstLine = _reader.ReadLine(); + if (!firstLine.StartsWith(FIRST_LINE_PREFIX)) { - Disconnect(); + await Disconnect(); throw new InvalidDataException("Response of mpd does not start with \"" + FIRST_LINE_PREFIX + "\"." ); } _version = firstLine.Substring(FIRST_LINE_PREFIX.Length); - _writer.WriteLine(); + await _writer.WriteLineAsync(); _writer.Flush(); ReadResponse(); - - Connected?.Invoke(this, EventArgs.Empty); } /// /// Disconnects from the current MPD server. /// - public void Disconnect() + public Task Disconnect() { if (_tcpClient == null) - return; + return Task.CompletedTask; _networkStream.Dispose(); - ClearConnectionFields(); - Disconnected?.Invoke(this, EventArgs.Empty); + return Task.CompletedTask; } /// /// Executes a simple command without arguments on the MPD server and returns the response. @@ -223,7 +210,7 @@ namespace LibMpc if (!IsConnected) { if (_autoConnect) - Connect(); + ConnectAsync(); else throw new NotConnectedException(); }