QTfrontend/tcpBase.cpp
changeset 185 3b777d7008d9
parent 184 f97a7a3dc8f6
child 186 7157b34a24d6
--- a/QTfrontend/tcpBase.cpp	Thu Oct 05 17:02:09 2006 +0000
+++ b/QTfrontend/tcpBase.cpp	Thu Oct 05 18:33:10 2006 +0000
@@ -22,44 +22,47 @@
 #include <QList>
 
 #include <QImage>
-#include <QTimer>
 
 #include "hwconsts.h"
 
 QList<TCPBase*> srvsList;
+int TCPBase::isIPCServerStarted=0;
+QTcpServer* TCPBase::IPCServer(0);
 
 TCPBase::TCPBase(bool demoMode) :
-  m_isDemoMode(demoMode)
+  m_isDemoMode(demoMode),
+  IPCSocket(0)
 {
-  IPCServer = new QTcpServer(this);
+  if(!isIPCServerStarted++) {
+    IPCServer = new QTcpServer(this);
+    IPCServer->setMaxPendingConnections(1);
+    if (!IPCServer->listen(QHostAddress::LocalHost, IPC_PORT)) {
+      QMessageBox::critical(0, tr("Error"),
+			    tr("Unable to start the server: %1.")
+			    .arg(IPCServer->errorString()));
+    }
+  }
   connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
-  IPCServer->setMaxPendingConnections(1);
 }
 
 void TCPBase::NewConnection()
 {
+  if(IPCSocket) {
+    // connection should be already finished
+    return;
+  }
   QTcpSocket * client = IPCServer->nextPendingConnection();
-  if(!IPCSocket) {
-    IPCServer->close();
-    IPCSocket = client;
-    connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
-    connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
-    SendToClientFirst();
-  } else {
-    qWarning("2nd IPC client?!");
-    client->disconnectFromHost();
-  }
+  if(!client) return;
+  IPCSocket = client;
+  connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
+  connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
+  SendToClientFirst();
 }
 
 void TCPBase::RealStart()
 {
   IPCSocket = 0;
-  if (!IPCServer->listen(QHostAddress::LocalHost, IPC_PORT)) {
-    QMessageBox::critical(0, tr("Error"),
-			  tr("Unable to start the server: %1.")
-			  .arg(IPCServer->errorString()));
-  }
-
+  
   QProcess * process;
   process = new QProcess;
   connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
@@ -70,7 +73,7 @@
 void TCPBase::ClientDisconnect()
 {
   IPCSocket->close();
-  IPCServer->close();
+  //IPCServer->close();
 
   onClientDisconnect();
 
@@ -98,7 +101,7 @@
   disconnect(srvsList.front(), SIGNAL(isReadyNow()), *(++srvsList.begin()), SLOT(tcpServerReady()));
   srvsList.pop_front();
 
-  QTimer::singleShot(150, this, SLOT(RealStart()));
+  RealStart();
 }
 
 void TCPBase::Start()
@@ -110,6 +113,7 @@
     srvsList.push_back(this);
     return;
   }
+  
   RealStart();
 }