QTfrontend/net/tcpBase.cpp
changeset 6616 f77bb02b669f
parent 6060 fdfc01419815
child 6700 e04da46ee43c
equal deleted inserted replaced
6615:65602f1ef0f8 6616:f77bb02b669f
    32 TCPBase::~TCPBase()
    32 TCPBase::~TCPBase()
    33 {
    33 {
    34 }
    34 }
    35 
    35 
    36 TCPBase::TCPBase(bool demoMode) :
    36 TCPBase::TCPBase(bool demoMode) :
    37   m_isDemoMode(demoMode),
    37     m_isDemoMode(demoMode),
    38   IPCSocket(0)
    38     IPCSocket(0)
    39 {
    39 {
    40   if(!IPCServer) {
    40     if(!IPCServer)
    41     IPCServer = new QTcpServer(0);
    41     {
    42     IPCServer->setMaxPendingConnections(1);
    42         IPCServer = new QTcpServer(0);
    43     if (!IPCServer->listen(QHostAddress::LocalHost)) {
    43         IPCServer->setMaxPendingConnections(1);
    44       QMessageBox::critical(0, tr("Error"),
    44         if (!IPCServer->listen(QHostAddress::LocalHost))
    45                 tr("Unable to start the server: %1.")
    45         {
    46                 .arg(IPCServer->errorString()));
    46             QMessageBox::critical(0, tr("Error"),
    47       exit(0); // FIXME - should be graceful exit here
    47                                   tr("Unable to start the server: %1.")
       
    48                                   .arg(IPCServer->errorString()));
       
    49             exit(0); // FIXME - should be graceful exit here
       
    50         }
    48     }
    51     }
    49   }
    52     ipc_port=IPCServer->serverPort();
    50   ipc_port=IPCServer->serverPort();
       
    51 }
    53 }
    52 
    54 
    53 void TCPBase::NewConnection()
    55 void TCPBase::NewConnection()
    54 {
    56 {
    55   if(IPCSocket) {
    57     if(IPCSocket)
    56     // connection should be already finished
    58     {
    57     return;
    59         // connection should be already finished
    58   }
    60         return;
    59   disconnect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
    61     }
    60   IPCSocket = IPCServer->nextPendingConnection();
    62     disconnect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
    61   if(!IPCSocket) return;
    63     IPCSocket = IPCServer->nextPendingConnection();
    62   connect(IPCSocket, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
    64     if(!IPCSocket) return;
    63   connect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
    65     connect(IPCSocket, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
    64   SendToClientFirst();
    66     connect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
       
    67     SendToClientFirst();
    65 }
    68 }
    66 
    69 
    67 void TCPBase::RealStart()
    70 void TCPBase::RealStart()
    68 {
    71 {
    69   connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
    72     connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
    70   IPCSocket = 0;
    73     IPCSocket = 0;
    71 
    74 
    72   QProcess * process;
    75     QProcess * process;
    73   process = new QProcess;
    76     process = new QProcess;
    74   connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
    77     connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
    75   QStringList arguments=getArguments();
    78     QStringList arguments=getArguments();
    76 
    79 
    77   // redirect everything written on stdout/stderr
    80     // redirect everything written on stdout/stderr
    78   if(isDevBuild)
    81     if(isDevBuild)
    79     process->setProcessChannelMode(QProcess::ForwardedChannels);
    82         process->setProcessChannelMode(QProcess::ForwardedChannels);
    80   process->start(bindir->absolutePath() + "/hwengine", arguments);
    83     process->start(bindir->absolutePath() + "/hwengine", arguments);
    81 }
    84 }
    82 
    85 
    83 void TCPBase::ClientDisconnect()
    86 void TCPBase::ClientDisconnect()
    84 {
    87 {
    85   disconnect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
    88     disconnect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
    86   onClientDisconnect();
    89     onClientDisconnect();
    87 
    90 
    88   if(srvsList.size()==1) srvsList.pop_front();
    91     if(srvsList.size()==1) srvsList.pop_front();
    89   emit isReadyNow();
    92     emit isReadyNow();
    90   IPCSocket->deleteLater();
    93     IPCSocket->deleteLater();
    91   deleteLater();
    94     deleteLater();
    92 }
    95 }
    93 
    96 
    94 void TCPBase::ClientRead()
    97 void TCPBase::ClientRead()
    95 {
    98 {
    96   QByteArray readed=IPCSocket->readAll();
    99     QByteArray readed=IPCSocket->readAll();
    97   if(readed.isEmpty()) return;
   100     if(readed.isEmpty()) return;
    98   readbuffer.append(readed);
   101     readbuffer.append(readed);
    99   onClientRead();
   102     onClientRead();
   100 }
   103 }
   101 
   104 
   102 void TCPBase::StartProcessError(QProcess::ProcessError error)
   105 void TCPBase::StartProcessError(QProcess::ProcessError error)
   103 {
   106 {
   104   QMessageBox::critical(0, tr("Error"),
   107     QMessageBox::critical(0, tr("Error"),
   105             tr("Unable to run engine: %1 (")
   108                           tr("Unable to run engine: %1 (")
   106             .arg(error) + bindir->absolutePath() + "/hwengine)");
   109                           .arg(error) + bindir->absolutePath() + "/hwengine)");
   107 }
   110 }
   108 
   111 
   109 void TCPBase::tcpServerReady()
   112 void TCPBase::tcpServerReady()
   110 {
   113 {
   111   disconnect(srvsList.takeFirst(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
   114     disconnect(srvsList.takeFirst(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
   112 
   115 
   113   RealStart();
   116     RealStart();
   114 }
   117 }
   115 
   118 
   116 void TCPBase::Start()
   119 void TCPBase::Start()
   117 {
   120 {
   118   if(srvsList.isEmpty()) {
   121     if(srvsList.isEmpty())
   119     srvsList.push_back(this);
   122     {
   120   } else {
   123         srvsList.push_back(this);
   121     connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
   124     }
   122     srvsList.push_back(this);
   125     else
   123     return;
   126     {
   124   }
   127         connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
       
   128         srvsList.push_back(this);
       
   129         return;
       
   130     }
   125 
   131 
   126   RealStart();
   132     RealStart();
   127 }
   133 }
   128 
   134 
   129 void TCPBase::onClientRead()
   135 void TCPBase::onClientRead()
   130 {
   136 {
   131 }
   137 }
   148 void TCPBase::RawSendIPC(const QByteArray & buf)
   154 void TCPBase::RawSendIPC(const QByteArray & buf)
   149 {
   155 {
   150     if (!IPCSocket)
   156     if (!IPCSocket)
   151     {
   157     {
   152         toSendBuf += buf;
   158         toSendBuf += buf;
   153     } else
   159     }
       
   160     else
   154     {
   161     {
   155         if (toSendBuf.size() > 0)
   162         if (toSendBuf.size() > 0)
   156         {
   163         {
   157             IPCSocket->write(toSendBuf);
   164             IPCSocket->write(toSendBuf);
   158             if(m_isDemoMode) demo.append(toSendBuf);
   165             if(m_isDemoMode) demo.append(toSendBuf);
   159             toSendBuf.clear();
   166             toSendBuf.clear();
   160         }
   167         }
   161         if(!buf.isEmpty()) {
   168         if(!buf.isEmpty())
   162           IPCSocket->write(buf);
   169         {
   163           if(m_isDemoMode) demo.append(buf);
   170             IPCSocket->write(buf);
       
   171             if(m_isDemoMode) demo.append(buf);
   164         }
   172         }
   165     }
   173     }
   166 }
   174 }