From: APTX Date: Thu, 14 Jun 2012 11:47:11 +0000 (+0200) Subject: Make sure sentCommandOrder and sentCommands stay in sync. Reset the replyTimeoutTimer... X-Git-Url: https://gitweb.tyo.aptx.org/?a=commitdiff_plain;h=bb8151af2c2e995dbe0723b4a3e8eef06026c5eb;p=anidbudpclient.git Make sure sentCommandOrder and sentCommands stay in sync. Reset the replyTimeoutTimer to the next commands timeout, rather than the current ones. --- diff --git a/client.cpp b/client.cpp index 9d7cf1c..80c2490 100644 --- a/client.cpp +++ b/client.cpp @@ -695,10 +695,7 @@ qDebug() << QString("Sending reply to command with id: %1") else { // If we got the first command sent, we update the replyTimeoutTimer to count the remaining time untill timeout. - // This should be less than Client::UDP_API_COMMAND_TIMEOUT, but never negative. - int newTimeout = qBound(0, Client::UDP_API_COMMAND_TIMEOUT - sentCommands[commandId]->timeSent().secsTo(QDateTime::currentDateTime()), Client::UDP_API_COMMAND_TIMEOUT); - newTimeout *= 1000; - replyTimeoutTimer->start(newTimeout); + resetReplyTimeoutTimer(sentCommands[sentCommandOrder.head()]); #ifdef ANIDBUDPCLIENT_CLIENT_MISC_DEBUG qDebug() << "Starting replyTimeoutTimer" << newTimeout; #endif @@ -912,8 +909,22 @@ qDebug() << QString("Sending RAW command: %1").arg(command.constData()); void Client::cancel(AbstractReply *reply) { commandQueue.removeAll(reply); - if (reply) - sentCommands.remove(reply->id()); + if (!reply) + return; + + int index = 0; + if (sentCommandOrder.head() == reply->id()) + { + resetReplyTimeoutTimer(sentCommands[sentCommandOrder.head()]); + emit model_sentCommandRemoved(0); + } + else + { + index = sentCommandOrder.indexOf(reply->id()); + emit model_sentCommandRemoved(index); + } + sentCommandOrder.removeAt(index); + sentCommands.remove(reply->id()); } void Client::clearErrors() @@ -1050,6 +1061,14 @@ bool Client::possiblyValid(const QByteArray &reply) const return false; } +void Client::resetReplyTimeoutTimer(AbstractReply *cmd) +{ + Q_ASSERT(cmd); + int newTimeout = qBound(0, Client::UDP_API_COMMAND_TIMEOUT - cmd->timeSent().secsTo(QDateTime::currentDateTime()), Client::UDP_API_COMMAND_TIMEOUT); + newTimeout *= 1000; + replyTimeoutTimer->start(newTimeout); +} + void Client::init() { static bool done = false; @@ -1072,9 +1091,7 @@ qDebug() << commandId << "timed out"; #endif if (!sentCommandOrder.isEmpty()) { - int newTimeout = qBound(0, Client::UDP_API_COMMAND_TIMEOUT - sentCommands[commandId]->timeSent().secsTo(QDateTime::currentDateTime()), Client::UDP_API_COMMAND_TIMEOUT); - newTimeout *= 1000; - replyTimeoutTimer->start(newTimeout); + resetReplyTimeoutTimer(sentCommands[sentCommandOrder.head()]); #ifdef ANIDBUDPCLIENT_CLIENT_MISC_DEBUG qDebug() << "Starting replyTimeoutTimer" << newTimeout; #endif diff --git a/client.h b/client.h index 417c52c..a66b4fe 100644 --- a/client.h +++ b/client.h @@ -173,6 +173,8 @@ private: bool possiblyValid(const QByteArray &reply) const; + void resetReplyTimeoutTimer(AbstractReply *cmd); + QTimer *commandTimer; QTimer *idleTimer; QTimer *replyTimeoutTimer;