]> Some of my projects - aniplayer-old.git/commitdiff
Mostly working again.
authorAPTX <marek321@gmail.com>
Sat, 29 May 2010 19:09:59 +0000 (21:09 +0200)
committerAPTX <marek321@gmail.com>
Sat, 29 May 2010 19:09:59 +0000 (21:09 +0200)
lib/anidbudpclient/abstractcommand.cpp
lib/anidbudpclient/abstractcommand.h
lib/anidbudpclient/client.cpp
lib/anidbudpclient/client.h

index 6c2eb08cb3a1ce11477cac845e34d82d2db15532..bf2d151bdbf04dc1182c74749cea38e0a4daa631 100644 (file)
@@ -32,6 +32,7 @@ AbstractReply::AbstractReply(const QByteArray &id, Client *client, QObject *pare
        m_replyCode = UNKNOWN_REPLY;
        m_id = id;
        m_client = client;
+       m_commandPtr = 0;
 }
 
 AbstractReply::~AbstractReply()
@@ -41,7 +42,7 @@ AbstractReply::~AbstractReply()
 
 const AbstractCommand &AbstractReply::command() const
 {
-       return m_command;
+       return m_commandPtr == 0 ? m_command : *m_commandPtr;
 }
 
 void AbstractReply::setRawReply(ReplyCode replyCode, const QString &reply)
@@ -65,4 +66,9 @@ QByteArray AbstractReply::id() const
        return m_id;
 }
 
+void AbstractReply::setId(const QByteArray &id)
+{
+       m_id = id;
+}
+
 } // namespace AniDBUdpClient
index e8b386f4e6d09359c3e83997833bf255be6b243f..4d41abfcf53cc8bce77f0d4ab8daa42cc202a669 100644 (file)
@@ -37,7 +37,7 @@ public:
        private: \
                CommandType m_command; \
                Client *m_client; \
-               name##Reply(const CommandType command, const QByteArray &id, Client *client, QObject *parent) : m_command(command), m_client(client), AbstractReply(id, client, parent) {} \
+               name##Reply(const CommandType command, const QByteArray &id, Client *client, QObject *parent) : m_command(command), m_client(client), AbstractReply(id, client, parent) {m_commandPtr = &m_command;} \
                inline const CommandType &command() const { return m_command; }
 
 #define REPLY_DEFINITION_HELPER2(name) \
@@ -47,11 +47,13 @@ public:
        private: \
                CommandType m_command; \
                Client *m_client; \
-               name##Reply(const CommandType command, const QByteArray &id, Client *client, QObject *parent) : m_command(command), m_client(client), AbstractReply(id, client, parent) {init();} \
+               name##Reply(const CommandType command, const QByteArray &id, Client *client, QObject *parent) : m_command(command), m_client(client), AbstractReply(id, client, parent) {m_commandPtr = &m_command; init();} \
                inline const CommandType &command() const { return m_command; }
 
 class ANIDBUDPCLIENTSHARED_EXPORT AbstractReply : public QObject
 {
+       friend class Client;
+
        Q_OBJECT
 
        Q_PROPERTY(QByteArray id READ id);
@@ -75,11 +77,14 @@ signals:
        void replyReady(bool success = false);
 
 protected:
+       void setId(const QByteArray &id);
+
        QString m_rawReply;
        ReplyCode m_replyCode;
        QByteArray m_id;
        Client *m_client;
 
+       AbstractCommand *m_commandPtr;
        AbstractCommand m_command;
 };
 
index 929a879638c5a8cfaed279fbedad552b4cf140c9..1bb5acaadfbdeee69b44867c8d95e58c3f2d6e67 100644 (file)
@@ -26,6 +26,8 @@ Client::Client(QObject *parent) : QObject(parent)
 {
 qDebug() << "Api instance init!";
 
+       authReply = 0;
+
        m_error = NoError;
        m_idlePolicy = DoNothingIdlePolicy;
 
@@ -308,7 +310,10 @@ qDebug() << "Entering Authenticating State";
 
        if (m_sessionId.isEmpty())
        {
-               enqueueControlCommand(createReply(authCommand), true);
+               if (authReply != 0) authReply->deleteLater();
+               authReply = createReply(authCommand);
+               QObject::connect(authReply, SIGNAL(replyReady(bool)), this, SLOT(doAuthenticate(bool)));
+               enqueueControlCommand(authReply, true);
                return;
        }
        emit authenticated();
@@ -656,7 +661,7 @@ void Client::send(AbstractReply *command)
 {
        connect();
 
-       QObject::connect(command, SIGNAL(destroyed()), this, SLOT(removeDeletedFromQueue()));
+       QObject::connect(command, SIGNAL(destroyed(QObject*)), this, SLOT(removeDeletedFromQueue(QObject*)));
        enqueueCommand(command);
 }
 
@@ -669,7 +674,8 @@ qDebug() << QString("Sending RAW command: %1").arg(command.constData());
 void Client::cancel(AbstractReply *reply)
 {
        commandQueue.removeAll(reply);
-       sentCommands.remove(reply->id());
+       if (reply)
+               sentCommands.remove(reply->id());
 }
 
 void Client::logout()
@@ -686,6 +692,9 @@ qDebug() << commandId << "timed out";
 
        CommandData *cmd = sentCommands.take(commandId);
 
+       // Regenerate ID.
+       cmd->command->setId(nextCommandId());
+
        if (cmd->controlCommand)
                enqueueControlCommand(cmd->command);
        else
@@ -763,10 +772,10 @@ qDebug() << QString("SENDING datagram:\n\t%1\nto: %2 ([%3]:%4)")
        socket->writeDatagram(datagram, m_hostAddress, m_hostPort);
 }
 
-void Client::removeDeletedFromQueue()
+void Client::removeDeletedFromQueue(QObject *object)
 {
-       AbstractReply *cmd = (AbstractReply *) sender();
-       cancel(cmd);
+       AbstractReply *cmd = qobject_cast<AbstractReply *>(object);
+       if (cmd) cancel(cmd);
 }
 
 QByteArray Client::buildCmd(const QString &cmd, const QVariantMap &args)
index 87aabbdaee51c32fedaea1cd16ff456fd1f29a5c..f836c018042d55d845edeedcd964b7e576d5b396 100644 (file)
@@ -166,7 +166,7 @@ private slots:
 
        void commandTimeout(const QByteArray &commandId);
 
-       void removeDeletedFromQueue();
+       void removeDeletedFromQueue(QObject *object);
 
 private:
        void enqueueCommand(AbstractReply *command, bool first = false);
@@ -220,7 +220,7 @@ private:
        static const int UDP_API_INACTIVITY_UPDATE = 30 * 60;
        static const int UDP_API_INACTIVITY_LOGOUT = 35 * 60;
 
-       static const int UDP_API_COMMAND_TIMEOUT = 10;
+       static const int UDP_API_COMMAND_TIMEOUT = 30;
 
        QStateMachine *stateMachine;
        QState *errorState;