QTfrontend/net/tcpBase.cpp
branchflibqtfrontend
changeset 8092 08960209db8c
parent 8082 675372256a01
child 8363 0b4ac686fc44
--- a/QTfrontend/net/tcpBase.cpp	Thu Nov 22 00:56:36 2012 +0400
+++ b/QTfrontend/net/tcpBase.cpp	Thu Nov 22 01:19:16 2012 +0400
@@ -57,74 +57,32 @@
 #endif
 
 QList<TCPBase*> srvsList;
-QPointer<QTcpServer> TCPBase::IPCServer(0);
 
 TCPBase::~TCPBase()
 {
     // make sure this object is not in the server list anymore
     srvsList.removeOne(this);
-
-    if (IPCSocket)
-        IPCSocket->deleteLater();
 }
 
-TCPBase::TCPBase(bool demoMode, QObject *parent) :
+TCPBase::TCPBase(QObject *parent) :
     QObject(parent),
-    m_hasStarted(false),
-    m_isDemoMode(demoMode),
-    IPCSocket(0)
+    m_hasStarted(false)
 {
-    if(!IPCServer)
-    {
-        IPCServer = new QTcpServer(0);
-        IPCServer->setMaxPendingConnections(1);
-        if (!IPCServer->listen(QHostAddress::LocalHost))
-        {
-            QMessageBox deniedMsg(QApplication::activeWindow());
-            deniedMsg.setIcon(QMessageBox::Critical);
-            deniedMsg.setWindowTitle(QMessageBox::tr("TCP - Error"));
-            deniedMsg.setText(QMessageBox::tr("Unable to start the server: %1.").arg(IPCServer->errorString()));
-            deniedMsg.setWindowModality(Qt::WindowModal);
-            deniedMsg.exec();
 
-            exit(0); // FIXME - should be graceful exit here (lower Critical -> Warning above when implemented)
-        }
-    }
-#ifdef HWLIBRARY
-    ipc_port=65000; //HACK
-#else
-    ipc_port=IPCServer->serverPort();
-#endif
-}
-
-void TCPBase::NewConnection()
-{
-    if(IPCSocket)
-    {
-        // connection should be already finished
-        return;
-    }
-    disconnect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
-    IPCSocket = IPCServer->nextPendingConnection();
-    if(!IPCSocket) return;
-    connect(IPCSocket, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
-    connect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
-    SendToClientFirst();
 }
 
 void TCPBase::RealStart()
 {
-    connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
-    IPCSocket = 0;
+    onEngineStart();
 
 #ifdef HWLIBRARY
     EngineThread engineThread;// = new EngineThread(this);
     engineThread.start();
 #else
     QProcess * process;
+
     process = new QProcess();
-    connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
-    QStringList arguments=getArguments();
+    QStringList arguments = getArguments();
 
     // redirect everything written on stdout/stderr
     if(isDevBuild)
@@ -135,50 +93,26 @@
     m_hasStarted = true;
 }
 
-void TCPBase::ClientDisconnect()
+void TCPBase::clientDisconnected()
 {
-    disconnect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
-    onClientDisconnect();
-
-    emit isReadyNow();
-    IPCSocket->deleteLater();
+    emit nextPlease();
 
     deleteLater();
 }
 
-void TCPBase::ClientRead()
-{
-    QByteArray readed=IPCSocket->readAll();
-    if(readed.isEmpty()) return;
-    readbuffer.append(readed);
-    onClientRead();
-}
-
-void TCPBase::StartProcessError(QProcess::ProcessError error)
+void TCPBase::iStart()
 {
-    QMessageBox deniedMsg(QApplication::activeWindow());
-    deniedMsg.setIcon(QMessageBox::Critical);
-    deniedMsg.setWindowTitle(QMessageBox::tr("TCP - Error"));
-    deniedMsg.setText(QMessageBox::tr("Unable to run engine at ") + bindir->absolutePath() + "/hwengine\n" +
-                      QMessageBox::tr("Error code: %1").arg(error));
-    deniedMsg.setWindowModality(Qt::WindowModal);
-    deniedMsg.exec();
-
-    ClientDisconnect();
-}
-
-void TCPBase::tcpServerReady()
-{
-    disconnect(srvsList.first(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
+    disconnect(srvsList.first(), SIGNAL(nextPlease()), this, SLOT(iStart()));
 
     RealStart();
 }
 
-void TCPBase::Start(bool couldCancelPreviousRequest)
+void TCPBase::start(bool couldCancelPreviousRequest)
 {
     if(srvsList.isEmpty())
     {
         srvsList.push_back(this);
+
         RealStart();
     }
     else
@@ -190,56 +124,15 @@
         {
             srvsList.removeLast();
             last->deleteLater();
-            Start(couldCancelPreviousRequest);
+            start(couldCancelPreviousRequest);
         } else
         {
-            connect(srvsList.last(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
+            connect(srvsList.last(), SIGNAL(nextPlease()), this, SLOT(iStart()));
             srvsList.push_back(this);
         }
     }
 }
 
-void TCPBase::onClientRead()
-{
-}
-
-void TCPBase::onClientDisconnect()
-{
-}
-
-void TCPBase::SendToClientFirst()
-{
-}
-
-void TCPBase::SendIPC(const QByteArray & buf)
-{
-    if (buf.size() > MAXMSGCHARS) return;
-    quint8 len = buf.size();
-    RawSendIPC(QByteArray::fromRawData((char *)&len, 1) + buf);
-}
-
-void TCPBase::RawSendIPC(const QByteArray & buf)
-{
-    if (!IPCSocket)
-    {
-        toSendBuf += buf;
-    }
-    else
-    {
-        if (toSendBuf.size() > 0)
-        {
-            IPCSocket->write(toSendBuf);
-            if(m_isDemoMode) demo.append(toSendBuf);
-            toSendBuf.clear();
-        }
-        if(!buf.isEmpty())
-        {
-            IPCSocket->write(buf);
-            if(m_isDemoMode) demo.append(buf);
-        }
-    }
-}
-
 bool TCPBase::couldBeRemoved()
 {
     return false;