From: APTX Date: Sat, 29 May 2010 19:09:59 +0000 (+0200) Subject: Mostly working again. X-Git-Url: https://gitweb.tyo.aptx.org/?a=commitdiff_plain;h=ebf7781217360e812c241231fee17cd82cea2e8a;p=anidbudpclient.git Mostly working again. --- diff --git a/abstractcommand.cpp b/abstractcommand.cpp index 6c2eb08..bf2d151 100644 --- a/abstractcommand.cpp +++ b/abstractcommand.cpp @@ -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 diff --git a/abstractcommand.h b/abstractcommand.h index e8b386f..4d41abf 100644 --- a/abstractcommand.h +++ b/abstractcommand.h @@ -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; }; diff --git a/client.cpp b/client.cpp index 929a879..1bb5aca 100644 --- a/client.cpp +++ b/client.cpp @@ -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(object); + if (cmd) cancel(cmd); } QByteArray Client::buildCmd(const QString &cmd, const QVariantMap &args) diff --git a/client.h b/client.h index 87aabbd..f836c01 100644 --- a/client.h +++ b/client.h @@ -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;