From 64786ddcb72b91493b10bf25ffe945847f053dde Mon Sep 17 00:00:00 2001 From: sithlord48 Date: Thu, 7 Aug 2025 17:17:20 -0400 Subject: [PATCH] refactor: XDeskflow Classes => DeskflowException Classes --- src/lib/client/Client.cpp | 4 +- src/lib/client/ServerProxy.cpp | 4 +- src/lib/deskflow/App.cpp | 4 +- src/lib/deskflow/AppUtil.h | 4 +- src/lib/deskflow/CMakeLists.txt | 4 +- src/lib/deskflow/DeskflowException.cpp | 112 ++++++++++++++++++ .../{XDeskflow.h => DeskflowException.h} | 39 +++--- src/lib/deskflow/ProtocolUtil.cpp | 6 +- src/lib/deskflow/XDeskflow.cpp | 111 ----------------- src/lib/deskflow/win32/AppUtilWindows.cpp | 4 +- src/lib/server/ClientProxy1_0.cpp | 4 +- src/lib/server/ClientProxyUnknown.cpp | 14 +-- src/lib/server/Config.cpp | 4 +- src/lib/server/Server.cpp | 6 +- 14 files changed, 161 insertions(+), 159 deletions(-) create mode 100644 src/lib/deskflow/DeskflowException.cpp rename src/lib/deskflow/{XDeskflow.h => DeskflowException.h} (63%) delete mode 100644 src/lib/deskflow/XDeskflow.cpp diff --git a/src/lib/client/Client.cpp b/src/lib/client/Client.cpp index 8341745bb..e10ca1437 100644 --- a/src/lib/client/Client.cpp +++ b/src/lib/client/Client.cpp @@ -14,13 +14,13 @@ #include "base/TMethodJob.h" #include "client/ServerProxy.h" #include "deskflow/AppUtil.h" +#include "deskflow/DeskflowException.h" #include "deskflow/IPlatformScreen.h" #include "deskflow/PacketStreamFilter.h" #include "deskflow/ProtocolTypes.h" #include "deskflow/ProtocolUtil.h" #include "deskflow/Screen.h" #include "deskflow/StreamChunker.h" -#include "deskflow/XDeskflow.h" #include "mt/Thread.h" #include "net/IDataSocket.h" #include "net/ISocketFactory.h" @@ -68,7 +68,7 @@ Client::Client( cleanupConnection(); }, [this](int major, int minor) { - sendConnectionFailedEvent(XIncompatibleClient(major, minor).what()); + sendConnectionFailedEvent(IncompatibleClientException(major, minor).what()); cleanupTimer(); cleanupConnection(); } diff --git a/src/lib/client/ServerProxy.cpp b/src/lib/client/ServerProxy.cpp index ee0fcc4a6..2214dbb9c 100644 --- a/src/lib/client/ServerProxy.cpp +++ b/src/lib/client/ServerProxy.cpp @@ -15,11 +15,11 @@ #include "deskflow/AppUtil.h" #include "deskflow/Clipboard.h" #include "deskflow/ClipboardChunk.h" +#include "deskflow/DeskflowException.h" #include "deskflow/OptionTypes.h" #include "deskflow/ProtocolTypes.h" #include "deskflow/ProtocolUtil.h" #include "deskflow/StreamChunker.h" -#include "deskflow/XDeskflow.h" #include "io/IStream.h" #include @@ -111,7 +111,7 @@ void ServerProxy::handleData() case Disconnect: return; } - } catch (const XBadClient &e) { + } catch (const BadClientException &e) { LOG_ERR("protocol error from server: %s", e.what()); ProtocolUtil::writef(m_stream, kMsgEBad); m_client->disconnect("invalid message from server"); diff --git a/src/lib/deskflow/App.cpp b/src/lib/deskflow/App.cpp index 9d7df0eae..3998216e6 100644 --- a/src/lib/deskflow/App.cpp +++ b/src/lib/deskflow/App.cpp @@ -15,8 +15,8 @@ #include "common/Constants.h" #include "deskflow/ArgsBase.h" #include "deskflow/Config.h" +#include "deskflow/DeskflowException.h" #include "deskflow/ProtocolTypes.h" -#include "deskflow/XDeskflow.h" #if SYSAPI_WIN32 #include "base/IEventQueue.h" @@ -92,7 +92,7 @@ int App::run(int argc, char **argv) try { result = appUtil().run(argc, argv); - } catch (XExitApp &e) { + } catch (ExitAppException &e) { // instead of showing a nasty error, just exit with the error code. // not sure if i like this behaviour, but it's probably better than // using the exit(int) function! diff --git a/src/lib/deskflow/AppUtil.h b/src/lib/deskflow/AppUtil.h index de1e3e30e..5e6efff1d 100644 --- a/src/lib/deskflow/AppUtil.h +++ b/src/lib/deskflow/AppUtil.h @@ -7,8 +7,8 @@ #pragma once +#include "deskflow/DeskflowException.h" #include "deskflow/IAppUtil.h" -#include "deskflow/XDeskflow.h" class AppUtil : public IAppUtil { @@ -20,7 +20,7 @@ public: IApp &app() const override; virtual void exitApp(int code) { - throw XExitApp(code); + throw ExitAppException(code); } static AppUtil &instance(); diff --git a/src/lib/deskflow/CMakeLists.txt b/src/lib/deskflow/CMakeLists.txt index bfe312f9f..e546bf26f 100644 --- a/src/lib/deskflow/CMakeLists.txt +++ b/src/lib/deskflow/CMakeLists.txt @@ -85,6 +85,8 @@ add_library(${lib_name} STATIC ${PLATFORM_CODE} Config.h DaemonApp.cpp DaemonApp.h + DeskflowException.cpp + DeskflowException.h DisplayInvalidException.h IApp.h IAppUtil.h @@ -127,8 +129,6 @@ add_library(${lib_name} STATIC ${PLATFORM_CODE} ServerArgs.h StreamChunker.cpp StreamChunker.h - XDeskflow.cpp - XDeskflow.h languages/LanguageManager.cpp languages/LanguageManager.h ipc/DaemonIpcServer.cpp diff --git a/src/lib/deskflow/DeskflowException.cpp b/src/lib/deskflow/DeskflowException.cpp new file mode 100644 index 000000000..486b0d426 --- /dev/null +++ b/src/lib/deskflow/DeskflowException.cpp @@ -0,0 +1,112 @@ +/* + * Deskflow -- mouse and keyboard sharing utility + * SPDX-FileCopyrightText: (C) 2025 Deskflow Developers + * SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd. + * SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman + * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception + */ + +#include "deskflow/DeskflowException.h" +#include "base/String.h" + +// +// BadClientException +// + +std::string BadClientException::getWhat() const throw() +{ + return "BadClientException"; +} + +// +// InvalidProtocolException +// + +std::string InvalidProtocolException::getWhat() const throw() +{ + return "InvalidProtocolException"; +} + +// +// IncompatibleClientException +// + +IncompatibleClientException::IncompatibleClientException(int major, int minor) : m_major(major), m_minor(minor) +{ + // do nothing +} + +int IncompatibleClientException::getMajor() const noexcept +{ + return m_major; +} + +int IncompatibleClientException::getMinor() const noexcept +{ + return m_minor; +} + +std::string IncompatibleClientException::getWhat() const throw() +{ + return format( + "IncompatibleClientException", "incompatible client %{1}.%{2}", deskflow::string::sprintf("%d", m_major).c_str(), + deskflow::string::sprintf("%d", m_minor).c_str() + ); +} + +// +// DuplicateClientException +// + +DuplicateClientException::DuplicateClientException(const std::string &name) : m_name(name) +{ + // do nothing +} + +const std::string &DuplicateClientException::getName() const noexcept +{ + return m_name; +} + +std::string DuplicateClientException::getWhat() const throw() +{ + return format("DuplicateClientException", "duplicate client %{1}", m_name.c_str()); +} + +// +// UnknownClientException +// + +UnknownClientException::UnknownClientException(const std::string &name) : m_name(name) +{ + // do nothing +} + +const std::string &UnknownClientException::getName() const noexcept +{ + return m_name; +} + +std::string UnknownClientException::getWhat() const throw() +{ + return format("UnknownClientException", "unknown client %{1}", m_name.c_str()); +} + +// +// ExitAppException +// + +ExitAppException::ExitAppException(int code) : m_code(code) +{ + // do nothing +} + +int ExitAppException::getCode() const noexcept +{ + return m_code; +} + +std::string ExitAppException::getWhat() const throw() +{ + return format("ExitAppException", "exiting with code %{1}", deskflow::string::sprintf("%d", m_code).c_str()); +} diff --git a/src/lib/deskflow/XDeskflow.h b/src/lib/deskflow/DeskflowException.h similarity index 63% rename from src/lib/deskflow/XDeskflow.h rename to src/lib/deskflow/DeskflowException.h index 15cf0e4b1..c1b9ea219 100644 --- a/src/lib/deskflow/XDeskflow.h +++ b/src/lib/deskflow/DeskflowException.h @@ -1,5 +1,6 @@ /* * Deskflow -- mouse and keyboard sharing utility + * SPDX-FileCopyrightText: (C) 2025 Deskflow Developers * SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd. * SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception @@ -10,30 +11,30 @@ #include "base/XBase.h" /** - * @brief The XDeskflow class Generic deskflow exception class + * @brief The DeskflowException class Generic deskflow exception class */ -class XDeskflow : public XBase +class DeskflowException : public XBase { using XBase::XBase; }; /** - * @brief XBadClient - Thrown when the client fails to follow the protocol. + * @brief BadClientException - Thrown when the client fails to follow the protocol. */ -class XBadClient : public XDeskflow +class BadClientException : public DeskflowException { - using XDeskflow::XDeskflow; + using DeskflowException::DeskflowException; protected: std::string getWhat() const throw() override; }; /** - * @brief XInvalidProtocol - Thrown when the server protocol is unreconized. + * @brief InvalidProtocolException - Thrown when the server protocol is unreconized. */ -class XInvalidProtocol : public XDeskflow +class InvalidProtocolException : public DeskflowException { - using XDeskflow::XDeskflow; + using DeskflowException::DeskflowException; protected: std::string getWhat() const throw() override; @@ -43,10 +44,10 @@ protected: /*! Thrown when a client attempting to connect has an incompatible version. */ -class XIncompatibleClient : public XDeskflow +class IncompatibleClientException : public DeskflowException { public: - XIncompatibleClient(int major, int minor); + IncompatibleClientException(int major, int minor); //! @name accessors //@{ @@ -71,11 +72,11 @@ private: Thrown when a client attempting to connect is using the same name as a client that is already connected. */ -class XDuplicateClient : public XDeskflow +class DuplicateClientException : public DeskflowException { public: - explicit XDuplicateClient(const std::string &name); - ~XDuplicateClient() throw() override = default; + explicit DuplicateClientException(const std::string &name); + ~DuplicateClientException() throw() override = default; //! @name accessors //@{ @@ -97,11 +98,11 @@ private: Thrown when a client attempting to connect is using a name that is unknown to the server. */ -class XUnknownClient : public XDeskflow +class UnknownClientException : public DeskflowException { public: - explicit XUnknownClient(const std::string &name); - ~XUnknownClient() throw() override = default; + explicit UnknownClientException(const std::string &name); + ~UnknownClientException() throw() override = default; //! @name accessors //@{ @@ -124,11 +125,11 @@ Thrown when we want to abort, with the opportunity to clean up. This is a little bit of a hack, but it's a better way of exiting, than just calling exit(int). */ -class XExitApp : public XDeskflow +class ExitAppException : public DeskflowException { public: - explicit XExitApp(int code); - ~XExitApp() throw() override = default; + explicit ExitAppException(int code); + ~ExitAppException() throw() override = default; //! Get the exit code int getCode() const noexcept; diff --git a/src/lib/deskflow/ProtocolUtil.cpp b/src/lib/deskflow/ProtocolUtil.cpp index b79d3f2bd..d79c2cbaf 100644 --- a/src/lib/deskflow/ProtocolUtil.cpp +++ b/src/lib/deskflow/ProtocolUtil.cpp @@ -7,8 +7,8 @@ #include "deskflow/ProtocolUtil.h" #include "base/Log.h" +#include "deskflow/DeskflowException.h" #include "deskflow/ProtocolTypes.h" -#include "deskflow/XDeskflow.h" #include "io/IStream.h" #include #include @@ -193,7 +193,7 @@ void ProtocolUtil::vreadf(deskflow::IStream *stream, const char *fmt, va_list ar if (len > PROTOCOL_MAX_STRING_LENGTH) { LOG_ERR("read: string length exceeds maximum allowed size: %u", len); - throw XBadClient("Too long message received"); + throw BadClientException("Too long message received"); } readBytes(stream, len, destination); @@ -507,7 +507,7 @@ uint32_t ProtocolUtil::readVectorSize(deskflow::IStream *stream) if (size > PROTOCOL_MAX_LIST_LENGTH) { LOG_ERR("readVectorSize: vector length exceeds maximum allowed size: %u", size); - throw XBadClient("Too long message received"); + throw BadClientException("Too long message received"); } return size; diff --git a/src/lib/deskflow/XDeskflow.cpp b/src/lib/deskflow/XDeskflow.cpp deleted file mode 100644 index 57d1fcddf..000000000 --- a/src/lib/deskflow/XDeskflow.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Deskflow -- mouse and keyboard sharing utility - * SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd. - * SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman - * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception - */ - -#include "deskflow/XDeskflow.h" -#include "base/String.h" - -// -// XBadClient -// - -std::string XBadClient::getWhat() const throw() -{ - return "XBadClient"; -} - -// -// XInvalidProtocol -// - -std::string XInvalidProtocol::getWhat() const throw() -{ - return "XInvalidProtocol"; -} - -// -// XIncompatibleClient -// - -XIncompatibleClient::XIncompatibleClient(int major, int minor) : m_major(major), m_minor(minor) -{ - // do nothing -} - -int XIncompatibleClient::getMajor() const noexcept -{ - return m_major; -} - -int XIncompatibleClient::getMinor() const noexcept -{ - return m_minor; -} - -std::string XIncompatibleClient::getWhat() const throw() -{ - return format( - "XIncompatibleClient", "incompatible client %{1}.%{2}", deskflow::string::sprintf("%d", m_major).c_str(), - deskflow::string::sprintf("%d", m_minor).c_str() - ); -} - -// -// XDuplicateClient -// - -XDuplicateClient::XDuplicateClient(const std::string &name) : m_name(name) -{ - // do nothing -} - -const std::string &XDuplicateClient::getName() const noexcept -{ - return m_name; -} - -std::string XDuplicateClient::getWhat() const throw() -{ - return format("XDuplicateClient", "duplicate client %{1}", m_name.c_str()); -} - -// -// XUnknownClient -// - -XUnknownClient::XUnknownClient(const std::string &name) : m_name(name) -{ - // do nothing -} - -const std::string &XUnknownClient::getName() const noexcept -{ - return m_name; -} - -std::string XUnknownClient::getWhat() const throw() -{ - return format("XUnknownClient", "unknown client %{1}", m_name.c_str()); -} - -// -// XExitApp -// - -XExitApp::XExitApp(int code) : m_code(code) -{ - // do nothing -} - -int XExitApp::getCode() const noexcept -{ - return m_code; -} - -std::string XExitApp::getWhat() const throw() -{ - return format("XExitApp", "exiting with code %{1}", deskflow::string::sprintf("%d", m_code).c_str()); -} diff --git a/src/lib/deskflow/win32/AppUtilWindows.cpp b/src/lib/deskflow/win32/AppUtilWindows.cpp index c06822ce8..6d92414a1 100644 --- a/src/lib/deskflow/win32/AppUtilWindows.cpp +++ b/src/lib/deskflow/win32/AppUtilWindows.cpp @@ -17,8 +17,8 @@ #include "common/Constants.h" #include "deskflow/App.h" #include "deskflow/ArgsBase.h" +#include "deskflow/DeskflowException.h" #include "deskflow/Screen.h" -#include "deskflow/XDeskflow.h" #include "platform/MSWindowsScreen.h" #include @@ -77,7 +77,7 @@ void AppUtilWindows::exitApp(int code) break; default: - throw XExitApp(code); + throw ExitAppException(code); } } diff --git a/src/lib/server/ClientProxy1_0.cpp b/src/lib/server/ClientProxy1_0.cpp index e369457c3..feadccd3c 100644 --- a/src/lib/server/ClientProxy1_0.cpp +++ b/src/lib/server/ClientProxy1_0.cpp @@ -10,8 +10,8 @@ #include "base/IEventQueue.h" #include "base/Log.h" +#include "deskflow/DeskflowException.h" #include "deskflow/ProtocolUtil.h" -#include "deskflow/XDeskflow.h" #include "io/IStream.h" #include @@ -131,7 +131,7 @@ void ClientProxy1_0::handleData() while (getStream()->read(nullptr, 4)) ; } - } catch (const XBadClient &e) { + } catch (const BadClientException &e) { LOG_ERR("protocol error from client \"%s\": %s", getName().c_str(), e.what()); disconnect(); return; diff --git a/src/lib/server/ClientProxyUnknown.cpp b/src/lib/server/ClientProxyUnknown.cpp index 574b1d156..94952e34b 100644 --- a/src/lib/server/ClientProxyUnknown.cpp +++ b/src/lib/server/ClientProxyUnknown.cpp @@ -11,9 +11,9 @@ #include "base/IEventQueue.h" #include "base/Log.h" #include "deskflow/AppUtil.h" +#include "deskflow/DeskflowException.h" #include "deskflow/ProtocolTypes.h" #include "deskflow/ProtocolUtil.h" -#include "deskflow/XDeskflow.h" #include "io/IOException.h" #include "io/IStream.h" #include "server/ClientProxy1_0.h" @@ -189,7 +189,7 @@ void ClientProxyUnknown::initProxy(const std::string &name, int major, int minor // hangup (with error) if version isn't supported if (m_proxy == nullptr) { - throw XIncompatibleClient(major, minor); + throw IncompatibleClientException(major, minor); } } @@ -203,19 +203,19 @@ void ClientProxyUnknown::handleData() // limit the maximum length of the hello if (uint32_t n = m_stream->getSize(); n > kMaxHelloLength) { LOG_DEBUG1("hello reply too long"); - throw XBadClient(); + throw BadClientException(); } // parse the reply to hello int16_t major; int16_t minor; if (std::string protocolName; !ProtocolUtil::readf(m_stream, kMsgHelloBack, &protocolName, &major, &minor, &name)) { - throw XBadClient(); + throw BadClientException(); } // disallow invalid version numbers if (major <= 0 || minor < 0) { - throw XIncompatibleClient(major, minor); + throw IncompatibleClientException(major, minor); } // remove stream event handlers. the proxy we're about to create @@ -233,11 +233,11 @@ void ClientProxyUnknown::handleData() // wait until the proxy signals that it's ready or has disconnected addProxyHandlers(); return; - } catch (XIncompatibleClient &e) { + } catch (IncompatibleClientException &e) { // client is incompatible LOG_WARN("client \"%s\" has incompatible version %d.%d)", name.c_str(), e.getMajor(), e.getMinor()); ProtocolUtil::writef(m_stream, kMsgEIncompatible, kProtocolMajorVersion, kProtocolMinorVersion); - } catch (XBadClient &) { + } catch (BadClientException &) { // client not behaving LOG_WARN("protocol error from client \"%s\"", name.c_str()); ProtocolUtil::writef(m_stream, kMsgEBad); diff --git a/src/lib/server/Config.cpp b/src/lib/server/Config.cpp index 228f8cf6c..2feea9b45 100644 --- a/src/lib/server/Config.cpp +++ b/src/lib/server/Config.cpp @@ -8,10 +8,10 @@ #include "server/Config.h" #include "base/IEventQueue.h" +#include "deskflow/DeskflowException.h" #include "deskflow/KeyMap.h" #include "deskflow/KeyTypes.h" #include "deskflow/OptionTypes.h" -#include "deskflow/XDeskflow.h" #include "net/SocketException.h" #include "server/Server.h" @@ -1313,7 +1313,7 @@ std::string Config::getOptionValue(OptionID id, OptionValue value) } else if (enumValue == Barrier) { return kBarrierProtocolOption; } else { - throw XInvalidProtocol(); + throw InvalidProtocolException(); } } diff --git a/src/lib/server/Server.cpp b/src/lib/server/Server.cpp index 57c2b5c94..570c28df5 100644 --- a/src/lib/server/Server.cpp +++ b/src/lib/server/Server.cpp @@ -13,13 +13,13 @@ #include "base/Log.h" #include "base/TMethodJob.h" #include "deskflow/AppUtil.h" +#include "deskflow/DeskflowException.h" #include "deskflow/IPlatformScreen.h" #include "deskflow/OptionTypes.h" #include "deskflow/PacketStreamFilter.h" #include "deskflow/ProtocolTypes.h" #include "deskflow/Screen.h" #include "deskflow/StreamChunker.h" -#include "deskflow/XDeskflow.h" #include "mt/Thread.h" #include "net/TCPSocket.h" #include "server/ClientListener.h" @@ -280,7 +280,7 @@ std::string Server::protocolString() const } else if (m_protocol == Barrier) { return kBarrierProtocolName; } - throw XInvalidProtocol(); + throw InvalidProtocolException(); } uint32_t Server::getNumClients() const @@ -1080,7 +1080,7 @@ void Server::processOptions() } else if (enumValue == Barrier) { m_protocol = Barrier; } else { - throw XInvalidProtocol(); + throw InvalidProtocolException(); } } else if (id == kOptionScreenSwitchDelay) { m_switchWaitDelay = 1.0e-3 * static_cast(value);