]> Some of my projects - anidbudpclient.git/commitdiff
Make sure sentCommandOrder and sentCommands stay in sync. Reset the replyTimeoutTimer...
authorAPTX <marek321@gmail.com>
Thu, 14 Jun 2012 11:47:11 +0000 (13:47 +0200)
committerAPTX <marek321@gmail.com>
Thu, 14 Jun 2012 11:47:11 +0000 (13:47 +0200)
client.cpp
client.h

index 9d7cf1c8b3e8cb9cf9dc3af9f97adf7fb291e81e..80c2490a74587abe16e0cba543ab56a0108e08e9 100644 (file)
@@ -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
index 417c52c44e902b650b9902b99d23e9700f9f4c52..a66b4fe6d8aa5f44d446d9d0fbc967da878ccadb 100644 (file)
--- 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;