m_replyCode = UNKNOWN_REPLY;
m_id = id;
m_client = client;
+ m_commandPtr = 0;
}
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)
return m_id;
}
+void AbstractReply::setId(const QByteArray &id)
+{
+ m_id = id;
+}
+
} // namespace AniDBUdpClient
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) \
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);
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;
};
{
qDebug() << "Api instance init!";
+ authReply = 0;
+
m_error = NoError;
m_idlePolicy = DoNothingIdlePolicy;
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();
{
connect();
- QObject::connect(command, SIGNAL(destroyed()), this, SLOT(removeDeletedFromQueue()));
+ QObject::connect(command, SIGNAL(destroyed(QObject*)), this, SLOT(removeDeletedFromQueue(QObject*)));
enqueueCommand(command);
}
void Client::cancel(AbstractReply *reply)
{
commandQueue.removeAll(reply);
- sentCommands.remove(reply->id());
+ if (reply)
+ sentCommands.remove(reply->id());
}
void Client::logout()
CommandData *cmd = sentCommands.take(commandId);
+ // Regenerate ID.
+ cmd->command->setId(nextCommandId());
+
if (cmd->controlCommand)
enqueueControlCommand(cmd->command);
else
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)
void commandTimeout(const QByteArray &commandId);
- void removeDeletedFromQueue();
+ void removeDeletedFromQueue(QObject *object);
private:
void enqueueCommand(AbstractReply *command, bool first = false);
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;