diff --git a/RedPandaIDE/problems/competitivecompenionhandler.cpp b/RedPandaIDE/problems/competitivecompenionhandler.cpp index f409afda..c9bd655e 100644 --- a/RedPandaIDE/problems/competitivecompenionhandler.cpp +++ b/RedPandaIDE/problems/competitivecompenionhandler.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include "ojproblemset.h" @@ -38,14 +39,14 @@ void CompetitiveCompanionHandler::start() if (!pSettings->executor().enableCompetitiveCompanion()) return; mThread = new CompetitiveCompanionThread(this); - if (!mThread->listen()) { - delete mThread; - mThread = nullptr; - } else { - connect(mThread, + connect(mThread, &CompetitiveCompanionThread::newProblemReceived, this, &CompetitiveCompanionHandler::onNewProblemReceived); - mThread->start(); + mThread->start(); + if (!mThread->waitStart()) { + qDebug()<<"Failed to listen!"; + delete mThread; + mThread = nullptr; } } @@ -64,12 +65,8 @@ void CompetitiveCompanionHandler::onNewProblemReceived(int num, int total, POJPr emit newProblemReceived(num, total, newProblem); } -void CompetitiveCompanionThread::onNewProblemConnection() +void CompetitiveCompanionThread::onNewProblemConnection(QTcpSocket* clientConnection) { - QTcpSocket* clientConnection = mTcpServer.nextPendingConnection(); - connect(clientConnection, &QAbstractSocket::disconnected, - clientConnection, &QObject::deleteLater); - QByteArray content; int unreadCount = 0; while (clientConnection->state() == QTcpSocket::ConnectedState) { @@ -156,10 +153,10 @@ void CompetitiveCompanionThread::onNewProblemConnection() CompetitiveCompanionThread::CompetitiveCompanionThread(QObject *parent): QThread{parent}, mStop{false}, - mBatchProblemsRecieved{0} + mBatchProblemsRecieved{0}, + mStartSemaphore{0}, + mStartOk{false} { - connect(&mTcpServer,&QTcpServer::newConnection, - this, &CompetitiveCompanionThread::onNewProblemConnection); } void CompetitiveCompanionThread::stop() @@ -167,18 +164,27 @@ void CompetitiveCompanionThread::stop() mStop = true; } -bool CompetitiveCompanionThread::listen() +bool CompetitiveCompanionThread::waitStart() { - if (mTcpServer.listen(QHostAddress::LocalHost,pSettings->executor().competivieCompanionPort())) { - return true; - } - return false; + mStartSemaphore.acquire(1); + return mStartOk; } void CompetitiveCompanionThread::run() { - while(!mStop) { - QThread::msleep(100); + QTcpServer tcpServer; + mStartOk = tcpServer.listen(QHostAddress::LocalHost,pSettings->executor().competivieCompanionPort()); + if (!mStartOk) { + mStop=true; } - mTcpServer.close(); + mStartSemaphore.release(1); + while(!mStop) { + tcpServer.waitForNewConnection(100); + while (tcpServer.hasPendingConnections()) { + QTcpSocket* clientConnection = tcpServer.nextPendingConnection(); + onNewProblemConnection(clientConnection); + delete clientConnection; + } + } + tcpServer.close(); } diff --git a/RedPandaIDE/problems/competitivecompenionhandler.h b/RedPandaIDE/problems/competitivecompenionhandler.h index abb44ddb..3ba3502e 100644 --- a/RedPandaIDE/problems/competitivecompenionhandler.h +++ b/RedPandaIDE/problems/competitivecompenionhandler.h @@ -17,9 +17,9 @@ #ifndef COMPETITIVECOMPANIONHANDLER_H #define COMPETITIVECOMPANIONHANDLER_H #include -#include #include #include +#include class OJProblem; using POJProblem = std::shared_ptr; @@ -32,22 +32,23 @@ public: CompetitiveCompanionThread(const CompetitiveCompanionThread&) = delete; CompetitiveCompanionThread &operator=(const CompetitiveCompanionThread&) = delete; void stop(); - bool listen(); + bool waitStart(); signals: void newProblemReceived(int num, int total, POJProblem newProblem); // void newBatchReceived(int total); // void batchFinished(int total); private slots: - void onNewProblemConnection(); + void onNewProblemConnection(QTcpSocket* connection); // QThread interface protected: void run() override; private: - QTcpServer mTcpServer; bool mStop; QString mBatchId; int mBatchCount; int mBatchProblemsRecieved; + QSemaphore mStartSemaphore; + bool mStartOk; };