From: APTX Date: Sun, 30 May 2010 10:33:58 +0000 (+0200) Subject: Should all be working just like before the rewrite. Commands with waitForResult(... X-Git-Url: https://gitweb.tyo.aptx.org/?a=commitdiff_plain;h=9cf64f614b7819c55cb377048f2ebd99b6b40a14;p=anidbudpclient.git Should all be working just like before the rewrite. Commands with waitForResult() == false are owned by the client. --- diff --git a/abstractcommand.h b/abstractcommand.h index 4d41abf..62cad54 100644 --- a/abstractcommand.h +++ b/abstractcommand.h @@ -26,29 +26,28 @@ public: virtual Command rawCommand() const; + /** + If it's false the client takes ownership of the request! + **/ virtual bool waitForResult() const; virtual bool requiresSession() const; }; +#define REPLY_DEFINITION_HELPER_INTERNAL(name, constructor) \ +friend class Client; \ +public: \ +typedef name##Command CommandType; \ +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) {constructor} \ +inline const CommandType &command() const { return m_command; } + #define REPLY_DEFINITION_HELPER(name) \ - friend class Client; \ - public: \ - typedef name##Command CommandType; \ - 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) {m_commandPtr = &m_command;} \ - inline const CommandType &command() const { return m_command; } + REPLY_DEFINITION_HELPER_INTERNAL(name, m_commandPtr = &m_command;) #define REPLY_DEFINITION_HELPER2(name) \ - friend class Client; \ - public: \ - typedef name##Command CommandType; \ - 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) {m_commandPtr = &m_command; init();} \ - inline const CommandType &command() const { return m_command; } + REPLY_DEFINITION_HELPER_INTERNAL(name, m_commandPtr = &m_command; init();) class ANIDBUDPCLIENTSHARED_EXPORT AbstractReply : public QObject { diff --git a/client.cpp b/client.cpp index 1bb5aca..e6705ad 100644 --- a/client.cpp +++ b/client.cpp @@ -27,6 +27,7 @@ Client::Client(QObject *parent) : QObject(parent) qDebug() << "Api instance init!"; authReply = 0; + uptimeReply = 0; m_error = NoError; m_idlePolicy = DoNothingIdlePolicy; @@ -129,27 +130,35 @@ qDebug() << "Api instance init!"; Client::~Client() { - disconnect(); - clearCommandQueue(); - foreach (CommandData *cmd, sentCommands) { - if (!cmd->command->command().waitForResult()) - { - // Send CLIENT_DESTROYED to indicate that no real reply will come. - cmd->command->setRawReply(CLIENT_DESTROYED, ""); - } + // Send CLIENT_DESTROYED to indicate that no real reply will come. + cmd->command->setRawReply(CLIENT_DESTROYED, ""); } + sentCommands.clear(); + + clearCommandQueue(); if (!m_sessionId.isEmpty()) { while (commandTimer->isActive()) QCoreApplication::processEvents(); - sendCommand(createReply(LogoutCommand()), true); + logout(true); socket->waitForBytesWritten(5); } + disconnect(); + if (authReply) + { + delete authReply; + authReply = 0; + } + if (uptimeReply) + { + delete uptimeReply; + uptimeReply = 0; + } } QString Client::host() const @@ -394,7 +403,7 @@ qDebug() << m_idlePolicy; idleTimer->start(UDP_API_INACTIVITY_UPDATE * 1000); break; case ImmediateLogoutIdlePolicy: - enqueueControlCommand(logoutReply); + logout(true); break; default: break; @@ -532,7 +541,7 @@ qDebug() << "COMPRESSED DATAGRAM = " << tmp; case LOGIN_FIRST: case INVALID_SESSION: qDebug() << "LOGIN FIRST required, authing"; - m_sessionId = ""; + m_sessionId.clear(); if (controlCommand) enqueueControlCommand(cmd); else @@ -541,7 +550,7 @@ qDebug() << "LOGIN FIRST required, authing"; goto continueLoop; break; case LOGGED_OUT: - m_sessionId = ""; + m_sessionId.clear(); break; case BANNED: m_error = BannedError; @@ -578,6 +587,10 @@ qDebug() << "LOGIN FIRST required, authing"; reply = reply.mid(10); cmd->setRawReply(replyCode, reply); + + // Delete if command is owned by the client. + if (!cmd->command().waitForResult()) + delete cmd; continueLoop: ; } @@ -600,7 +613,7 @@ void Client::clearCommandQueue() AbstractReply *reply = commandQueue.dequeue(); if (!reply->command().waitForResult()) { - // These would be deleted anyway + // These are owned by the client delete reply; } else @@ -615,7 +628,7 @@ void Client::clearCommandQueue() AbstractReply *reply = commandQueue.dequeue(); if (!reply->command().waitForResult()) { - // These would be deleted anyway + // These are owned by the client delete reply; } else @@ -680,8 +693,14 @@ void Client::cancel(AbstractReply *reply) void Client::logout() { - if (!m_sessionId.isEmpty()) - enqueueControlCommand(logoutReply); + logout(false); +} + +void Client::logout(bool force) +{ + if (m_sessionId.isEmpty()) + return; + enqueueControlCommand(createReply(LogoutCommand()), force); } void Client::commandTimeout(const QByteArray &commandId) diff --git a/client.h b/client.h index f836c01..1d197ff 100644 --- a/client.h +++ b/client.h @@ -173,6 +173,7 @@ private: void enqueueControlCommand(AbstractReply *command, bool first = false); void sendCommand(AbstractReply *command, bool controlCommand = false); + void logout(bool force); QByteArray buildCmd(const QString &cmd, const QVariantMap &args); QByteArray nextCommandId(int len = 5); @@ -209,7 +210,6 @@ private: AuthCommand authCommand; AuthReply *authReply; - LogoutReply *logoutReply; UptimeReply *uptimeReply; static Client *m_instance; diff --git a/file.cpp b/file.cpp index a8532ce..c8acde7 100644 --- a/file.cpp +++ b/file.cpp @@ -21,6 +21,11 @@ File::File(const QFileInfo &file, QObject *parent) : QObject(parent) File::~File() { + if (markReply) + { + delete markReply; + markReply = 0; + } if (fileReply) { delete fileReply; @@ -117,16 +122,21 @@ bool File::addToMyList() return true; } -bool File::markWatched(bool watched) +bool File::markWatched() { - if (m_markingState != Success) + if (m_markingState == Success) return true; if (m_hashingState != Success) actionsQueue.enqueue(Hashing); + if (m_addingState != Success) + actionsQueue.enqueue(Adding); + + actionsQueue.enqueue(MarkingWatched); + if (notWorking) work(); - return watched; + return true; } @@ -201,7 +211,12 @@ void File::finishAdding(bool success) void File::finishMarking(bool success) { - Q_UNUSED(success); + if (!success) + { + updateStatus(MarkingWatched, Failure); + return; + } + updateStatus(MarkingWatched, Success); } void File::work() @@ -214,6 +229,7 @@ qDebug() << actionsQueue; if (actionsQueue.isEmpty()) { emit statusUpdate(All, Finished); + emit finished(); notWorking = true; return; } @@ -333,6 +349,18 @@ void File::startMarking() work(); return; } + MyListAddCommand markCommand(0); + if (addReply->lid()) + markCommand = MyListAddCommand(addReply->lid()); + else + markCommand = MyListAddCommand(m_ed2k, size(), true); + markCommand.setViewed(true); + + if (markReply) markReply->deleteLater(); + markReply = Client::instance()->send(markCommand); + connect(markReply, SIGNAL(replyReady(bool)), this, SLOT(finishMarking(bool))); + + updateStatus(MarkingWatched, InProgress); } void File::init() @@ -340,6 +368,7 @@ void File::init() hashResult = 0; fileReply = 0; addReply = 0; + markReply = 0; m_hashingState = m_renamingState = m_addingState = m_markingState = NotStarted; notWorking = true; diff --git a/file.h b/file.h index 9118a0d..0d83544 100644 --- a/file.h +++ b/file.h @@ -73,10 +73,11 @@ public slots: void hash(); bool rename(); bool addToMyList(); - bool markWatched(bool watched = true); + bool markWatched(); signals: void statusUpdate(AniDBUdpClient::File::Action action, AniDBUdpClient::File::ActionState state); + void finished(); private slots: void finishHashing(); @@ -114,6 +115,7 @@ private: HashResult *hashResult; FileReply *fileReply; MyListAddReply *addReply; + MyListAddReply *markReply; }; diff --git a/uptimecommand.cpp b/uptimecommand.cpp index b4c05f6..f45ebaa 100644 --- a/uptimecommand.cpp +++ b/uptimecommand.cpp @@ -6,6 +6,11 @@ UptimeCommand::UptimeCommand() : AbstractCommand() { } +bool UptimeCommand::waitForResult() const +{ + return true; +} + Command UptimeCommand::rawCommand() const { Command command; diff --git a/uptimecommand.h b/uptimecommand.h index 7b3aaa2..4a01766 100644 --- a/uptimecommand.h +++ b/uptimecommand.h @@ -13,6 +13,7 @@ public: typedef UptimeReply ReplyType; UptimeCommand(); + bool waitForResult() const; Command rawCommand() const; };