refactor: XDeskflow Classes => DeskflowException Classes

This commit is contained in:
sithlord48 2025-08-07 17:17:20 -04:00 committed by Nick Bolton
parent e4a9d1eb78
commit 64786ddcb7
14 changed files with 161 additions and 159 deletions

View file

@ -14,13 +14,13 @@
#include "base/TMethodJob.h" #include "base/TMethodJob.h"
#include "client/ServerProxy.h" #include "client/ServerProxy.h"
#include "deskflow/AppUtil.h" #include "deskflow/AppUtil.h"
#include "deskflow/DeskflowException.h"
#include "deskflow/IPlatformScreen.h" #include "deskflow/IPlatformScreen.h"
#include "deskflow/PacketStreamFilter.h" #include "deskflow/PacketStreamFilter.h"
#include "deskflow/ProtocolTypes.h" #include "deskflow/ProtocolTypes.h"
#include "deskflow/ProtocolUtil.h" #include "deskflow/ProtocolUtil.h"
#include "deskflow/Screen.h" #include "deskflow/Screen.h"
#include "deskflow/StreamChunker.h" #include "deskflow/StreamChunker.h"
#include "deskflow/XDeskflow.h"
#include "mt/Thread.h" #include "mt/Thread.h"
#include "net/IDataSocket.h" #include "net/IDataSocket.h"
#include "net/ISocketFactory.h" #include "net/ISocketFactory.h"
@ -68,7 +68,7 @@ Client::Client(
cleanupConnection(); cleanupConnection();
}, },
[this](int major, int minor) { [this](int major, int minor) {
sendConnectionFailedEvent(XIncompatibleClient(major, minor).what()); sendConnectionFailedEvent(IncompatibleClientException(major, minor).what());
cleanupTimer(); cleanupTimer();
cleanupConnection(); cleanupConnection();
} }

View file

@ -15,11 +15,11 @@
#include "deskflow/AppUtil.h" #include "deskflow/AppUtil.h"
#include "deskflow/Clipboard.h" #include "deskflow/Clipboard.h"
#include "deskflow/ClipboardChunk.h" #include "deskflow/ClipboardChunk.h"
#include "deskflow/DeskflowException.h"
#include "deskflow/OptionTypes.h" #include "deskflow/OptionTypes.h"
#include "deskflow/ProtocolTypes.h" #include "deskflow/ProtocolTypes.h"
#include "deskflow/ProtocolUtil.h" #include "deskflow/ProtocolUtil.h"
#include "deskflow/StreamChunker.h" #include "deskflow/StreamChunker.h"
#include "deskflow/XDeskflow.h"
#include "io/IStream.h" #include "io/IStream.h"
#include <algorithm> #include <algorithm>
@ -111,7 +111,7 @@ void ServerProxy::handleData()
case Disconnect: case Disconnect:
return; return;
} }
} catch (const XBadClient &e) { } catch (const BadClientException &e) {
LOG_ERR("protocol error from server: %s", e.what()); LOG_ERR("protocol error from server: %s", e.what());
ProtocolUtil::writef(m_stream, kMsgEBad); ProtocolUtil::writef(m_stream, kMsgEBad);
m_client->disconnect("invalid message from server"); m_client->disconnect("invalid message from server");

View file

@ -15,8 +15,8 @@
#include "common/Constants.h" #include "common/Constants.h"
#include "deskflow/ArgsBase.h" #include "deskflow/ArgsBase.h"
#include "deskflow/Config.h" #include "deskflow/Config.h"
#include "deskflow/DeskflowException.h"
#include "deskflow/ProtocolTypes.h" #include "deskflow/ProtocolTypes.h"
#include "deskflow/XDeskflow.h"
#if SYSAPI_WIN32 #if SYSAPI_WIN32
#include "base/IEventQueue.h" #include "base/IEventQueue.h"
@ -92,7 +92,7 @@ int App::run(int argc, char **argv)
try { try {
result = appUtil().run(argc, argv); result = appUtil().run(argc, argv);
} catch (XExitApp &e) { } catch (ExitAppException &e) {
// instead of showing a nasty error, just exit with the error code. // 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 // not sure if i like this behaviour, but it's probably better than
// using the exit(int) function! // using the exit(int) function!

View file

@ -7,8 +7,8 @@
#pragma once #pragma once
#include "deskflow/DeskflowException.h"
#include "deskflow/IAppUtil.h" #include "deskflow/IAppUtil.h"
#include "deskflow/XDeskflow.h"
class AppUtil : public IAppUtil class AppUtil : public IAppUtil
{ {
@ -20,7 +20,7 @@ public:
IApp &app() const override; IApp &app() const override;
virtual void exitApp(int code) virtual void exitApp(int code)
{ {
throw XExitApp(code); throw ExitAppException(code);
} }
static AppUtil &instance(); static AppUtil &instance();

View file

@ -85,6 +85,8 @@ add_library(${lib_name} STATIC ${PLATFORM_CODE}
Config.h Config.h
DaemonApp.cpp DaemonApp.cpp
DaemonApp.h DaemonApp.h
DeskflowException.cpp
DeskflowException.h
DisplayInvalidException.h DisplayInvalidException.h
IApp.h IApp.h
IAppUtil.h IAppUtil.h
@ -127,8 +129,6 @@ add_library(${lib_name} STATIC ${PLATFORM_CODE}
ServerArgs.h ServerArgs.h
StreamChunker.cpp StreamChunker.cpp
StreamChunker.h StreamChunker.h
XDeskflow.cpp
XDeskflow.h
languages/LanguageManager.cpp languages/LanguageManager.cpp
languages/LanguageManager.h languages/LanguageManager.h
ipc/DaemonIpcServer.cpp ipc/DaemonIpcServer.cpp

View file

@ -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());
}

View file

@ -1,5 +1,6 @@
/* /*
* Deskflow -- mouse and keyboard sharing utility * Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2025 Deskflow Developers
* SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd. * SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
* SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman * SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
@ -10,30 +11,30 @@
#include "base/XBase.h" #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; 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: protected:
std::string getWhat() const throw() override; 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: protected:
std::string getWhat() const throw() override; std::string getWhat() const throw() override;
@ -43,10 +44,10 @@ protected:
/*! /*!
Thrown when a client attempting to connect has an incompatible version. Thrown when a client attempting to connect has an incompatible version.
*/ */
class XIncompatibleClient : public XDeskflow class IncompatibleClientException : public DeskflowException
{ {
public: public:
XIncompatibleClient(int major, int minor); IncompatibleClientException(int major, int minor);
//! @name accessors //! @name accessors
//@{ //@{
@ -71,11 +72,11 @@ private:
Thrown when a client attempting to connect is using the same name as Thrown when a client attempting to connect is using the same name as
a client that is already connected. a client that is already connected.
*/ */
class XDuplicateClient : public XDeskflow class DuplicateClientException : public DeskflowException
{ {
public: public:
explicit XDuplicateClient(const std::string &name); explicit DuplicateClientException(const std::string &name);
~XDuplicateClient() throw() override = default; ~DuplicateClientException() throw() override = default;
//! @name accessors //! @name accessors
//@{ //@{
@ -97,11 +98,11 @@ private:
Thrown when a client attempting to connect is using a name that is Thrown when a client attempting to connect is using a name that is
unknown to the server. unknown to the server.
*/ */
class XUnknownClient : public XDeskflow class UnknownClientException : public DeskflowException
{ {
public: public:
explicit XUnknownClient(const std::string &name); explicit UnknownClientException(const std::string &name);
~XUnknownClient() throw() override = default; ~UnknownClientException() throw() override = default;
//! @name accessors //! @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 little bit of a hack, but it's a better way of exiting, than just calling
exit(int). exit(int).
*/ */
class XExitApp : public XDeskflow class ExitAppException : public DeskflowException
{ {
public: public:
explicit XExitApp(int code); explicit ExitAppException(int code);
~XExitApp() throw() override = default; ~ExitAppException() throw() override = default;
//! Get the exit code //! Get the exit code
int getCode() const noexcept; int getCode() const noexcept;

View file

@ -7,8 +7,8 @@
#include "deskflow/ProtocolUtil.h" #include "deskflow/ProtocolUtil.h"
#include "base/Log.h" #include "base/Log.h"
#include "deskflow/DeskflowException.h"
#include "deskflow/ProtocolTypes.h" #include "deskflow/ProtocolTypes.h"
#include "deskflow/XDeskflow.h"
#include "io/IStream.h" #include "io/IStream.h"
#include <array> #include <array>
#include <iterator> #include <iterator>
@ -193,7 +193,7 @@ void ProtocolUtil::vreadf(deskflow::IStream *stream, const char *fmt, va_list ar
if (len > PROTOCOL_MAX_STRING_LENGTH) { if (len > PROTOCOL_MAX_STRING_LENGTH) {
LOG_ERR("read: string length exceeds maximum allowed size: %u", len); 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); readBytes(stream, len, destination);
@ -507,7 +507,7 @@ uint32_t ProtocolUtil::readVectorSize(deskflow::IStream *stream)
if (size > PROTOCOL_MAX_LIST_LENGTH) { if (size > PROTOCOL_MAX_LIST_LENGTH) {
LOG_ERR("readVectorSize: vector length exceeds maximum allowed size: %u", size); 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; return size;

View file

@ -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());
}

View file

@ -17,8 +17,8 @@
#include "common/Constants.h" #include "common/Constants.h"
#include "deskflow/App.h" #include "deskflow/App.h"
#include "deskflow/ArgsBase.h" #include "deskflow/ArgsBase.h"
#include "deskflow/DeskflowException.h"
#include "deskflow/Screen.h" #include "deskflow/Screen.h"
#include "deskflow/XDeskflow.h"
#include "platform/MSWindowsScreen.h" #include "platform/MSWindowsScreen.h"
#include <VersionHelpers.h> #include <VersionHelpers.h>
@ -77,7 +77,7 @@ void AppUtilWindows::exitApp(int code)
break; break;
default: default:
throw XExitApp(code); throw ExitAppException(code);
} }
} }

View file

@ -10,8 +10,8 @@
#include "base/IEventQueue.h" #include "base/IEventQueue.h"
#include "base/Log.h" #include "base/Log.h"
#include "deskflow/DeskflowException.h"
#include "deskflow/ProtocolUtil.h" #include "deskflow/ProtocolUtil.h"
#include "deskflow/XDeskflow.h"
#include "io/IStream.h" #include "io/IStream.h"
#include <cstring> #include <cstring>
@ -131,7 +131,7 @@ void ClientProxy1_0::handleData()
while (getStream()->read(nullptr, 4)) 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()); LOG_ERR("protocol error from client \"%s\": %s", getName().c_str(), e.what());
disconnect(); disconnect();
return; return;

View file

@ -11,9 +11,9 @@
#include "base/IEventQueue.h" #include "base/IEventQueue.h"
#include "base/Log.h" #include "base/Log.h"
#include "deskflow/AppUtil.h" #include "deskflow/AppUtil.h"
#include "deskflow/DeskflowException.h"
#include "deskflow/ProtocolTypes.h" #include "deskflow/ProtocolTypes.h"
#include "deskflow/ProtocolUtil.h" #include "deskflow/ProtocolUtil.h"
#include "deskflow/XDeskflow.h"
#include "io/IOException.h" #include "io/IOException.h"
#include "io/IStream.h" #include "io/IStream.h"
#include "server/ClientProxy1_0.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 // hangup (with error) if version isn't supported
if (m_proxy == nullptr) { 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 // limit the maximum length of the hello
if (uint32_t n = m_stream->getSize(); n > kMaxHelloLength) { if (uint32_t n = m_stream->getSize(); n > kMaxHelloLength) {
LOG_DEBUG1("hello reply too long"); LOG_DEBUG1("hello reply too long");
throw XBadClient(); throw BadClientException();
} }
// parse the reply to hello // parse the reply to hello
int16_t major; int16_t major;
int16_t minor; int16_t minor;
if (std::string protocolName; !ProtocolUtil::readf(m_stream, kMsgHelloBack, &protocolName, &major, &minor, &name)) { if (std::string protocolName; !ProtocolUtil::readf(m_stream, kMsgHelloBack, &protocolName, &major, &minor, &name)) {
throw XBadClient(); throw BadClientException();
} }
// disallow invalid version numbers // disallow invalid version numbers
if (major <= 0 || minor < 0) { if (major <= 0 || minor < 0) {
throw XIncompatibleClient(major, minor); throw IncompatibleClientException(major, minor);
} }
// remove stream event handlers. the proxy we're about to create // 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 // wait until the proxy signals that it's ready or has disconnected
addProxyHandlers(); addProxyHandlers();
return; return;
} catch (XIncompatibleClient &e) { } catch (IncompatibleClientException &e) {
// client is incompatible // client is incompatible
LOG_WARN("client \"%s\" has incompatible version %d.%d)", name.c_str(), e.getMajor(), e.getMinor()); LOG_WARN("client \"%s\" has incompatible version %d.%d)", name.c_str(), e.getMajor(), e.getMinor());
ProtocolUtil::writef(m_stream, kMsgEIncompatible, kProtocolMajorVersion, kProtocolMinorVersion); ProtocolUtil::writef(m_stream, kMsgEIncompatible, kProtocolMajorVersion, kProtocolMinorVersion);
} catch (XBadClient &) { } catch (BadClientException &) {
// client not behaving // client not behaving
LOG_WARN("protocol error from client \"%s\"", name.c_str()); LOG_WARN("protocol error from client \"%s\"", name.c_str());
ProtocolUtil::writef(m_stream, kMsgEBad); ProtocolUtil::writef(m_stream, kMsgEBad);

View file

@ -8,10 +8,10 @@
#include "server/Config.h" #include "server/Config.h"
#include "base/IEventQueue.h" #include "base/IEventQueue.h"
#include "deskflow/DeskflowException.h"
#include "deskflow/KeyMap.h" #include "deskflow/KeyMap.h"
#include "deskflow/KeyTypes.h" #include "deskflow/KeyTypes.h"
#include "deskflow/OptionTypes.h" #include "deskflow/OptionTypes.h"
#include "deskflow/XDeskflow.h"
#include "net/SocketException.h" #include "net/SocketException.h"
#include "server/Server.h" #include "server/Server.h"
@ -1313,7 +1313,7 @@ std::string Config::getOptionValue(OptionID id, OptionValue value)
} else if (enumValue == Barrier) { } else if (enumValue == Barrier) {
return kBarrierProtocolOption; return kBarrierProtocolOption;
} else { } else {
throw XInvalidProtocol(); throw InvalidProtocolException();
} }
} }

View file

@ -13,13 +13,13 @@
#include "base/Log.h" #include "base/Log.h"
#include "base/TMethodJob.h" #include "base/TMethodJob.h"
#include "deskflow/AppUtil.h" #include "deskflow/AppUtil.h"
#include "deskflow/DeskflowException.h"
#include "deskflow/IPlatformScreen.h" #include "deskflow/IPlatformScreen.h"
#include "deskflow/OptionTypes.h" #include "deskflow/OptionTypes.h"
#include "deskflow/PacketStreamFilter.h" #include "deskflow/PacketStreamFilter.h"
#include "deskflow/ProtocolTypes.h" #include "deskflow/ProtocolTypes.h"
#include "deskflow/Screen.h" #include "deskflow/Screen.h"
#include "deskflow/StreamChunker.h" #include "deskflow/StreamChunker.h"
#include "deskflow/XDeskflow.h"
#include "mt/Thread.h" #include "mt/Thread.h"
#include "net/TCPSocket.h" #include "net/TCPSocket.h"
#include "server/ClientListener.h" #include "server/ClientListener.h"
@ -280,7 +280,7 @@ std::string Server::protocolString() const
} else if (m_protocol == Barrier) { } else if (m_protocol == Barrier) {
return kBarrierProtocolName; return kBarrierProtocolName;
} }
throw XInvalidProtocol(); throw InvalidProtocolException();
} }
uint32_t Server::getNumClients() const uint32_t Server::getNumClients() const
@ -1080,7 +1080,7 @@ void Server::processOptions()
} else if (enumValue == Barrier) { } else if (enumValue == Barrier) {
m_protocol = Barrier; m_protocol = Barrier;
} else { } else {
throw XInvalidProtocol(); throw InvalidProtocolException();
} }
} else if (id == kOptionScreenSwitchDelay) { } else if (id == kOptionScreenSwitchDelay) {
m_switchWaitDelay = 1.0e-3 * static_cast<double>(value); m_switchWaitDelay = 1.0e-3 * static_cast<double>(value);