2021-07-07 11:36:03 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2021 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** 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
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2022-02-14 15:54:04 +01:00
|
|
|
#include "environment.h"
|
|
|
|
|
#include "filepath.h"
|
2021-07-07 11:36:03 +02:00
|
|
|
#include "launcherpackets.h"
|
2022-03-02 04:12:25 +01:00
|
|
|
#include "processinterface.h"
|
2021-07-07 11:36:03 +02:00
|
|
|
|
2021-11-05 14:55:10 +01:00
|
|
|
#include <QDeadlineTimer>
|
2021-07-14 16:02:25 +02:00
|
|
|
#include <QHash>
|
|
|
|
|
#include <QMutex>
|
2021-08-19 16:39:21 +02:00
|
|
|
#include <QObject>
|
2021-07-14 16:02:25 +02:00
|
|
|
#include <QProcess>
|
|
|
|
|
#include <QWaitCondition>
|
|
|
|
|
|
|
|
|
|
#include <atomic>
|
2021-08-23 17:58:44 +02:00
|
|
|
#include <memory>
|
|
|
|
|
#include <vector>
|
2021-07-07 11:36:03 +02:00
|
|
|
|
|
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
class QLocalSocket;
|
|
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
|
|
|
|
|
namespace Utils {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2021-07-14 16:02:25 +02:00
|
|
|
class LauncherInterfacePrivate;
|
2021-08-23 17:58:44 +02:00
|
|
|
class LauncherHandle;
|
|
|
|
|
class LauncherSignal;
|
|
|
|
|
class ErrorSignal;
|
|
|
|
|
class StartedSignal;
|
|
|
|
|
class ReadyReadSignal;
|
|
|
|
|
class FinishedSignal;
|
|
|
|
|
|
|
|
|
|
// All the methods and data fields in this class are called / accessed from the caller's thread.
|
|
|
|
|
// Exceptions are explicitly marked.
|
|
|
|
|
class CallerHandle : public QObject
|
2021-07-14 16:02:25 +02:00
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
2021-08-18 13:40:43 +02:00
|
|
|
enum class SignalType {
|
|
|
|
|
NoSignal,
|
|
|
|
|
Error,
|
|
|
|
|
Started,
|
|
|
|
|
ReadyRead,
|
|
|
|
|
Finished
|
|
|
|
|
};
|
|
|
|
|
Q_ENUM(SignalType)
|
2022-02-16 03:21:51 +01:00
|
|
|
CallerHandle(QObject *parent, quintptr token)
|
|
|
|
|
: QObject(parent), m_token(token) {}
|
2021-08-27 17:56:02 +02:00
|
|
|
~CallerHandle() override;
|
2021-08-23 17:58:44 +02:00
|
|
|
|
|
|
|
|
LauncherHandle *launcherHandle() const { return m_launcherHandle; }
|
|
|
|
|
void setLauncherHandle(LauncherHandle *handle) { QMutexLocker locker(&m_mutex); m_launcherHandle = handle; }
|
2021-08-18 13:40:43 +02:00
|
|
|
|
2021-08-23 17:58:44 +02:00
|
|
|
bool waitForStarted(int msecs);
|
|
|
|
|
bool waitForReadyRead(int msces);
|
|
|
|
|
bool waitForFinished(int msecs);
|
|
|
|
|
|
|
|
|
|
// Returns the list of flushed signals.
|
|
|
|
|
QList<SignalType> flush();
|
|
|
|
|
QList<SignalType> flushFor(SignalType signalType);
|
|
|
|
|
bool shouldFlushFor(SignalType signalType) const;
|
|
|
|
|
// Called from launcher's thread exclusively.
|
|
|
|
|
void appendSignal(LauncherSignal *launcherSignal);
|
2021-07-14 16:02:25 +02:00
|
|
|
|
2021-08-26 12:02:39 +02:00
|
|
|
// Called from caller's or launcher's thread.
|
2021-08-18 13:40:43 +02:00
|
|
|
QProcess::ProcessState state() const;
|
2021-07-14 16:02:25 +02:00
|
|
|
void cancel();
|
|
|
|
|
|
2021-08-18 13:40:43 +02:00
|
|
|
QByteArray readAllStandardOutput();
|
|
|
|
|
QByteArray readAllStandardError();
|
2021-07-14 16:02:25 +02:00
|
|
|
|
2021-08-18 13:40:43 +02:00
|
|
|
qint64 processId() const;
|
2021-09-02 16:38:40 +02:00
|
|
|
int exitCode() const;
|
2021-08-18 13:40:43 +02:00
|
|
|
QString errorString() const;
|
|
|
|
|
void setErrorString(const QString &str);
|
2021-07-14 16:02:25 +02:00
|
|
|
|
2022-02-16 03:21:51 +01:00
|
|
|
void start(const QString &program, const QStringList &arguments);
|
2021-08-23 17:58:44 +02:00
|
|
|
// Called from caller's or launcher's thread.
|
|
|
|
|
void startIfNeeded();
|
2021-07-14 16:02:25 +02:00
|
|
|
|
2021-08-06 14:43:03 +02:00
|
|
|
qint64 write(const QByteArray &data);
|
|
|
|
|
|
2021-08-18 13:40:43 +02:00
|
|
|
QProcess::ProcessError error() const;
|
2021-08-26 12:02:39 +02:00
|
|
|
// Called from caller's or launcher's thread.
|
2021-08-18 13:40:43 +02:00
|
|
|
QString program() const;
|
2021-08-26 12:02:39 +02:00
|
|
|
// Called from caller's or launcher's thread.
|
|
|
|
|
QStringList arguments() const;
|
2022-02-16 03:21:51 +01:00
|
|
|
void setProcessSetupData(const ProcessSetupData &setup);
|
2021-08-18 13:40:43 +02:00
|
|
|
QProcess::ExitStatus exitStatus() const;
|
2021-08-09 12:17:13 +02:00
|
|
|
|
2021-07-14 16:02:25 +02:00
|
|
|
signals:
|
|
|
|
|
void errorOccurred(QProcess::ProcessError error);
|
|
|
|
|
void started();
|
2022-02-17 17:28:08 +01:00
|
|
|
void finished();
|
2021-07-14 16:02:25 +02:00
|
|
|
void readyReadStandardOutput();
|
|
|
|
|
void readyReadStandardError();
|
2021-08-18 10:45:57 +02:00
|
|
|
|
2021-08-23 17:58:44 +02:00
|
|
|
private:
|
|
|
|
|
bool waitForSignal(int msecs, CallerHandle::SignalType newSignal);
|
|
|
|
|
bool canWaitFor(SignalType newSignal) const; // TODO: employ me before calling waitForSignal()
|
2021-07-14 16:02:25 +02:00
|
|
|
|
2021-08-23 17:58:44 +02:00
|
|
|
// Called from caller's or launcher's thread. Call me with mutex locked.
|
2021-07-14 16:02:25 +02:00
|
|
|
void doStart();
|
2021-08-23 17:58:44 +02:00
|
|
|
// Called from caller's or launcher's thread.
|
|
|
|
|
void sendPacket(const Internal::LauncherPacket &packet);
|
|
|
|
|
// Called from caller's or launcher's thread.
|
|
|
|
|
bool isCalledFromCallersThread() const;
|
|
|
|
|
// Called from caller's or launcher's thread. Call me with mutex locked.
|
|
|
|
|
bool isCalledFromLaunchersThread() const;
|
2021-07-14 16:02:25 +02:00
|
|
|
|
2021-08-18 10:45:57 +02:00
|
|
|
QByteArray readAndClear(QByteArray &data) const
|
2021-07-14 16:02:25 +02:00
|
|
|
{
|
|
|
|
|
const QByteArray tmp = data;
|
|
|
|
|
data.clear();
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 17:58:44 +02:00
|
|
|
void handleError(const ErrorSignal *launcherSignal);
|
|
|
|
|
void handleStarted(const StartedSignal *launcherSignal);
|
|
|
|
|
void handleReadyRead(const ReadyReadSignal *launcherSignal);
|
|
|
|
|
void handleFinished(const FinishedSignal *launcherSignal);
|
2021-07-14 16:02:25 +02:00
|
|
|
|
2021-08-23 17:58:44 +02:00
|
|
|
// Lives in launcher's thread. Modified from caller's thread.
|
|
|
|
|
LauncherHandle *m_launcherHandle = nullptr;
|
2021-08-18 13:40:43 +02:00
|
|
|
|
2021-07-14 16:02:25 +02:00
|
|
|
mutable QMutex m_mutex;
|
2021-08-23 17:58:44 +02:00
|
|
|
// Accessed from caller's and launcher's thread
|
|
|
|
|
QList<LauncherSignal *> m_signals;
|
|
|
|
|
|
2021-07-14 16:02:25 +02:00
|
|
|
const quintptr m_token;
|
|
|
|
|
|
2021-08-23 17:58:44 +02:00
|
|
|
// Modified from caller's thread, read from launcher's thread
|
|
|
|
|
std::atomic<QProcess::ProcessState> m_processState = QProcess::NotRunning;
|
|
|
|
|
std::unique_ptr<StartProcessPacket> m_startPacket;
|
2021-07-14 16:02:25 +02:00
|
|
|
int m_processId = 0;
|
|
|
|
|
int m_exitCode = 0;
|
|
|
|
|
QProcess::ExitStatus m_exitStatus = QProcess::ExitStatus::NormalExit;
|
|
|
|
|
QByteArray m_stdout;
|
|
|
|
|
QByteArray m_stderr;
|
|
|
|
|
QProcess::ProcessError m_error = QProcess::UnknownError;
|
|
|
|
|
|
|
|
|
|
QString m_command;
|
|
|
|
|
QStringList m_arguments;
|
2022-02-16 03:21:51 +01:00
|
|
|
ProcessSetupData m_setup;
|
2021-08-23 17:58:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Moved to the launcher thread, returned to caller's thread.
|
|
|
|
|
// It's assumed that this object will be alive at least
|
|
|
|
|
// as long as the corresponding QtcProcess is alive.
|
|
|
|
|
|
|
|
|
|
class LauncherHandle : public QObject
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
|
|
|
|
// Called from caller's thread, moved to launcher's thread afterwards.
|
2022-02-16 03:21:51 +01:00
|
|
|
LauncherHandle(quintptr token) : m_token(token) {}
|
2021-08-23 17:58:44 +02:00
|
|
|
// Called from caller's thread exclusively.
|
|
|
|
|
bool waitForSignal(int msecs, CallerHandle::SignalType newSignal);
|
|
|
|
|
CallerHandle *callerHandle() const { return m_callerHandle; }
|
|
|
|
|
void setCallerHandle(CallerHandle *handle) { QMutexLocker locker(&m_mutex); m_callerHandle = handle; }
|
|
|
|
|
// Called from caller's thread exclusively.
|
|
|
|
|
void setCanceled() { m_awaitingShouldContinue = false; }
|
|
|
|
|
|
|
|
|
|
// Called from launcher's thread exclusively.
|
|
|
|
|
void handleSocketReady();
|
|
|
|
|
void handleSocketError(const QString &message);
|
|
|
|
|
void handlePacket(LauncherPacketType type, const QByteArray &payload);
|
2021-08-09 12:17:13 +02:00
|
|
|
|
2021-08-23 17:58:44 +02:00
|
|
|
// Called from caller's thread exclusively.
|
|
|
|
|
bool isSocketError() const { return m_socketError; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// Called from caller's thread exclusively.
|
2021-11-05 14:55:10 +01:00
|
|
|
bool doWaitForSignal(QDeadlineTimer deadline, CallerHandle::SignalType newSignal);
|
2021-08-23 17:58:44 +02:00
|
|
|
// Called from launcher's thread exclusively. Call me with mutex locked.
|
|
|
|
|
void wakeUpIfWaitingFor(CallerHandle::SignalType newSignal);
|
|
|
|
|
|
|
|
|
|
// Called from launcher's thread exclusively. Call me with mutex locked.
|
|
|
|
|
void flushCaller();
|
|
|
|
|
// Called from launcher's thread exclusively.
|
|
|
|
|
void handleErrorPacket(const QByteArray &packetData);
|
|
|
|
|
void handleStartedPacket(const QByteArray &packetData);
|
|
|
|
|
void handleReadyReadStandardOutput(const QByteArray &packetData);
|
|
|
|
|
void handleReadyReadStandardError(const QByteArray &packetData);
|
|
|
|
|
void handleFinishedPacket(const QByteArray &packetData);
|
|
|
|
|
|
|
|
|
|
// Called from caller's or launcher's thread.
|
|
|
|
|
bool isCalledFromLaunchersThread() const;
|
|
|
|
|
bool isCalledFromCallersThread() const;
|
|
|
|
|
|
|
|
|
|
// Lives in caller's thread. Modified only in caller's thread. TODO: check usages - all should be with mutex
|
|
|
|
|
CallerHandle *m_callerHandle = nullptr;
|
|
|
|
|
|
|
|
|
|
// Modified only in caller's thread.
|
|
|
|
|
bool m_awaitingShouldContinue = false;
|
|
|
|
|
mutable QMutex m_mutex;
|
|
|
|
|
QWaitCondition m_waitCondition;
|
|
|
|
|
const quintptr m_token;
|
|
|
|
|
std::atomic_bool m_socketError = false;
|
|
|
|
|
// Modified only in caller's thread.
|
|
|
|
|
CallerHandle::SignalType m_waitingFor = CallerHandle::SignalType::NoSignal;
|
2021-07-14 16:02:25 +02:00
|
|
|
};
|
|
|
|
|
|
2021-07-07 11:36:03 +02:00
|
|
|
class LauncherSocket : public QObject
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
2021-07-14 16:02:25 +02:00
|
|
|
friend class LauncherInterfacePrivate;
|
2021-07-07 11:36:03 +02:00
|
|
|
public:
|
2021-08-18 10:45:57 +02:00
|
|
|
// Called from caller's or launcher's thread.
|
2021-07-07 11:36:03 +02:00
|
|
|
bool isReady() const { return m_socket.load(); }
|
|
|
|
|
void sendData(const QByteArray &data);
|
|
|
|
|
|
2021-08-18 10:45:57 +02:00
|
|
|
// Called from caller's thread exclusively.
|
2022-02-17 18:31:57 +01:00
|
|
|
CallerHandle *registerHandle(QObject *parent, quintptr token);
|
2021-07-14 16:02:25 +02:00
|
|
|
void unregisterHandle(quintptr token);
|
|
|
|
|
|
2021-07-07 11:36:03 +02:00
|
|
|
signals:
|
|
|
|
|
void ready();
|
|
|
|
|
void errorOccurred(const QString &error);
|
|
|
|
|
|
|
|
|
|
private:
|
2021-08-18 10:45:57 +02:00
|
|
|
// Called from caller's thread, moved to launcher's thread.
|
2021-07-14 16:02:25 +02:00
|
|
|
LauncherSocket(QObject *parent = nullptr);
|
2021-08-26 12:02:39 +02:00
|
|
|
// Called from launcher's thread exclusively.
|
|
|
|
|
~LauncherSocket() override;
|
2021-07-14 16:02:25 +02:00
|
|
|
|
2021-08-18 10:45:57 +02:00
|
|
|
// Called from launcher's thread exclusively.
|
2021-07-14 16:02:25 +02:00
|
|
|
LauncherHandle *handleForToken(quintptr token) const;
|
2021-07-07 11:36:03 +02:00
|
|
|
|
2021-08-18 10:45:57 +02:00
|
|
|
// Called from launcher's thread exclusively.
|
2021-07-07 11:36:03 +02:00
|
|
|
void setSocket(QLocalSocket *socket);
|
|
|
|
|
void shutdown();
|
|
|
|
|
|
2021-08-18 10:45:57 +02:00
|
|
|
// Called from launcher's thread exclusively.
|
2021-07-07 11:36:03 +02:00
|
|
|
void handleSocketError();
|
|
|
|
|
void handleSocketDataAvailable();
|
|
|
|
|
void handleSocketDisconnected();
|
|
|
|
|
void handleError(const QString &error);
|
|
|
|
|
void handleRequests();
|
|
|
|
|
|
2021-08-18 13:40:43 +02:00
|
|
|
// Called from caller's or launcher's thread.
|
|
|
|
|
bool isCalledFromLaunchersThread() const;
|
|
|
|
|
|
2021-07-07 11:36:03 +02:00
|
|
|
std::atomic<QLocalSocket *> m_socket{nullptr};
|
|
|
|
|
PacketParser m_packetParser;
|
|
|
|
|
std::vector<QByteArray> m_requests;
|
2021-07-14 16:02:25 +02:00
|
|
|
mutable QMutex m_mutex;
|
|
|
|
|
QHash<quintptr, LauncherHandle *> m_handles;
|
2021-07-07 11:36:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Utils
|