1
0
mirror of https://github.com/ZetaKebab/MpcNET.git synced 2025-01-14 22:18:43 +00:00

ReadResponse cleaned up and async made.

This commit is contained in:
glucaci 2016-12-01 08:31:12 +01:00
parent dd941b1f83
commit c1f763a9db

View File

@ -22,7 +22,6 @@ namespace LibMpc
private static readonly string OK = "OK"; private static readonly string OK = "OK";
private static readonly string ACK = "ACK"; private static readonly string ACK = "ACK";
private static readonly Regex ACK_REGEX = new Regex("^ACK \\[(?<code>[0-9]*)@(?<nr>[0-9]*)] \\{(?<command>[a-z]*)} (?<message>.*)$");
private readonly IPEndPoint _server; private readonly IPEndPoint _server;
@ -72,11 +71,9 @@ namespace LibMpc
await _writer.WriteLineAsync(); await _writer.WriteLineAsync();
_writer.Flush(); _writer.Flush();
ReadResponse(); await ReadResponseAsync();
} }
/// <summary>
/// Disconnects from the current MPD server.
/// </summary>
private Task Disconnect() private Task Disconnect()
{ {
if (_tcpClient == null) if (_tcpClient == null)
@ -112,7 +109,7 @@ namespace LibMpc
_writer.WriteLine(command); _writer.WriteLine(command);
_writer.Flush(); _writer.Flush();
return ReadResponse(); return await ReadResponseAsync();
} }
catch (Exception) catch (Exception)
{ {
@ -160,7 +157,7 @@ namespace LibMpc
_writer.WriteLine(); _writer.WriteLine();
_writer.Flush(); _writer.Flush();
return ReadResponse(); return await ReadResponseAsync();
} }
catch (Exception) catch (Exception)
{ {
@ -194,31 +191,25 @@ namespace LibMpc
_writer.Write(token); _writer.Write(token);
} }
private MpdResponse ReadResponse() private async Task<MpdResponse> ReadResponseAsync()
{ {
List<string> ret = new List<string>(); var response = new List<string>();
string line = _reader.ReadLine(); var currentLine = _reader.ReadLine();
while (!(line.Equals(OK) || line.StartsWith(ACK)))
// Read response untli reach end token (OK or ACK)
while (!(currentLine.Equals(OK) || currentLine.StartsWith(ACK)))
{ {
ret.Add(line); response.Add(currentLine);
line = _reader.ReadLine(); currentLine = await _reader.ReadLineAsync();
}
if (currentLine.Equals(OK))
{
return new MpdResponse(new ReadOnlyCollection<string>(response));
} }
if (line.Equals(OK))
return new MpdResponse(new ReadOnlyCollection<string>(ret));
else else
{ {
Match match = ACK_REGEX.Match(line); return AcknowledgeResponse.Parse(currentLine, response);
if (match.Groups.Count != 5)
throw new InvalidDataException( "Error response not as expected" );
return new MpdResponse(
int.Parse( match.Result("${code}") ),
int.Parse( match.Result("${nr}") ),
match.Result("${command}"),
match.Result("${message}"),
new ReadOnlyCollection<string>(ret)
);
} }
} }
@ -231,4 +222,24 @@ namespace LibMpc
_version = string.Empty; _version = string.Empty;
} }
} }
public class AcknowledgeResponse
{
private static readonly Regex AcknowledgePattern = new Regex("^ACK \\[(?<code>[0-9]*)@(?<nr>[0-9]*)] \\{(?<command>[a-z]*)} (?<message>.*)$");
public static MpdResponse Parse(string acknowledgeResponse, IList<string> payload)
{
var match = AcknowledgePattern.Match(acknowledgeResponse);
if (match.Groups.Count != 5)
throw new InvalidDataException("Error response not as expected");
return new MpdResponse(
int.Parse(match.Result("${code}")),
int.Parse(match.Result("${nr}")),
match.Result("${command}"),
match.Result("${message}"),
new ReadOnlyCollection<string>(payload));
}
}
} }