public async Task<bool> Connect(string host, string port, string password = null, bool silent = false, int timeout = 0)
{
connectionproblem = false;
this.host = host;
this.port = port;
this.password = password;
if (!silent)
{
Connected = false;
Connecting = true;
}
if (_connection != null)
{
_connection.Close();
Debug.WriteLine(this.name + " : CLOSE CONNECTION");
}
_connection = new Connection();
bool success = await _connection.Connect(host, port, timeout);
Debug.WriteLine(this.name + " : NEW CONNECTION : " + success);
if (success)
{
var response = await Connection.Recive(_connection.Socket,
new List<string>() { MPDKeyWords.Response.SUCCESS_CONNECT },
new List<string>() { MPDKeyWords.Response.OK + MPDKeyWords.Response.LINEBREAK },
new List<string>() { MPDKeyWords.Response.ACK },
new List<string>() { MPDKeyWords.Response.LINEBREAK });
if (string.IsNullOrEmpty(response) || !response.StartsWith(MPDKeyWords.Response.SUCCESS_CONNECT))
success = false;
Debug.WriteLine(this.name + " : NEW CONNECTION : RESPONSE : " + success);
//if (!string.IsNullOrEmpty(password) && !await Password(password, false))
// return false;
//if (!await Password(password, false))
// return false;
if (success)
{
await Password(password, false);
if (await TestPermission() == null)
success = false;
}
Debug.WriteLine(this.name + " : NEW CONNECTION : PERMISSION : " + success);
}
if (!success)
{
Connected = false;
Connecting = false;
_connection.Close();
if (CouldNotConnect != null)
CouldNotConnect(this, new EventArgs());
}
else if (success && !silent)
{
Connected = true;
Connecting = false;
if (ConnectionConnected != null)
ConnectionConnected(this, new EventArgs());
}
Debug.WriteLine(this.name + " : NEW CONNECTION : RETURN : " + success);
return success;
}