QTfrontend/net/tcpBase.cpp
changeset 10177 0f9042536c46
parent 10175 c92668840ea8
child 10248 7b9b44a051f8
equal deleted inserted replaced
10176:ea022e9483c2 10177:0f9042536c46
    41 
    41 
    42 }
    42 }
    43 
    43 
    44 EngineInstance::~EngineInstance()
    44 EngineInstance::~EngineInstance()
    45 {
    45 {
       
    46     qDebug() << "EngineInstance delete" << QThread::currentThreadId();
    46 }
    47 }
    47 
    48 
    48 void EngineInstance::setArguments(const QStringList & arguments)
    49 void EngineInstance::setArguments(const QStringList & arguments)
    49 {
    50 {
    50     m_arguments.clear();
    51     m_arguments.clear();
    62     }
    63     }
    63 }
    64 }
    64 
    65 
    65 void EngineInstance::start()
    66 void EngineInstance::start()
    66 {
    67 {
       
    68     qDebug() << "EngineInstance start" << QThread::currentThreadId();
       
    69 
    67     RunEngine(m_argv.size(), m_argv.data());
    70     RunEngine(m_argv.size(), m_argv.data());
       
    71 
       
    72     emit finished();
    68 }
    73 }
    69 
    74 
    70 #endif
    75 #endif
    71 
    76 
    72 QList<TCPBase*> srvsList;
    77 QList<TCPBase*> srvsList;
    73 QPointer<QTcpServer> TCPBase::IPCServer(0);
    78 QPointer<QTcpServer> TCPBase::IPCServer(0);
    74 
    79 
    75 TCPBase::~TCPBase()
    80 TCPBase::~TCPBase()
    76 {
    81 {
       
    82     if(m_hasStarted)
       
    83     {
       
    84         if(IPCSocket)
       
    85             IPCSocket->close();
       
    86 
       
    87         if(m_connected)
       
    88         {
       
    89 #ifdef HWLIBRARY
       
    90             if(!thread)
       
    91                 qDebug("WTF");
       
    92             thread->quit();
       
    93             thread->wait();
       
    94 #else
       
    95             process->waitForFinished(1000);
       
    96 #endif
       
    97         }
       
    98     }
    77     // make sure this object is not in the server list anymore
    99     // make sure this object is not in the server list anymore
    78     srvsList.removeOne(this);
   100     srvsList.removeOne(this);
    79 
   101 
    80     if (IPCSocket)
   102     if (IPCSocket)
    81         IPCSocket->deleteLater();
   103         IPCSocket->deleteLater();
   133 {
   155 {
   134     connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
   156     connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
   135     IPCSocket = 0;
   157     IPCSocket = 0;
   136 
   158 
   137 #ifdef HWLIBRARY
   159 #ifdef HWLIBRARY
   138     QThread *thread = new QThread();
   160     thread = new QThread(this);
   139     EngineInstance *instance = new EngineInstance();
   161     EngineInstance *instance = new EngineInstance();
   140     instance->setArguments(getArguments());
   162     instance->setArguments(getArguments());
   141 
   163 
   142     instance->moveToThread(thread);
   164     instance->moveToThread(thread);
   143 
   165 
   145     connect(instance, SIGNAL(finished()), thread, SLOT(quit()));
   167     connect(instance, SIGNAL(finished()), thread, SLOT(quit()));
   146     connect(instance, SIGNAL(finished()), instance, SLOT(deleteLater()));
   168     connect(instance, SIGNAL(finished()), instance, SLOT(deleteLater()));
   147     connect(instance, SIGNAL(finished()), thread, SLOT(deleteLater()));
   169     connect(instance, SIGNAL(finished()), thread, SLOT(deleteLater()));
   148     thread->start();
   170     thread->start();
   149 #else
   171 #else
   150     QProcess * process;
   172     process = new QProcess(this);
   151     process = new QProcess();
       
   152     connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
   173     connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
   153     QStringList arguments = getArguments();
   174     QStringList arguments = getArguments();
   154 
   175 
   155 #ifdef QT_DEBUG
   176 #ifdef QT_DEBUG
   156     // redirect everything written on stdout/stderr
   177     // redirect everything written on stdout/stderr
   166 {
   187 {
   167     disconnect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
   188     disconnect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
   168     onClientDisconnect();
   189     onClientDisconnect();
   169 
   190 
   170     if(!simultaneousRun())
   191     if(!simultaneousRun())
       
   192     {
       
   193 #ifdef HWLIBRARY
       
   194         thread->quit();
       
   195         thread->wait();
       
   196 #endif
   171         emit isReadyNow();
   197         emit isReadyNow();
       
   198     }
   172     IPCSocket->deleteLater();
   199     IPCSocket->deleteLater();
   173 
   200 
   174     deleteLater();
   201     deleteLater();
   175 }
   202 }
   176 
   203 
   177 void TCPBase::ClientRead()
   204 void TCPBase::ClientRead()
   178 {
   205 {
   179     QByteArray readed=IPCSocket->readAll();
   206     QByteArray read = IPCSocket->readAll();
   180     if(readed.isEmpty()) return;
   207     if(read.isEmpty()) return;
   181     readbuffer.append(readed);
   208     readbuffer.append(read);
   182     onClientRead();
   209     onClientRead();
   183 }
   210 }
   184 
   211 
   185 void TCPBase::StartProcessError(QProcess::ProcessError error)
   212 void TCPBase::StartProcessError(QProcess::ProcessError error)
   186 {
   213 {
   209             && last->couldBeRemoved()
   236             && last->couldBeRemoved()
   210             && (last->isConnected() || !last->hasStarted())
   237             && (last->isConnected() || !last->hasStarted())
   211             && (last->parent() == parent()))
   238             && (last->parent() == parent()))
   212         {
   239         {
   213             srvsList.removeLast();
   240             srvsList.removeLast();
   214             last->deleteLater();
   241             delete last;
   215             Start(couldCancelPreviousRequest);
   242             Start(couldCancelPreviousRequest);
   216         } else
   243         } else
   217         {
   244         {
   218             connect(last, SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
   245             connect(last, SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
   219             srvsList.push_back(this);
   246             srvsList.push_back(this);