2015-10-01 12:45:06 +02:00
|
|
|
/****************************************************************************
|
2015-06-01 18:51:55 +02:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2015-06-01 18:51:55 +02:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:58:39 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2015-06-01 18:51:55 +02:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2015-06-01 18:51:55 +02:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "connectionclient.h"
|
|
|
|
|
|
2015-07-28 16:04:49 +02:00
|
|
|
#include "clangbackendipcdebugutils.h"
|
2015-08-19 12:36:43 +02:00
|
|
|
#include "cmbcompletecodemessage.h"
|
2015-08-31 14:36:58 +02:00
|
|
|
#include "cmbregistertranslationunitsforeditormessage.h"
|
|
|
|
|
#include "cmbunregistertranslationunitsforeditormessage.h"
|
2015-06-16 12:38:04 +02:00
|
|
|
|
|
|
|
|
#include <QCoreApplication>
|
2016-07-11 13:44:02 +02:00
|
|
|
#include <QMetaMethod>
|
2015-06-16 12:38:04 +02:00
|
|
|
#include <QProcess>
|
2015-11-30 20:32:47 +01:00
|
|
|
#include <QTemporaryDir>
|
2015-06-16 12:38:04 +02:00
|
|
|
#include <QThread>
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2015-06-16 11:56:00 +02:00
|
|
|
namespace ClangBackEnd {
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2016-07-04 14:55:58 +02:00
|
|
|
ConnectionClient::ConnectionClient()
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
processAliveTimer.setInterval(10000);
|
|
|
|
|
|
2015-09-21 13:21:55 +02:00
|
|
|
const bool startAliveTimer = !qgetenv("QTC_CLANG_NO_ALIVE_TIMER").toInt();
|
|
|
|
|
|
2016-07-11 13:44:02 +02:00
|
|
|
if (startAliveTimer)
|
|
|
|
|
connectAliveTimer();
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2016-07-11 13:44:02 +02:00
|
|
|
connectLocalSocketError();
|
|
|
|
|
connectLocalSocketConnected();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2016-07-11 13:44:02 +02:00
|
|
|
void ConnectionClient::startProcessAndConnectToServerAsynchronously()
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2016-08-04 15:26:53 +02:00
|
|
|
process_ = startProcess();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ConnectionClient::disconnectFromServer()
|
|
|
|
|
{
|
|
|
|
|
localSocket.disconnectFromServer();
|
|
|
|
|
if (localSocket.state() != QLocalSocket::UnconnectedState)
|
|
|
|
|
return localSocket.waitForDisconnected();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ConnectionClient::isConnected() const
|
|
|
|
|
{
|
|
|
|
|
return localSocket.state() == QLocalSocket::ConnectedState;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-19 12:36:43 +02:00
|
|
|
void ConnectionClient::ensureMessageIsWritten()
|
2015-06-18 13:04:38 +02:00
|
|
|
{
|
2015-07-02 10:47:53 +02:00
|
|
|
while (isConnected() && localSocket.bytesToWrite() > 0)
|
|
|
|
|
localSocket.waitForBytesWritten(50);
|
2015-06-18 13:04:38 +02:00
|
|
|
}
|
|
|
|
|
|
2015-08-19 12:36:43 +02:00
|
|
|
void ConnectionClient::sendEndMessage()
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2016-07-04 14:55:58 +02:00
|
|
|
sendEndCommand();
|
2015-06-01 18:51:55 +02:00
|
|
|
localSocket.flush();
|
2015-08-19 12:36:43 +02:00
|
|
|
ensureMessageIsWritten();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConnectionClient::resetProcessAliveTimer()
|
|
|
|
|
{
|
|
|
|
|
isAliveTimerResetted = true;
|
|
|
|
|
processAliveTimer.start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConnectionClient::setProcessAliveTimerInterval(int processTimerInterval)
|
|
|
|
|
{
|
|
|
|
|
processAliveTimer.setInterval(processTimerInterval);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-30 20:32:47 +01:00
|
|
|
QProcessEnvironment ConnectionClient::processEnvironment() const
|
|
|
|
|
{
|
|
|
|
|
auto processEnvironment = QProcessEnvironment::systemEnvironment();
|
|
|
|
|
|
|
|
|
|
if (temporaryDirectory().isValid()) {
|
|
|
|
|
const QString temporaryDirectoryPath = temporaryDirectory().path();
|
|
|
|
|
processEnvironment.insert(QStringLiteral("TMPDIR"), temporaryDirectoryPath);
|
|
|
|
|
processEnvironment.insert(QStringLiteral("TMP"), temporaryDirectoryPath);
|
|
|
|
|
processEnvironment.insert(QStringLiteral("TEMP"), temporaryDirectoryPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return processEnvironment;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-04 15:26:53 +02:00
|
|
|
const QTemporaryDir &ConnectionClient::temporaryDirectory() const
|
|
|
|
|
{
|
|
|
|
|
static QTemporaryDir temporaryDirectory(QDir::tempPath() + QStringLiteral("/qtc-clang-XXXXXX"));
|
|
|
|
|
|
|
|
|
|
return temporaryDirectory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LinePrefixer &ConnectionClient::stdErrPrefixer()
|
|
|
|
|
{
|
|
|
|
|
return stdErrPrefixer_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LinePrefixer &ConnectionClient::stdOutPrefixer()
|
|
|
|
|
{
|
|
|
|
|
return stdOutPrefixer_;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-11 13:44:02 +02:00
|
|
|
std::unique_ptr<QProcess> ConnectionClient::startProcess()
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2016-07-11 13:44:02 +02:00
|
|
|
processIsStarting = true;
|
2015-07-28 16:04:49 +02:00
|
|
|
|
2016-07-11 13:44:02 +02:00
|
|
|
auto process = std::unique_ptr<QProcess>(new QProcess);
|
|
|
|
|
connectProcessFinished(process.get());
|
|
|
|
|
connectProcessStarted(process.get());
|
|
|
|
|
connectStandardOutputAndError(process.get());
|
|
|
|
|
process->setProcessEnvironment(processEnvironment());
|
|
|
|
|
process->start(processPath(), {connectionName()});
|
|
|
|
|
resetProcessAliveTimer();
|
|
|
|
|
|
|
|
|
|
return process;
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2016-07-11 13:44:02 +02:00
|
|
|
void ConnectionClient::restartProcessAsynchronously()
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2016-07-11 13:44:02 +02:00
|
|
|
if (!processIsStarting) {
|
|
|
|
|
finishProcess(std::move(process_));
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2016-07-11 13:44:02 +02:00
|
|
|
startProcessAndConnectToServerAsynchronously();
|
|
|
|
|
}
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConnectionClient::restartProcessIfTimerIsNotResettedAndSocketIsEmpty()
|
|
|
|
|
{
|
|
|
|
|
if (isAliveTimerResetted) {
|
|
|
|
|
isAliveTimerResetted = false;
|
|
|
|
|
return; // Already reset, but we were scheduled after.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (localSocket.bytesAvailable() > 0)
|
|
|
|
|
return; // We come first, the incoming data was not yet processed.
|
|
|
|
|
|
2016-07-11 13:44:02 +02:00
|
|
|
restartProcessAsynchronously();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2016-07-11 13:44:02 +02:00
|
|
|
void ConnectionClient::connectToLocalSocket()
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2016-07-11 13:44:02 +02:00
|
|
|
if (!isConnected()) {
|
2015-06-01 18:51:55 +02:00
|
|
|
localSocket.connectToServer(connectionName());
|
2016-07-11 13:44:02 +02:00
|
|
|
QTimer::singleShot(20, this, &ConnectionClient::connectToLocalSocket);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-11 13:44:02 +02:00
|
|
|
void ConnectionClient::endProcess(QProcess *process)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2016-07-11 13:44:02 +02:00
|
|
|
if (isProcessIsRunning() && isConnected()) {
|
2015-08-19 12:36:43 +02:00
|
|
|
sendEndMessage();
|
2016-07-11 13:44:02 +02:00
|
|
|
process->waitForFinished();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-11 13:44:02 +02:00
|
|
|
void ConnectionClient::terminateProcess(QProcess *process)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2016-07-19 14:25:31 +02:00
|
|
|
Q_UNUSED(process)
|
2015-06-01 18:51:55 +02:00
|
|
|
#ifndef Q_OS_WIN32
|
|
|
|
|
if (isProcessIsRunning()) {
|
2016-07-11 13:44:02 +02:00
|
|
|
process->terminate();
|
|
|
|
|
process->waitForFinished();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-11 13:44:02 +02:00
|
|
|
void ConnectionClient::killProcess(QProcess *process)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
if (isProcessIsRunning()) {
|
2016-07-11 13:44:02 +02:00
|
|
|
process->kill();
|
|
|
|
|
process->waitForFinished();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-11 13:44:02 +02:00
|
|
|
void ConnectionClient::resetProcessIsStarting()
|
|
|
|
|
{
|
|
|
|
|
processIsStarting = false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-25 12:53:59 +02:00
|
|
|
void ConnectionClient::printLocalSocketError(QLocalSocket::LocalSocketError socketError)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2015-06-25 12:53:59 +02:00
|
|
|
if (socketError != QLocalSocket::ServerNotFoundError)
|
2016-08-04 15:26:53 +02:00
|
|
|
qWarning() << outputName() << "LocalSocket Error:" << localSocket.errorString();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConnectionClient::printStandardOutput()
|
|
|
|
|
{
|
2016-08-04 15:26:53 +02:00
|
|
|
qDebug("%s", stdOutPrefixer_.prefix(process_->readAllStandardOutput()).constData());
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConnectionClient::printStandardError()
|
|
|
|
|
{
|
2016-08-04 15:26:53 +02:00
|
|
|
qDebug("%s", stdErrPrefixer_.prefix(process_->readAllStandardError()).constData());
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2016-07-11 13:44:02 +02:00
|
|
|
void ConnectionClient::connectLocalSocketConnected()
|
|
|
|
|
{
|
|
|
|
|
connect(&localSocket,
|
|
|
|
|
&QLocalSocket::connected,
|
|
|
|
|
this,
|
|
|
|
|
&ConnectionClient::connectedToLocalSocket);
|
|
|
|
|
|
|
|
|
|
connect(&localSocket,
|
|
|
|
|
&QLocalSocket::connected,
|
|
|
|
|
this,
|
|
|
|
|
&ConnectionClient::resetProcessIsStarting);
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-01 18:51:55 +02:00
|
|
|
void ConnectionClient::finishProcess()
|
|
|
|
|
{
|
2016-07-11 13:44:02 +02:00
|
|
|
finishProcess(std::move(process_));
|
|
|
|
|
}
|
2015-07-28 16:04:49 +02:00
|
|
|
|
2016-07-11 13:44:02 +02:00
|
|
|
void ConnectionClient::finishProcess(std::unique_ptr<QProcess> &&process)
|
|
|
|
|
{
|
|
|
|
|
if (process) {
|
|
|
|
|
processAliveTimer.stop();
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2016-07-11 13:44:02 +02:00
|
|
|
disconnectProcessFinished(process.get());
|
|
|
|
|
endProcess(process.get());
|
|
|
|
|
disconnectFromServer();
|
|
|
|
|
terminateProcess(process.get());
|
|
|
|
|
killProcess(process.get());
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2016-08-04 15:26:53 +02:00
|
|
|
resetCounter();
|
2016-07-11 13:44:02 +02:00
|
|
|
}
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ConnectionClient::waitForEcho()
|
|
|
|
|
{
|
|
|
|
|
return localSocket.waitForReadyRead();
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-11 13:44:02 +02:00
|
|
|
bool ConnectionClient::waitForConnected()
|
|
|
|
|
{
|
|
|
|
|
bool isConnected = false;
|
|
|
|
|
|
|
|
|
|
for (int counter = 0; counter < 100; counter++) {
|
|
|
|
|
isConnected = localSocket.waitForConnected(20);
|
|
|
|
|
if (isConnected)
|
|
|
|
|
return isConnected;
|
|
|
|
|
else {
|
|
|
|
|
QThread::msleep(30);
|
|
|
|
|
QCoreApplication::instance()->processEvents();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-04 15:26:53 +02:00
|
|
|
qWarning() << outputName() << "cannot connect:" << localSocket.errorString();
|
2016-07-11 13:44:02 +02:00
|
|
|
|
|
|
|
|
return isConnected;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
QProcess *ConnectionClient::processForTestOnly() const
|
|
|
|
|
{
|
|
|
|
|
return process_.get();
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-04 14:55:58 +02:00
|
|
|
QIODevice *ConnectionClient::ioDevice()
|
|
|
|
|
{
|
|
|
|
|
return &localSocket;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-01 18:51:55 +02:00
|
|
|
bool ConnectionClient::isProcessIsRunning() const
|
|
|
|
|
{
|
|
|
|
|
return process_ && process_->state() == QProcess::Running;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-11 13:44:02 +02:00
|
|
|
void ConnectionClient::connectProcessFinished(QProcess *process) const
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2016-07-11 13:44:02 +02:00
|
|
|
connect(process,
|
|
|
|
|
static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
|
|
|
|
|
this,
|
|
|
|
|
&ConnectionClient::restartProcessAsynchronously);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-11 13:44:02 +02:00
|
|
|
void ConnectionClient::connectProcessStarted(QProcess *process) const
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2016-07-11 13:44:02 +02:00
|
|
|
connect(process,
|
|
|
|
|
&QProcess::started,
|
2015-06-01 18:51:55 +02:00
|
|
|
this,
|
2016-07-11 13:44:02 +02:00
|
|
|
&ConnectionClient::connectToLocalSocket);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2016-07-11 13:44:02 +02:00
|
|
|
void ConnectionClient::disconnectProcessFinished(QProcess *process) const
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2016-07-11 13:44:02 +02:00
|
|
|
if (process) {
|
|
|
|
|
disconnect(process,
|
2015-06-01 18:51:55 +02:00
|
|
|
static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
|
|
|
|
|
this,
|
2016-07-11 13:44:02 +02:00
|
|
|
&ConnectionClient::restartProcessAsynchronously);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-11 13:44:02 +02:00
|
|
|
void ConnectionClient::connectStandardOutputAndError(QProcess *process) const
|
|
|
|
|
{
|
|
|
|
|
connect(process, &QProcess::readyReadStandardOutput, this, &ConnectionClient::printStandardOutput);
|
|
|
|
|
connect(process, &QProcess::readyReadStandardError, this, &ConnectionClient::printStandardError);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConnectionClient::connectLocalSocketError() const
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2016-07-11 13:44:02 +02:00
|
|
|
connect(&localSocket,
|
|
|
|
|
static_cast<void (QLocalSocket::*)(QLocalSocket::LocalSocketError)>(&QLocalSocket::error),
|
|
|
|
|
this,
|
|
|
|
|
&ConnectionClient::printLocalSocketError);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConnectionClient::connectAliveTimer()
|
|
|
|
|
{
|
|
|
|
|
connect(&processAliveTimer,
|
|
|
|
|
&QTimer::timeout,
|
|
|
|
|
this,
|
|
|
|
|
&ConnectionClient::restartProcessIfTimerIsNotResettedAndSocketIsEmpty);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QString &ConnectionClient::processPath() const
|
|
|
|
|
{
|
|
|
|
|
return processPath_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConnectionClient::setProcessPath(const QString &processPath)
|
|
|
|
|
{
|
|
|
|
|
processPath_ = processPath;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-16 11:56:00 +02:00
|
|
|
} // namespace ClangBackEnd
|
2015-06-01 18:51:55 +02:00
|
|
|
|