refactor: XSocket::EError => XSocket::SocketError enum class

This commit is contained in:
sithlord48 2025-07-08 10:21:13 -04:00 committed by Chris Rizzitello
parent 94be5f7498
commit 6ca980e3e5
4 changed files with 24 additions and 23 deletions

View file

@ -87,7 +87,7 @@ void ClientApp::parseArgs(int argc, const char *const *argv)
// we'll try to resolve the address each time we connect to the // we'll try to resolve the address each time we connect to the
// server. a bad port will never get better. patch by Brent // server. a bad port will never get better. patch by Brent
// Priddy. // Priddy.
if (!args().m_restartable || e.getError() == XSocketAddress::kBadPort) { if (!args().m_restartable || e.getError() == XSocketAddress::SocketError::BadPort) {
LOG((CLOG_CRIT "%s: %s" BYE, args().m_pname, e.what(), args().m_pname)); LOG((CLOG_CRIT "%s: %s" BYE, args().m_pname, e.what(), args().m_pname));
m_bye(s_exitFailed); m_bye(s_exitFailed);
} }

View file

@ -43,7 +43,7 @@ NetworkAddress::NetworkAddress(const std::string &hostname, int port) : m_hostna
try { try {
m_port = std::stoi(m_hostname.substr(hostIt + 1)); m_port = std::stoi(m_hostname.substr(hostIt + 1));
} catch (...) { } catch (...) {
throw XSocketAddress(XSocketAddress::kBadPort, m_hostname, m_port); throw XSocketAddress(XSocketAddress::SocketError::BadPort, m_hostname, m_port);
} }
auto endHostnameIt = static_cast<int>(hostIt); 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 // bad syntax of ipv6 with port
if (hostIt == std::string::npos) { if (hostIt == std::string::npos) {
throw XSocketAddress(XSocketAddress::kUnknown, m_hostname, m_port); throw XSocketAddress(XSocketAddress::SocketError::Unknown, m_hostname, m_port);
} }
auto portSuffix = m_hostname.substr(hostIt + portDelimeter.size()); auto portSuffix = m_hostname.substr(hostIt + portDelimeter.size());
// port is implied but omitted // port is implied but omitted
if (portSuffix.empty()) { if (portSuffix.empty()) {
throw XSocketAddress(XSocketAddress::kBadPort, m_hostname, m_port); throw XSocketAddress(XSocketAddress::SocketError::BadPort, m_hostname, m_port);
} }
try { try {
m_port = std::stoi(portSuffix); m_port = std::stoi(portSuffix);
} catch (...) { } catch (...) {
// port is not a number // port is not a number
throw XSocketAddress(XSocketAddress::kBadPort, m_hostname, m_port); throw XSocketAddress(XSocketAddress::SocketError::BadPort, m_hostname, m_port);
} }
auto endHostnameIt = static_cast<int>(hostIt) - 1; 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 // ensure that ipv6 link-local adress ended with scope id
if (m_hostname.rfind("fe80:", 0) == 0 && m_hostname.find('%') == std::string::npos) { if (m_hostname.rfind("fe80:", 0) == 0 && m_hostname.find('%') == std::string::npos) {
throw XSocketAddress(XSocketAddress::kUnknown, m_hostname, m_port); throw XSocketAddress(XSocketAddress::SocketError::Unknown, m_hostname, m_port);
} }
} }
@ -156,13 +156,13 @@ size_t NetworkAddress::resolve(size_t index)
} }
} }
} catch (XArchNetworkNameUnknown &) { } catch (XArchNetworkNameUnknown &) {
throw XSocketAddress(XSocketAddress::kNotFound, m_hostname, m_port); throw XSocketAddress(XSocketAddress::SocketError::NotFound, m_hostname, m_port);
} catch (XArchNetworkNameNoAddress &) { } catch (XArchNetworkNameNoAddress &) {
throw XSocketAddress(XSocketAddress::kNoAddress, m_hostname, m_port); throw XSocketAddress(XSocketAddress::SocketError::NoAddress, m_hostname, m_port);
} catch (XArchNetworkNameUnsupported &) { } catch (XArchNetworkNameUnsupported &) {
throw XSocketAddress(XSocketAddress::kUnsupported, m_hostname, m_port); throw XSocketAddress(XSocketAddress::SocketError::Unsupported, m_hostname, m_port);
} catch (XArchNetworkName &) { } catch (XArchNetworkName &) {
throw XSocketAddress(XSocketAddress::kUnknown, m_hostname, m_port); throw XSocketAddress(XSocketAddress::SocketError::Unknown, m_hostname, m_port);
} }
// set port in address // set port in address
@ -205,6 +205,6 @@ void NetworkAddress::checkPort() const
{ {
// check port number // check port number
if (m_port < 0 || m_port > 65535) { if (m_port < 0 || m_port > 65535) {
throw XSocketAddress(XSocketAddress::kBadPort, m_hostname, m_port); throw XSocketAddress(XSocketAddress::SocketError::BadPort, m_hostname, m_port);
} }
} }

View file

@ -12,7 +12,7 @@
// XSocketAddress // XSocketAddress
// //
XSocketAddress::XSocketAddress(EError error, const std::string &hostname, int port) noexcept XSocketAddress::XSocketAddress(SocketError error, const std::string &hostname, int port) noexcept
: m_error(error), : m_error(error),
m_hostname(hostname), m_hostname(hostname),
m_port(port) m_port(port)
@ -20,7 +20,7 @@ XSocketAddress::XSocketAddress(EError error, const std::string &hostname, int po
// do nothing // do nothing
} }
XSocketAddress::EError XSocketAddress::getError() const noexcept XSocketAddress::SocketError XSocketAddress::getError() const noexcept
{ {
return m_error; return m_error;
} }
@ -46,8 +46,9 @@ std::string XSocketAddress::getWhat() const throw()
"unsupported address for: %{1}", "unsupported address for: %{1}",
"invalid port" // m_port may not be set to the bad port "invalid port" // m_port may not be set to the bad port
}; };
const auto index = static_cast<int>(m_error);
return format( return format(
s_errorID[m_error], s_errorMsg[m_error], m_hostname.c_str(), deskflow::string::sprintf("%d", m_port).c_str() s_errorID[index], s_errorMsg[index], m_hostname.c_str(), deskflow::string::sprintf("%d", m_port).c_str()
); );
} }

View file

@ -27,23 +27,23 @@ class XSocketAddress : public XSocket
{ {
public: public:
//! Failure codes //! Failure codes
enum EError enum class SocketError
{ {
kUnknown, //!< Unknown error Unknown, //!< Unknown error
kNotFound, //!< The hostname is unknown NotFound, //!< The hostname is unknown
kNoAddress, //!< The hostname is valid but has no IP address NoAddress, //!< The hostname is valid but has no IP address
kUnsupported, //!< The hostname is valid but has no supported address Unsupported, //!< The hostname is valid but has no supported address
kBadPort //!< The port is invalid BadPort //!< The port is invalid
}; };
XSocketAddress(EError, const std::string &hostname, int port) noexcept; XSocketAddress(SocketError, const std::string &hostname, int port) noexcept;
~XSocketAddress() throw() override = default; ~XSocketAddress() throw() override = default;
//! @name accessors //! @name accessors
//@{ //@{
//! Get the error code //! Get the error code
EError getError() const noexcept; SocketError getError() const noexcept;
//! Get the hostname //! Get the hostname
std::string getHostname() const noexcept; std::string getHostname() const noexcept;
//! Get the port //! Get the port
@ -56,7 +56,7 @@ protected:
std::string getWhat() const throw() override; std::string getWhat() const throw() override;
private: private:
EError m_error; SocketError m_error;
std::string m_hostname; std::string m_hostname;
int m_port; int m_port;
}; };