refactor: move no-restart to CoreArgs

chore: Client remove unused ClientArgs member m_args
This commit is contained in:
sithlord48
2025-09-17 21:39:24 -04:00
committed by Chris Rizzitello
parent 295137dbd2
commit eeaf139bd9
15 changed files with 137 additions and 80 deletions

View File

@ -45,15 +45,14 @@ using namespace deskflow::client;
Client::Client(
IEventQueue *events, const std::string &name, const NetworkAddress &address, ISocketFactory *socketFactory,
deskflow::Screen *screen, deskflow::ClientArgs const &args
deskflow::Screen *screen
)
: m_name(name),
m_serverAddress(address),
m_socketFactory(socketFactory),
m_screen(screen),
m_events(events),
m_useSecureNetwork(Settings::value(Settings::Security::TlsEnabled).toBool()),
m_args(args)
m_useSecureNetwork(Settings::value(Settings::Security::TlsEnabled).toBool())
{
assert(m_socketFactory != nullptr);
assert(m_screen != nullptr);
@ -422,7 +421,7 @@ void Client::setupConnection()
handleDisconnected();
});
m_events->addHandler(EventTypes::SocketStopRetry, m_stream->getEventTarget(), [this](const auto &) {
m_args.m_restartable = false;
Settings::setValue(Settings::Core::RestartOnFailure, false);
});
}

View File

@ -12,7 +12,6 @@
#include "HelloBack.h"
#include "base/EventTypes.h"
#include "deskflow/ClientArgs.h"
#include "deskflow/Clipboard.h"
#include "mt/CondVar.h"
#include "net/NetworkAddress.h"
@ -60,7 +59,7 @@ public:
*/
Client(
IEventQueue *events, const std::string &name, const NetworkAddress &address, ISocketFactory *socketFactory,
deskflow::Screen *screen, deskflow::ClientArgs const &args
deskflow::Screen *screen
);
Client(Client const &) = delete;
Client(Client &&) = delete;
@ -202,7 +201,6 @@ private:
bool m_useSecureNetwork = false;
bool m_enableClipboard = true;
size_t m_maximumClipboardSize = INT_MAX;
deskflow::ClientArgs m_args;
size_t m_resolvedAddressesCount = 0;
std::unique_ptr<deskflow::client::HelloBack> m_pHelloBack;
};

View File

@ -117,9 +117,9 @@ QVariant Settings::defaultValue(const QString &key)
return false;
}
if ((key == Gui::CloseToTray) || (key == Gui::LogExpanded) || (key == Gui::SymbolicTrayIcon) ||
(key == Gui::CloseReminder) || (key == Security::TlsEnabled) || (key == Security::CheckPeers) ||
(key == Client::LanguageSync) || (key == Gui::ShowGenericClientFailureDialog)) {
if ((key == Core::RestartOnFailure) || (key == Gui::CloseToTray) || (key == Gui::LogExpanded) ||
(key == Gui::SymbolicTrayIcon) || (key == Gui::CloseReminder) || (key == Security::TlsEnabled) ||
(key == Security::CheckPeers) || (key == Client::LanguageSync) || (key == Gui::ShowGenericClientFailureDialog)) {
return true;
}

View File

@ -50,6 +50,7 @@ public:
inline static const auto StartedBefore = QStringLiteral("core/startedBefore");
inline static const auto UpdateUrl = QStringLiteral("core/updateUrl");
inline static const auto Display = QStringLiteral("core/display");
inline static const auto RestartOnFailure = QStringLiteral("core/restartOnFailure");
};
struct Daemon
{
@ -173,6 +174,7 @@ private:
, Settings::Core::StartedBefore
, Settings::Core::UpdateUrl
, Settings::Core::Display
, Settings::Core::RestartOnFailure
, Settings::Daemon::Command
, Settings::Daemon::Elevate
, Settings::Daemon::LogFile

View File

@ -128,14 +128,8 @@ private:
#else
#define DAEMON_RUNNING(running_)
#endif
constexpr static auto s_helpGeneralArgs = //
" -1, --no-restart do not try to restart on failure.\n"
"* --restart restart the server automatically if it fails.\n";
constexpr static auto s_helpVersionArgs = //
" -h, --help display this help and exit.\n";
constexpr static auto s_helpCommonArgs = " [--restart|--no-restart]";
constexpr static auto s_helpVersionArgs = " -h, --help display this help and exit.\n";
#if !defined(WINAPI_LIBEI) && WINAPI_XWINDOWS
constexpr static auto s_helpNoWayland = //

View File

@ -101,13 +101,7 @@ bool ArgParser::parseClientArgs(deskflow::ClientArgs &args, int argc, const char
bool ArgParser::parseGenericArgs(int argc, const char *const *argv, int &i) const
{
if (isArg(i, argc, argv, "-1", "--no-restart")) {
// don't try to restart
argsBase().m_restartable = false;
} else if (isArg(i, argc, argv, nullptr, "--restart")) {
// try to restart
argsBase().m_restartable = true;
} else if (isArg(i, argc, argv, nullptr, "--no-hooks")) {
if (isArg(i, argc, argv, nullptr, "--no-hooks")) {
argsBase().m_noHooks = true;
} else if (isArg(i, argc, argv, "-h", "--help")) {
if (m_app) {

View File

@ -32,9 +32,6 @@ public:
/// @brief Stores what type of object this is
ClassType m_classType = ClassType::Base;
/// @brief Should the app restart automatically
bool m_restartable = true;
/// @brief Should the app use hooks
bool m_noHooks = false;

View File

@ -89,7 +89,8 @@ void ClientApp::parseArgs(int argc, const char *const *argv)
// we'll try to resolve the address each time we connect to the
// server. a bad port will never get better. patch by Brent
// Priddy.
if (!args().m_restartable || e.getError() == SocketAddressException::SocketError::BadPort) {
if (!Settings::value(Settings::Core::RestartOnFailure).toBool() ||
e.getError() == SocketAddressException::SocketError::BadPort) {
LOG_CRIT("%s: %s" BYE, args().m_pname, e.what(), args().m_pname);
bye(s_exitFailed);
}
@ -106,20 +107,15 @@ void ClientApp::help()
<< " [--yscroll <delta>]"
<< " [--sync-language]"
<< " [--invert-scroll]"
#ifdef WINAPI_XWINDOWS
<< " [--display <display>]"
#endif
<< s_helpCommonArgs << " <server-address>"
<< " <server-address>"
<< "\n\n"
<< "Connect to a " << kAppName << " mouse/keyboard sharing server.\n"
<< "\n"
<< s_helpGeneralArgs << " --yscroll <delta> defines the vertical scrolling delta,\n"
<< " --yscroll <delta> defines the vertical scrolling delta,\n"
<< " which is 120 by default.\n"
<< " --sync-language enable language synchronization.\n"
<< " --invert-scroll invert scroll direction on this\n"
<< " computer.\n"
<< s_helpVersionArgs << "\n"
<< "* marks defaults.\n"
<< s_helpNoWayland
@ -252,7 +248,7 @@ void ClientApp::handleClientRefused(const Event &e)
{
std::unique_ptr<Client::FailInfo> info(static_cast<Client::FailInfo *>(e.getData()));
if (!args().m_restartable || !info->m_retry) {
if (!Settings::value(Settings::Core::RestartOnFailure).toBool() || !info->m_retry) {
LOG_ERR("failed to connect to server: %s", info->m_what.c_str());
getEvents()->addEvent(Event(EventTypes::Quit));
} else {
@ -266,7 +262,7 @@ void ClientApp::handleClientRefused(const Event &e)
void ClientApp::handleClientDisconnected()
{
LOG_IPC("disconnected from server");
if (!args().m_restartable) {
if (!Settings::value(Settings::Core::RestartOnFailure).toBool()) {
getEvents()->addEvent(Event(EventTypes::Quit));
} else if (!m_suspended) {
scheduleClientRestart(s_retryTime);
@ -275,7 +271,7 @@ void ClientApp::handleClientDisconnected()
Client *ClientApp::openClient(const std::string &name, const NetworkAddress &address, deskflow::Screen *screen)
{
auto *client = new Client(getEvents(), name, address, getSocketFactory(), screen, args());
auto *client = new Client(getEvents(), name, address, getSocketFactory(), screen);
try {
getEvents()->addHandler(EventTypes::ClientConnected, client->getEventTarget(), [this](const auto &) {
@ -343,7 +339,7 @@ bool ClientApp::startClient()
return false;
}
if (args().m_restartable) {
if (Settings::value(Settings::Core::RestartOnFailure).toBool()) {
scheduleClientRestart(retryTime);
return true;
} else {

View File

@ -95,6 +95,13 @@ void CoreArgParser::parse()
auto value = m_parser.value(CoreArgs::displayOption);
Settings::setValue(Settings::Core::Display, value);
}
if (m_parser.isSet(CoreArgs::restartOption)) {
bool value =
((m_parser.value(CoreArgs::restartOption).toLower() == "true") ||
(m_parser.value(CoreArgs::restartOption) == "1"));
Settings::setValue(Settings::Core::RestartOnFailure, value);
}
}
[[noreturn]] void CoreArgParser::showHelpText() const

View File

@ -51,7 +51,23 @@ struct CoreArgs
"prevent-sleep", "When true the machine will be prevented from sleeping while the program is running", "value"
);
inline static const auto options = {helpOption, versionOption, configOption, interfaceOption,
portOption, nameOption, logLevelOption, logFileOption,
secureOption, tlsCertOption, preventSleepOption, displayOption};
inline static const auto restartOption = QCommandLineOption(
{"r", "restartOnFailure"}, "Set if the core should automatically restart if it fails", "value"
);
inline static const auto options = {
helpOption,
versionOption,
configOption,
interfaceOption,
portOption,
nameOption,
logLevelOption,
logFileOption,
secureOption,
tlsCertOption,
preventSleepOption,
restartOption,
displayOption
};
};

View File

@ -104,16 +104,8 @@ void ServerApp::help()
{
std::stringstream help;
help << "\n\nServer Mode:\n\n"
<< "Usage: " << kAppId << "-core server"
<< " --config <pathname>"
#if WINAPI_XWINDOWS
<< " [--display <display>]"
#endif
<< s_helpCommonArgs << "\n"
<< "Usage: " << kAppId << "-core server \n"
<< " -c, --config <pathname> path of the configuration file\n"
<< s_helpGeneralArgs
<< " --disable-client-cert-check disable client SSL certificate \n"
" checking (deprecated)\n"
<< s_helpVersionArgs << "\n"
@ -368,7 +360,7 @@ bool ServerApp::initServer()
return false;
}
if (args().m_restartable) {
if (Settings::value(Settings::Core::RestartOnFailure).toBool()) {
// install a timer and handler to retry later
assert(m_timer == nullptr);
LOG_DEBUG("retry in %.0f seconds", retryTime);
@ -428,7 +420,7 @@ bool ServerApp::startServer()
m_serverState = Started;
return true;
} catch (SocketAddressInUseException &e) {
if (args().m_restartable) {
if (Settings::value(Settings::Core::RestartOnFailure).toBool()) {
LOG_ERR("cannot listen for clients: %s", e.what());
} else {
LOG_CRIT("cannot listen for clients: %s", e.what());
@ -440,7 +432,7 @@ bool ServerApp::startServer()
return false;
}
if (args().m_restartable) {
if (Settings::value(Settings::Core::RestartOnFailure).toBool()) {
// install a timer and handler to retry later
assert(m_timer == nullptr);
const auto retryTime = 10.0;

View File

@ -276,30 +276,6 @@ void ArgParserTests::deprecatedArg_crypoPass_false()
QCOMPARE(i, 1);
}
void ArgParserTests::generic_noRestart()
{
int i = 1;
const int argc = 2;
const char *kNoRestartCmd[argc] = {"stub", "--no-restart"};
m_parser.parseGenericArgs(argc, kNoRestartCmd, i);
QVERIFY(!m_parser.argsBase().m_restartable);
QCOMPARE(i, 1);
}
void ArgParserTests::generic_restart()
{
int i = 1;
const int argc = 2;
const char *kRestartCmd[argc] = {"stub", "--restart"};
m_parser.parseGenericArgs(argc, kRestartCmd, i);
QVERIFY(m_parser.argsBase().m_restartable);
QCOMPARE(i, 1);
}
void ArgParserTests::generic_unknown()
{
int i = 1;

View File

@ -32,8 +32,6 @@ private Q_SLOTS:
void client_badArgs();
void deprecatedArg_crypoPass_true();
void deprecatedArg_crypoPass_false();
void generic_noRestart();
void generic_restart();
void generic_unknown();
void generic_noHook();

View File

@ -190,6 +190,86 @@ void CoreArgParserTests::preventSleep_0()
QVERIFY(!Settings::value(Settings::Core::PreventSleep).toBool());
}
void CoreArgParserTests::restartLongOption_0()
{
QStringList args = {"stub", "client", "--restartOnFailure", "0"};
CoreArgParser parser(args);
parser.parse();
QVERIFY(!Settings::value(Settings::Core::RestartOnFailure).toBool());
}
void CoreArgParserTests::restartLongOption_1()
{
QStringList args = {"stub", "client", "--restartOnFailure", "1"};
CoreArgParser parser(args);
parser.parse();
QVERIFY(Settings::value(Settings::Core::RestartOnFailure).toBool());
}
void CoreArgParserTests::restartLongOption_false()
{
QStringList args = {"stub", "client", "--restartOnFailure", "false"};
CoreArgParser parser(args);
parser.parse();
QVERIFY(!Settings::value(Settings::Core::RestartOnFailure).toBool());
}
void CoreArgParserTests::restartLongOption_true()
{
QStringList args = {"stub", "client", "--restartOnFailure", "true"};
CoreArgParser parser(args);
parser.parse();
QVERIFY(Settings::value(Settings::Core::RestartOnFailure).toBool());
}
void CoreArgParserTests::restartShortOption_0()
{
QStringList args = {"stub", "client", "-r", "0"};
CoreArgParser parser(args);
parser.parse();
QVERIFY(!Settings::value(Settings::Core::RestartOnFailure).toBool());
}
void CoreArgParserTests::restartShortOption_1()
{
QStringList args = {"stub", "client", "-r", "1"};
CoreArgParser parser(args);
parser.parse();
QVERIFY(Settings::value(Settings::Core::RestartOnFailure).toBool());
}
void CoreArgParserTests::restartShortOption_false()
{
QStringList args = {"stub", "client", "-r", "false"};
CoreArgParser parser(args);
parser.parse();
QVERIFY(!Settings::value(Settings::Core::RestartOnFailure).toBool());
}
void CoreArgParserTests::restartShortOption_true()
{
QStringList args = {"stub", "client", "-r", "true"};
CoreArgParser parser(args);
parser.parse();
QVERIFY(Settings::value(Settings::Core::RestartOnFailure).toBool());
}
void CoreArgParserTests::preventSleep_true()
{
QStringList args = {"stub", "client", "--prevent-sleep", "true"};

View File

@ -32,6 +32,14 @@ private Q_SLOTS:
void preventSleep_false();
void preventSleep_1();
void preventSleep_0();
void restartLongOption_0();
void restartLongOption_1();
void restartLongOption_false();
void restartLongOption_true();
void restartShortOption_0();
void restartShortOption_1();
void restartShortOption_false();
void restartShortOption_true();
private:
inline static const QString m_settingsPath = QStringLiteral("tmp/test");