- Only allow one engine instance running at the moment
authorunc0rr
Mon, 19 Nov 2012 23:43:45 +0400
changeset 8069 bb7671829935
parent 8068 b35427506169
child 8070 66bc20d089fc
- Only allow one engine instance running at the moment - When adding new map preview call to the queue, check if previous map preview request has started, and if no, remove it from queue. Could be harmful if you manage to request previews for two widgets in less time than needed to generate one.
QTfrontend/game.cpp
QTfrontend/net/hwmap.cpp
QTfrontend/net/hwmap.h
QTfrontend/net/recorder.cpp
QTfrontend/net/tcpBase.cpp
QTfrontend/net/tcpBase.h
--- a/QTfrontend/game.cpp	Mon Nov 19 13:21:20 2012 +0100
+++ b/QTfrontend/game.cpp	Mon Nov 19 23:43:45 2012 +0400
@@ -350,7 +350,7 @@
 
     // run engine
     demo.clear();
-    Start();
+    Start(false);
     SetGameState(gsStarted);
 }
 
@@ -358,7 +358,7 @@
 {
     gameType = gtNet;
     demo.clear();
-    Start();
+    Start(false);
     SetGameState(gsStarted);
 }
 
@@ -366,7 +366,7 @@
 {
     gameType = gtLocal;
     demo.clear();
-    Start();
+    Start(false);
     SetGameState(gsStarted);
 }
 
@@ -374,7 +374,7 @@
 {
     gameType = gtQLocal;
     demo.clear();
-    Start();
+    Start(false);
     SetGameState(gsStarted);
 }
 
@@ -383,7 +383,7 @@
     gameType = gtTraining;
     training = "Missions/Training/" + file + ".lua";
     demo.clear();
-    Start();
+    Start(false);
     SetGameState(gsStarted);
 }
 
@@ -394,7 +394,7 @@
     campaignScript = "Missions/Campaign/" + camp + "/" + campScript;
     campaignTeam = campTeam;
     demo.clear();
-    Start();
+    Start(false);
     SetGameState(gsStarted);
 }
 
--- a/QTfrontend/net/hwmap.cpp	Mon Nov 19 13:21:20 2012 +0100
+++ b/QTfrontend/net/hwmap.cpp	Mon Nov 19 23:43:45 2012 +0400
@@ -29,6 +29,11 @@
 {
 }
 
+bool HWMap::couldBeRemoved()
+{
+    return !m_hasStarted;
+}
+
 void HWMap::getImage(const QString & seed, int filter, MapGenerator mapgen, int maze_size, const QByteArray & drawMapData)
 {
     m_seed = seed;
@@ -36,7 +41,7 @@
     m_mapgen = mapgen;
     m_maze_size = maze_size;
     if(mapgen == MAPGEN_DRAWN) m_drawMapData = drawMapData;
-    Start();
+    Start(true);
 }
 
 QStringList HWMap::getArguments()
--- a/QTfrontend/net/hwmap.h	Mon Nov 19 13:21:20 2012 +0100
+++ b/QTfrontend/net/hwmap.h	Mon Nov 19 23:43:45 2012 +0400
@@ -42,6 +42,7 @@
         HWMap();
         virtual ~HWMap();
         void getImage(const QString & seed, int templateFilter, MapGenerator mapgen, int maze_size, const QByteArray & drawMapData);
+        bool couldBeRemoved();
 
     protected:
         virtual QStringList getArguments();
--- a/QTfrontend/net/recorder.cpp	Mon Nov 19 13:21:20 2012 +0100
+++ b/QTfrontend/net/recorder.cpp	Mon Nov 19 23:43:45 2012 +0400
@@ -47,7 +47,7 @@
     if (queue.empty())
         numRecorders--;
     else
-        queue.takeFirst()->Start();
+        queue.takeFirst()->Start(false);
 }
 
 void HWRecorder::onClientDisconnect()
@@ -89,7 +89,7 @@
     if (numRecorders < maxRecorders)
     {
         numRecorders++;
-        Start(); // run engine
+        Start(false); // run engine
     }
     else
         queue.push_back(this);
--- a/QTfrontend/net/tcpBase.cpp	Mon Nov 19 13:21:20 2012 +0100
+++ b/QTfrontend/net/tcpBase.cpp	Mon Nov 19 23:43:45 2012 +0400
@@ -31,11 +31,15 @@
 
 TCPBase::~TCPBase()
 {
+    // make sure this object is not in the server list anymore
+    srvsList.removeOne(this);
+
     if (IPCSocket)
         IPCSocket->deleteLater();
 }
 
 TCPBase::TCPBase(bool demoMode) :
+    m_hasStarted(false),
     m_isDemoMode(demoMode),
     IPCSocket(0)
 {
@@ -71,9 +75,6 @@
     connect(IPCSocket, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
     connect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
     SendToClientFirst();
-
-    if(srvsList.size()==1) srvsList.pop_front();
-    emit isReadyNow();
 }
 
 void TCPBase::RealStart()
@@ -82,14 +83,17 @@
     IPCSocket = 0;
 
     QProcess * process;
-    process = new QProcess;
+    process = new QProcess();
     connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
     QStringList arguments=getArguments();
 
     // redirect everything written on stdout/stderr
     if(isDevBuild)
         process->setProcessChannelMode(QProcess::ForwardedChannels);
+
     process->start(bindir->absolutePath() + "/hwengine", arguments);
+
+    m_hasStarted = true;
 }
 
 void TCPBase::ClientDisconnect()
@@ -97,13 +101,9 @@
     disconnect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
     onClientDisconnect();
 
- /*   if(srvsList.size()==1) srvsList.pop_front();
-    emit isReadyNow();*/
+    emit isReadyNow();
     IPCSocket->deleteLater();
 
-    // make sure this object is not in the server list anymore
-    srvsList.removeOne(this);
-
     deleteLater();
 }
 
@@ -130,25 +130,31 @@
 
 void TCPBase::tcpServerReady()
 {
-    disconnect(srvsList.takeFirst(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
+    disconnect(srvsList.first(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
 
     RealStart();
 }
 
-void TCPBase::Start()
+void TCPBase::Start(bool couldCancelPreviousRequest)
 {
     if(srvsList.isEmpty())
     {
         srvsList.push_back(this);
+        RealStart();
     }
     else
     {
-        connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
-        srvsList.push_back(this);
-        return;
+        if(couldCancelPreviousRequest && srvsList.last()->couldBeRemoved())
+        {
+            TCPBase * last = srvsList.takeLast();
+            last->deleteLater();
+            Start(couldCancelPreviousRequest);
+        } else
+        {
+            connect(srvsList.last(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
+            srvsList.push_back(this);
+        }
     }
-
-    RealStart();
 }
 
 void TCPBase::onClientRead()
@@ -191,3 +197,8 @@
         }
     }
 }
+
+bool TCPBase::couldBeRemoved()
+{
+    return false;
+}
--- a/QTfrontend/net/tcpBase.h	Mon Nov 19 13:21:20 2012 +0100
+++ b/QTfrontend/net/tcpBase.h	Mon Nov 19 23:43:45 2012 +0400
@@ -41,13 +41,16 @@
         TCPBase(bool demoMode);
         virtual ~TCPBase();
 
+        virtual bool couldBeRemoved();
+
     signals:
         void isReadyNow();
 
     protected:
+        bool m_hasStarted;
         quint16 ipc_port;
 
-        void Start();
+        void Start(bool couldCancelPreviousRequest);
 
         QByteArray readbuffer;