diff --git a/src/Connection.cpp b/src/Connection.cpp index 6f5b05d7..a22014a7 100644 --- a/src/Connection.cpp +++ b/src/Connection.cpp @@ -22,17 +22,15 @@ Connection::Connection(QTcpSocket *socket, WebPageManager *manager, QObject *par } void Connection::commandReady(Command *command) { - m_queuedCommand = command; m_manager->logger() << "Received" << command->toString(); - startCommand(); + startCommand(command); } -void Connection::startCommand() { +void Connection::startCommand(Command *command) { if (m_pageSuccess) { - m_runningCommand = new PageLoadingCommand(m_queuedCommand, m_manager, this); - m_runningCommand = new TimeoutCommand(m_runningCommand, m_manager, this); - connect(m_runningCommand, SIGNAL(finished(Response *)), this, SLOT(finishCommand(Response *))); - m_runningCommand->start(); + command = new TimeoutCommand(new PageLoadingCommand(command, m_manager, this), m_manager, this); + connect(command, SIGNAL(finished(Response *)), this, SLOT(finishCommand(Response *))); + command->start(); } else { writePageLoadFailure(); } @@ -50,7 +48,7 @@ void Connection::writePageLoadFailure() { void Connection::finishCommand(Response *response) { m_pageSuccess = true; - m_runningCommand->deleteLater(); + sender()->deleteLater(); writeResponse(response); } diff --git a/src/Connection.h b/src/Connection.h index be27e0b6..f33dd6e0 100644 --- a/src/Connection.h +++ b/src/Connection.h @@ -22,16 +22,14 @@ class Connection : public QObject { void pendingLoadFinished(bool success); private: - void startCommand(); + void startCommand(Command *); void writeResponse(Response *response); void writePageLoadFailure(); QTcpSocket *m_socket; - Command *m_queuedCommand; WebPageManager *m_manager; CommandParser *m_commandParser; CommandFactory *m_commandFactory; - Command *m_runningCommand; bool m_pageSuccess; WebPage *currentPage(); }; diff --git a/src/TimeoutCommand.cpp b/src/TimeoutCommand.cpp index 1443735f..1d501b9d 100644 --- a/src/TimeoutCommand.cpp +++ b/src/TimeoutCommand.cpp @@ -39,6 +39,9 @@ void TimeoutCommand::pendingLoadFinished(bool success) { if (success) { startCommand(); } else { + disconnect(m_timer, SIGNAL(timeout()), this, SLOT(commandTimeout())); + disconnect(m_manager, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand())); + disconnect(m_manager, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool))); emit finished(new Response(false, m_manager->currentPage()->failureString())); } } @@ -48,12 +51,18 @@ void TimeoutCommand::pageLoadingFromCommand() { } void TimeoutCommand::commandTimeout() { + disconnect(m_manager, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand())); + disconnect(m_manager, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool))); + disconnect(m_command, SIGNAL(finished(Response *)), this, SLOT(commandFinished(Response *))); m_manager->currentPage()->triggerAction(QWebPage::Stop); m_command->deleteLater(); emit finished(new Response(false, QString("timeout"))); } void TimeoutCommand::commandFinished(Response *response) { + disconnect(m_timer, SIGNAL(timeout()), this, SLOT(commandTimeout())); + disconnect(m_manager, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand())); + disconnect(m_manager, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool))); m_command->deleteLater(); emit finished(response); }