refactor: XSocket::EError => XSocket::SocketError enum class
This commit is contained in:
committed by
Chris Rizzitello
parent
94be5f7498
commit
6ca980e3e5
@ -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
|
||||
// server. a bad port will never get better. patch by Brent
|
||||
// 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));
|
||||
m_bye(s_exitFailed);
|
||||
}
|
||||
|
||||
@ -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::kBadPort, m_hostname, m_port);
|
||||
throw XSocketAddress(XSocketAddress::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::kUnknown, m_hostname, m_port);
|
||||
throw XSocketAddress(XSocketAddress::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::kBadPort, m_hostname, m_port);
|
||||
throw XSocketAddress(XSocketAddress::SocketError::BadPort, m_hostname, m_port);
|
||||
}
|
||||
try {
|
||||
m_port = std::stoi(portSuffix);
|
||||
} catch (...) {
|
||||
// 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;
|
||||
@ -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::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 &) {
|
||||
throw XSocketAddress(XSocketAddress::kNotFound, m_hostname, m_port);
|
||||
throw XSocketAddress(XSocketAddress::SocketError::NotFound, m_hostname, m_port);
|
||||
} catch (XArchNetworkNameNoAddress &) {
|
||||
throw XSocketAddress(XSocketAddress::kNoAddress, m_hostname, m_port);
|
||||
throw XSocketAddress(XSocketAddress::SocketError::NoAddress, m_hostname, m_port);
|
||||
} catch (XArchNetworkNameUnsupported &) {
|
||||
throw XSocketAddress(XSocketAddress::kUnsupported, m_hostname, m_port);
|
||||
throw XSocketAddress(XSocketAddress::SocketError::Unsupported, m_hostname, m_port);
|
||||
} catch (XArchNetworkName &) {
|
||||
throw XSocketAddress(XSocketAddress::kUnknown, m_hostname, m_port);
|
||||
throw XSocketAddress(XSocketAddress::SocketError::Unknown, m_hostname, m_port);
|
||||
}
|
||||
|
||||
// set port in address
|
||||
@ -205,6 +205,6 @@ void NetworkAddress::checkPort() const
|
||||
{
|
||||
// check port number
|
||||
if (m_port < 0 || m_port > 65535) {
|
||||
throw XSocketAddress(XSocketAddress::kBadPort, m_hostname, m_port);
|
||||
throw XSocketAddress(XSocketAddress::SocketError::BadPort, m_hostname, m_port);
|
||||
}
|
||||
}
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
// 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_hostname(hostname),
|
||||
m_port(port)
|
||||
@ -20,7 +20,7 @@ XSocketAddress::XSocketAddress(EError error, const std::string &hostname, int po
|
||||
// do nothing
|
||||
}
|
||||
|
||||
XSocketAddress::EError XSocketAddress::getError() const noexcept
|
||||
XSocketAddress::SocketError XSocketAddress::getError() const noexcept
|
||||
{
|
||||
return m_error;
|
||||
}
|
||||
@ -46,8 +46,9 @@ std::string XSocketAddress::getWhat() const throw()
|
||||
"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[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()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -27,23 +27,23 @@ class XSocketAddress : public XSocket
|
||||
{
|
||||
public:
|
||||
//! Failure codes
|
||||
enum EError
|
||||
enum class SocketError
|
||||
{
|
||||
kUnknown, //!< Unknown error
|
||||
kNotFound, //!< The hostname is unknown
|
||||
kNoAddress, //!< The hostname is valid but has no IP address
|
||||
kUnsupported, //!< The hostname is valid but has no supported address
|
||||
kBadPort //!< The port is invalid
|
||||
Unknown, //!< Unknown error
|
||||
NotFound, //!< The hostname is unknown
|
||||
NoAddress, //!< The hostname is valid but has no IP address
|
||||
Unsupported, //!< The hostname is valid but has no supported address
|
||||
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;
|
||||
|
||||
//! @name accessors
|
||||
//@{
|
||||
|
||||
//! Get the error code
|
||||
EError getError() const noexcept;
|
||||
SocketError getError() const noexcept;
|
||||
//! Get the hostname
|
||||
std::string getHostname() const noexcept;
|
||||
//! Get the port
|
||||
@ -56,7 +56,7 @@ protected:
|
||||
std::string getWhat() const throw() override;
|
||||
|
||||
private:
|
||||
EError m_error;
|
||||
SocketError m_error;
|
||||
std::string m_hostname;
|
||||
int m_port;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user