private async Task RunQueue()
{
runQue = true;
while (runQue)
{
if (sendQueue.Count > 0)
{
if (!queueInUse)
{
queueInUse = true;
QueueJob queueJob = sendQueue[0];
int attemps = (queueJob.retry) ? 5 : 1;
int readAttemps = 2;
bool suc = false;
bool readsuc = false;
string responseString = string.Empty;
while (!suc && attemps > 0)
{
Debug.WriteLine(this.name + " : SEND : queueJob.id: " + queueJob.id + " queueJob.send: " + queueJob.send);
suc = await Connection.Send(_connection.Socket, queueJob.send);
if (suc && queueJob.wait)
{
responseString = string.Empty;
readAttemps = 3;
while (string.IsNullOrEmpty(responseString) && readAttemps > 0)
{
responseString = await Connection.Recive(_connection.Socket,
new List<string>() { MPDKeyWords.Response.SUCCESS_CONNECT },
new List<string>() { MPDKeyWords.Response.OK_LINEBREAK },
new List<string>() { MPDKeyWords.Response.ACK },
new List<string>() { MPDKeyWords.Response.LINEBREAK });
if (responseString.EndsWith(MPDKeyWords.Response.OK_LINEBREAK)
|| (responseString.StartsWith(MPDKeyWords.Response.ACK)))
{
queueJob.response = responseString;
this.responseDictionary.Add(queueJob.id, queueJob);
readsuc = true;
Debug.WriteLine(this.name + " : RECIVE : queueJob.id: " + queueJob.id + " queueJob.send: " + queueJob.send);
}
readAttemps--;
}
if (string.IsNullOrEmpty(responseString))
readsuc = false;
}
attemps--;
if ((!suc || !readsuc) && attemps > 0 && queueJob.wait)
{
Debug.WriteLine(this.name + " : RECONNECT");
await Connect(this.host, this.port, this.password, true);
suc = false;
//attemps--;
}
}
if (readsuc == false && suc == true && string.IsNullOrEmpty(responseString))
{
queueJob.response = responseString;
this.responseDictionary.Add(queueJob.id, queueJob);
Debug.WriteLine(this.name + " : EMPTY RECIVE : queueJob.id: " + queueJob.id + " queueJob.send: " + queueJob.send);
}
sendQueue.RemoveAt(0);
queueInUse = false;
if (attemps == 0 && !suc && !this.connectionproblem && !queueJob.silent)
{
this.connectionproblem = true;
if (ConnectionProblem != null)
ConnectionProblem(this, new EventArgs());
}
}
}
else runQue = false;
}
}