refactor: XSocket Classes => XSocketException Classes

This commit is contained in:
sithlord48
2025-08-07 16:52:32 -04:00
committed by Nick Bolton
parent 906c07e4fd
commit ada2813f9f
13 changed files with 166 additions and 164 deletions

View File

@ -20,9 +20,9 @@
#include "deskflow/Screen.h"
#include "deskflow/XScreen.h"
#include "net/NetworkAddress.h"
#include "net/SocketException.h"
#include "net/SocketMultiplexer.h"
#include "net/TCPSocketFactory.h"
#include "net/XSocket.h"
#include "platform/Wayland.h"
#if SYSAPI_WIN32
@ -82,12 +82,12 @@ void ClientApp::parseArgs(int argc, const char *const *argv)
try {
*m_serverAddress = NetworkAddress(args().m_serverAddress, kDefaultPort);
m_serverAddress->resolve();
} catch (XSocketAddress &e) {
} catch (SocketAddressException &e) {
// allow an address that we can't look up if we're restartable.
// 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() == XSocketAddress::SocketError::BadPort) {
if (!args().m_restartable || e.getError() == SocketAddressException::SocketError::BadPort) {
LOG_CRIT("%s: %s" BYE, args().m_pname, e.what(), args().m_pname);
bye(s_exitFailed);
}

View File

@ -17,9 +17,9 @@
#include "deskflow/Screen.h"
#include "deskflow/ServerArgs.h"
#include "deskflow/XScreen.h"
#include "net/SocketException.h"
#include "net/SocketMultiplexer.h"
#include "net/TCPSocketFactory.h"
#include "net/XSocket.h"
#include "server/ClientListener.h"
#include "server/ClientProxy.h"
#include "server/Config.h"
@ -89,7 +89,7 @@ void ServerApp::parseArgs(int argc, const char *const *argv)
try {
*m_deskflowAddress = NetworkAddress(args().m_deskflowAddress, kDefaultPort);
m_deskflowAddress->resolve();
} catch (XSocketAddress &e) {
} catch (SocketAddressException &e) {
LOG_CRIT("%s: %s" BYE, args().m_pname, e.what(), args().m_pname);
bye(s_exitArgs);
}
@ -445,7 +445,7 @@ bool ServerApp::startServer()
LOG_NOTE("started server, waiting for clients");
m_serverState = Started;
return true;
} catch (XSocketAddressInUse &e) {
} catch (SocketAddressInUseException &e) {
if (args().m_restartable) {
LOG_ERR("cannot listen for clients: %s", e.what());
} else {

View File

@ -30,6 +30,8 @@ add_library(net STATIC
SecurityLevel.h
SecureSocket.cpp
SecureSocket.h
SocketException.cpp
SocketException.h
SocketMultiplexer.cpp
SocketMultiplexer.h
SecureUtils.cpp
@ -43,8 +45,6 @@ add_library(net STATIC
TCPSocketFactory.cpp
TCPSocketFactory.h
TSocketMultiplexerMethodJob.h
XSocket.cpp
XSocket.h
)
target_link_libraries(

View File

@ -9,7 +9,7 @@
#include "arch/Arch.h"
#include "arch/XArch.h"
#include "net/XSocket.h"
#include "net/SocketException.h"
#include <algorithm>
#include <cstdlib>
@ -43,7 +43,7 @@ NetworkAddress::NetworkAddress(const std::string &hostname, int port) : m_hostna
try {
m_port = std::stoi(m_hostname.substr(hostIt + 1));
} catch (...) {
throw XSocketAddress(XSocketAddress::SocketError::BadPort, m_hostname, m_port);
throw SocketAddressException(SocketAddressException::SocketError::BadPort, m_hostname, m_port);
}
auto endHostnameIt = static_cast<int>(hostIt);
@ -57,19 +57,19 @@ NetworkAddress::NetworkAddress(const std::string &hostname, int port) : m_hostna
// bad syntax of ipv6 with port
if (hostIt == std::string::npos) {
throw XSocketAddress(XSocketAddress::SocketError::Unknown, m_hostname, m_port);
throw SocketAddressException(SocketAddressException::SocketError::Unknown, m_hostname, m_port);
}
auto portSuffix = m_hostname.substr(hostIt + portDelimeter.size());
// port is implied but omitted
if (portSuffix.empty()) {
throw XSocketAddress(XSocketAddress::SocketError::BadPort, m_hostname, m_port);
throw SocketAddressException(SocketAddressException::SocketError::BadPort, m_hostname, m_port);
}
try {
m_port = std::stoi(portSuffix);
} catch (...) {
// port is not a number
throw XSocketAddress(XSocketAddress::SocketError::BadPort, m_hostname, m_port);
throw SocketAddressException(SocketAddressException::SocketError::BadPort, m_hostname, m_port);
}
auto endHostnameIt = static_cast<int>(hostIt) - 1;
@ -78,7 +78,7 @@ NetworkAddress::NetworkAddress(const std::string &hostname, int port) : m_hostna
// ensure that ipv6 link-local adress ended with scope id
if (m_hostname.rfind("fe80:", 0) == 0 && m_hostname.find('%') == std::string::npos) {
throw XSocketAddress(XSocketAddress::SocketError::Unknown, m_hostname, m_port);
throw SocketAddressException(SocketAddressException::SocketError::Unknown, m_hostname, m_port);
}
}
@ -156,13 +156,13 @@ size_t NetworkAddress::resolve(size_t index)
}
}
} catch (XArchNetworkNameUnknown &) {
throw XSocketAddress(XSocketAddress::SocketError::NotFound, m_hostname, m_port);
throw SocketAddressException(SocketAddressException::SocketError::NotFound, m_hostname, m_port);
} catch (XArchNetworkNameNoAddress &) {
throw XSocketAddress(XSocketAddress::SocketError::NoAddress, m_hostname, m_port);
throw SocketAddressException(SocketAddressException::SocketError::NoAddress, m_hostname, m_port);
} catch (XArchNetworkNameUnsupported &) {
throw XSocketAddress(XSocketAddress::SocketError::Unsupported, m_hostname, m_port);
throw SocketAddressException(SocketAddressException::SocketError::Unsupported, m_hostname, m_port);
} catch (XArchNetworkName &) {
throw XSocketAddress(XSocketAddress::SocketError::Unknown, m_hostname, m_port);
throw SocketAddressException(SocketAddressException::SocketError::Unknown, m_hostname, m_port);
}
// set port in address
@ -200,6 +200,6 @@ void NetworkAddress::checkPort() const
{
// check port number
if (m_port < 0 || m_port > 65535) {
throw XSocketAddress(XSocketAddress::SocketError::BadPort, m_hostname, m_port);
throw SocketAddressException(SocketAddressException::SocketError::BadPort, m_hostname, m_port);
}
}

View File

@ -34,8 +34,8 @@ public:
it's used, otherwise it's used as a host name. If \c hostname ends
in ":[0-9]+" then that suffix is extracted and used as the port,
overridding the port parameter. The resulting port must be a valid
port number (zero is not a valid port number) otherwise \c XSocketAddress
is thrown with an error of \c XSocketAddress::kBadPort. The hostname
port number (zero is not a valid port number) otherwise \c SocketAddressException
is thrown with an error of \c SocketAddressException::kBadPort. The hostname
is not resolved by the c'tor; use \c resolve to do that.
*/
explicit NetworkAddress(const std::string &hostname, int port = 0);
@ -53,7 +53,7 @@ public:
/*!
Resolves the hostname to an address. This can be done any number of
times and is done automatically by the c'tor taking a hostname.
Throws XSocketAddress if resolution is unsuccessful, after which
Throws SocketAddressException if resolution is unsuccessful, after which
\c isValid returns false until the next call to this method.
index - determine index of IP we would like to use from resolved addresses
Returns count of successfully resolved addressed.

View File

@ -9,8 +9,8 @@
#include "io/Filesystem.h"
#include "net/SecurityLevel.h"
#include "net/SocketException.h"
#include "net/TCPSocket.h"
#include "net/XSocket.h"
#include <memory>
#include <mutex>

View File

@ -0,0 +1,99 @@
/*
* 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 "net/SocketException.h"
#include "base/String.h"
//
// SocketAddressException
//
SocketAddressException::SocketAddressException(SocketError error, const std::string &hostname, int port) noexcept
: m_error(error),
m_hostname(hostname),
m_port(port)
{
// do nothing
}
SocketAddressException::SocketError SocketAddressException::getError() const noexcept
{
return m_error;
}
std::string SocketAddressException::getHostname() const noexcept
{
return m_hostname;
}
int SocketAddressException::getPort() const noexcept
{
return m_port;
}
std::string SocketAddressException::getWhat() const throw()
{
static const char *s_errorID[] = {
"SocketAddressUnknownException", "SocketAddressNotFoundException", "SocketAddressNoAddressException",
"SocketAddressUnsupportedException", "SocketAddressBadPortException"
};
static const char *s_errorMsg[] = {
"unknown error for: %{1}:%{2}", "address not found for: %{1}", "no address for: %{1}",
"unsupported address for: %{1}",
"invalid port" // m_port may not be set to the bad port
};
const auto index = static_cast<int>(m_error);
return format(
s_errorID[index], s_errorMsg[index], m_hostname.c_str(), deskflow::string::sprintf("%d", m_port).c_str()
);
}
//
// SocketIOCloseException
//
std::string SocketIOCloseException::getWhat() const throw()
{
return format("SocketIOCloseException", "close: %{1}", what());
}
//
// SocketBindException
//
std::string SocketBindException::getWhat() const throw()
{
return format("SocketBindException", "cannot bind address: %{1}", what());
}
//
// SocketAddressInUseException
//
std::string SocketAddressInUseException::getWhat() const throw()
{
return format("SocketAddressInUseException", "cannot bind address: %{1}", what());
}
//
// SocketConnectException
//
std::string SocketConnectException::getWhat() const throw()
{
return format("SocketConnectException", "cannot connect socket: %{1}", what());
}
//
// SocketCreateException
//
std::string SocketCreateException::getWhat() const throw()
{
return format("SocketCreateException", "cannot create socket: %{1}", what());
}

View File

@ -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
@ -12,9 +13,9 @@
#include "io/XIO.h"
/**
* @brief XSocket generic socket exception
* @brief SocketException generic socket exception
*/
class XSocket : public XBase
class SocketException : public XBase
{
using XBase::XBase;
};
@ -23,7 +24,7 @@ class XSocket : public XBase
/*!
Thrown when attempting to create an invalid network address.
*/
class XSocketAddress : public XSocket
class SocketAddressException : public SocketException
{
public:
//! Failure codes
@ -36,8 +37,8 @@ public:
BadPort //!< The port is invalid
};
XSocketAddress(SocketError, const std::string &hostname, int port) noexcept;
~XSocketAddress() throw() override = default;
SocketAddressException(SocketError, const std::string &hostname, int port) noexcept;
~SocketAddressException() throw() override = default;
//! @name accessors
//@{
@ -62,20 +63,20 @@ private:
};
/**
* @brief XSocketIOClose - Thrown if a stream cannot be closed.
* @brief SocketIOCloseException - Thrown if a stream cannot be closed.
*/
class XSocketIOClose : public XIOClose
class SocketIOCloseException : public XIOClose
{
public:
XSocketIOClose() : XIOClose(), m_state(kDone)
SocketIOCloseException() : XIOClose(), m_state(kDone)
{
// do nothing
}
explicit XSocketIOClose(const std::string &msg) : XIOClose(msg), m_state(kFirst)
explicit SocketIOCloseException(const std::string &msg) : XIOClose(msg), m_state(kFirst)
{
// do nothing
}
~XSocketIOClose() throw() override = default;
~SocketIOCloseException() throw() override = default;
const char *what() const throw() override
{
@ -106,20 +107,20 @@ private:
};
/**
* @brief XSocketWithWhat - generic XSocket Exception with a generic `what` method impl
* @brief SocketWithWhatException - generic SocketException Exception with a generic `what` method impl
*/
class XSocketWithWhat : public XSocket
class SocketWithWhatException : public SocketException
{
public:
XSocketWithWhat() : XSocket(), m_state(kDone)
SocketWithWhatException() : SocketException(), m_state(kDone)
{
// do nothing
}
explicit XSocketWithWhat(const std::string &msg) : XSocket(msg), m_state(kFirst)
explicit SocketWithWhatException(const std::string &msg) : SocketException(msg), m_state(kFirst)
{
// do nothing
}
~XSocketWithWhat() throw() override = default;
~SocketWithWhatException() throw() override = default;
const char *what() const throw() override
{
@ -131,7 +132,7 @@ public:
if (m_state == kDone) {
return m_formatted.c_str();
} else {
return XSocket::what();
return SocketException::what();
}
}
@ -147,45 +148,45 @@ private:
};
/**
* @brief XSocketBind - Thrown when a socket cannot be bound to an address.
* @brief SocketBindException - Thrown when a socket cannot be bound to an address.
*/
class XSocketBind : public XSocketWithWhat
class SocketBindException : public SocketWithWhatException
{
using XSocketWithWhat::XSocketWithWhat;
using SocketWithWhatException::SocketWithWhatException;
protected:
std::string getWhat() const throw() override;
};
/**
* @brief XSocketAddressInUse
* @brief SocketAddressInUseException
* Thrown when a socket cannot be bound to an address because the address is already in use.
*/
class XSocketAddressInUse : public XSocketWithWhat
class SocketAddressInUseException : public SocketWithWhatException
{
using XSocketWithWhat::XSocketWithWhat;
using SocketWithWhatException::SocketWithWhatException;
protected:
std::string getWhat() const throw() override;
};
/**
* @brief XSocketConnect - Thrown when a socket cannot connect to a remote endpoint.
* @brief SocketConnectException - Thrown when a socket cannot connect to a remote endpoint.
*/
class XSocketConnect : public XSocketWithWhat
class SocketConnectException : public SocketWithWhatException
{
using XSocketWithWhat::XSocketWithWhat;
using SocketWithWhatException::SocketWithWhatException;
protected:
std::string getWhat() const throw() override;
};
/**
* @brief XSocketConnect - Thrown when a socket cannot be created (by the operating system).
* @brief SocketCreateException - Thrown when a socket cannot be created (by the operating system).
*/
class XSocketCreate : public XSocketWithWhat
class SocketCreateException : public SocketWithWhatException
{
using XSocketWithWhat::XSocketWithWhat;
using SocketWithWhatException::SocketWithWhatException;
protected:
std::string getWhat() const throw() override;

View File

@ -13,10 +13,10 @@
#include "base/Log.h"
#include "io/XIO.h"
#include "net/NetworkAddress.h"
#include "net/SocketException.h"
#include "net/SocketMultiplexer.h"
#include "net/TCPSocket.h"
#include "net/TSocketMultiplexerMethodJob.h"
#include "net/XSocket.h"
//
// TCPListenSocket
@ -31,7 +31,7 @@ TCPListenSocket::TCPListenSocket(
try {
m_socket = ARCH->newSocket(family, IArchNetwork::SocketType::Stream);
} catch (XArchNetwork &e) {
throw XSocketCreate(e.what());
throw SocketCreateException(e.what());
}
}
@ -61,9 +61,9 @@ void TCPListenSocket::bind(const NetworkAddress &addr)
)
);
} catch (XArchNetworkAddressInUse &e) {
throw XSocketAddressInUse(e.what());
throw SocketAddressInUseException(e.what());
} catch (XArchNetwork &e) {
throw XSocketBind(e.what());
throw SocketBindException(e.what());
}
}
@ -78,7 +78,7 @@ void TCPListenSocket::close()
ARCH->closeSocket(m_socket);
m_socket = nullptr;
} catch (XArchNetwork &e) {
throw XSocketIOClose(e.what());
throw SocketIOCloseException(e.what());
}
}

View File

@ -13,9 +13,9 @@
#include "base/Log.h"
#include "mt/Lock.h"
#include "net/NetworkAddress.h"
#include "net/SocketException.h"
#include "net/SocketMultiplexer.h"
#include "net/TSocketMultiplexerMethodJob.h"
#include "net/XSocket.h"
#include <cstdlib>
#include <cstring>
@ -36,7 +36,7 @@ TCPSocket::TCPSocket(IEventQueue *events, SocketMultiplexer *socketMultiplexer,
try {
m_socket = ARCH->newSocket(family, IArchNetwork::SocketType::Stream);
} catch (const XArchNetwork &e) {
throw XSocketCreate(e.what());
throw SocketCreateException(e.what());
}
LOG_DEBUG("opening new socket: %08X", m_socket);
@ -76,9 +76,9 @@ void TCPSocket::bind(const NetworkAddress &addr)
try {
ARCH->bindSocket(m_socket, addr.getAddress());
} catch (const XArchNetworkAddressInUse &e) {
throw XSocketAddressInUse(e.what());
throw SocketAddressInUseException(e.what());
} catch (const XArchNetwork &e) {
throw XSocketBind(e.what());
throw SocketBindException(e.what());
}
}
@ -266,7 +266,7 @@ void TCPSocket::connect(const NetworkAddress &addr)
m_writable = true;
}
} catch (const XArchNetwork &e) {
throw XSocketConnect(e.what());
throw SocketConnectException(e.what());
}
}
setJob(newJob());
@ -292,7 +292,7 @@ void TCPSocket::init()
// ignore, there's not much we can do
LOG_WARN("error closing socket: %s", e.what());
}
throw XSocketCreate(e.what());
throw SocketCreateException(e.what());
}
}

View File

@ -1,98 +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 "net/XSocket.h"
#include "base/String.h"
//
// XSocketAddress
//
XSocketAddress::XSocketAddress(SocketError error, const std::string &hostname, int port) noexcept
: m_error(error),
m_hostname(hostname),
m_port(port)
{
// do nothing
}
XSocketAddress::SocketError XSocketAddress::getError() const noexcept
{
return m_error;
}
std::string XSocketAddress::getHostname() const noexcept
{
return m_hostname;
}
int XSocketAddress::getPort() const noexcept
{
return m_port;
}
std::string XSocketAddress::getWhat() const throw()
{
static const char *s_errorID[] = {
"XSocketAddressUnknown", "XSocketAddressNotFound", "XSocketAddressNoAddress", "XSocketAddressUnsupported",
"XSocketAddressBadPort"
};
static const char *s_errorMsg[] = {
"unknown error for: %{1}:%{2}", "address not found for: %{1}", "no address for: %{1}",
"unsupported address for: %{1}",
"invalid port" // m_port may not be set to the bad port
};
const auto index = static_cast<int>(m_error);
return format(
s_errorID[index], s_errorMsg[index], m_hostname.c_str(), deskflow::string::sprintf("%d", m_port).c_str()
);
}
//
// XSocketIOClose
//
std::string XSocketIOClose::getWhat() const throw()
{
return format("XSocketIOClose", "close: %{1}", what());
}
//
// XSocketBind
//
std::string XSocketBind::getWhat() const throw()
{
return format("XSocketBind", "cannot bind address: %{1}", what());
}
//
// XSocketAddressInUse
//
std::string XSocketAddressInUse::getWhat() const throw()
{
return format("XSocketAddressInUse", "cannot bind address: %{1}", what());
}
//
// XSocketConnect
//
std::string XSocketConnect::getWhat() const throw()
{
return format("XSocketConnect", "cannot connect socket: %{1}", what());
}
//
// XSocketCreate
//
std::string XSocketCreate::getWhat() const throw()
{
return format("XSocketCreate", "cannot create socket: %{1}", what());
}

View File

@ -15,7 +15,7 @@
#include "net/IDataSocket.h"
#include "net/IListenSocket.h"
#include "net/ISocketFactory.h"
#include "net/XSocket.h"
#include "net/SocketException.h"
#include "server/ClientProxy.h"
#include "server/ClientProxyUnknown.h"
@ -36,7 +36,7 @@ ClientListener::ClientListener(
try {
start();
} catch (XSocketAddressInUse &) {
} catch (SocketAddressInUseException &) {
cleanupListenSocket();
m_socketFactory.reset();
throw;

View File

@ -12,7 +12,7 @@
#include "deskflow/KeyTypes.h"
#include "deskflow/OptionTypes.h"
#include "deskflow/XDeskflow.h"
#include "net/XSocket.h"
#include "net/SocketException.h"
#include "server/Server.h"
#include <cstdlib>
@ -643,7 +643,7 @@ void Config::readSectionOptions(ConfigReadContext &s)
try {
m_deskflowAddress = NetworkAddress(value, kDefaultPort);
m_deskflowAddress.resolve();
} catch (XSocketAddress &e) {
} catch (SocketAddressException &e) {
throw XConfigRead(s, std::string("invalid address argument ") + e.what());
}
} else if (name == "heartbeat") {