Skip to content

Commit

Permalink
Use standard library naming for exceptions
Browse files Browse the repository at this point in the history
This makes things more consistent since we mix with the standard library
exceptions so often.
  • Loading branch information
CendioOssman committed Nov 6, 2024
1 parent ed07250 commit 2b78572
Show file tree
Hide file tree
Showing 81 changed files with 388 additions and 386 deletions.
6 changes: 3 additions & 3 deletions common/network/Socket.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void network::initSockets() {
WSADATA initResult;

if (WSAStartup(requiredVersion, &initResult) != 0)
throw rdr::SocketException("unable to initialise Winsock2", errorNumber);
throw rdr::socket_error("unable to initialise Winsock2", errorNumber);
#else
signal(SIGPIPE, SIG_IGN);
#endif
Expand Down Expand Up @@ -163,7 +163,7 @@ Socket* SocketListener::accept() {

// Accept an incoming connection
if ((new_sock = ::accept(fd, nullptr, nullptr)) < 0)
throw rdr::SocketException("unable to accept new connection", errorNumber);
throw rdr::socket_error("unable to accept new connection", errorNumber);

// Create the socket object & check connection is allowed
Socket* s = createSocket(new_sock);
Expand All @@ -181,7 +181,7 @@ void SocketListener::listen(int sock)
if (::listen(sock, 5) < 0) {
int e = errorNumber;
closesocket(sock);
throw rdr::SocketException("unable to set socket to listening mode", e);
throw rdr::socket_error("unable to set socket to listening mode", e);
}

fd = sock;
Expand Down
26 changes: 13 additions & 13 deletions common/network/TcpSocket.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ int network::findFreeTcpPort (void)
addr.sin_addr.s_addr = INADDR_ANY;

if ((sock = socket (AF_INET, SOCK_STREAM, 0)) < 0)
throw SocketException ("unable to create socket", errorNumber);
throw socket_error("unable to create socket", errorNumber);

addr.sin_port = 0;
if (bind (sock, (struct sockaddr *)&addr, sizeof (addr)) < 0)
throw SocketException ("unable to find free port", errorNumber);
throw socket_error("unable to find free port", errorNumber);

socklen_t n = sizeof(addr);
if (getsockname (sock, (struct sockaddr *)&addr, &n) < 0)
throw SocketException ("unable to get port number", errorNumber);
throw socket_error("unable to get port number", errorNumber);

closesocket (sock);
return ntohs(addr.sin_port);
Expand Down Expand Up @@ -137,7 +137,7 @@ TcpSocket::TcpSocket(const char *host, int port)
hints.ai_next = nullptr;

if ((result = getaddrinfo(host, nullptr, &hints, &ai)) != 0) {
throw GAIException("unable to resolve host by name", result);
throw getaddrinfo_error("unable to resolve host by name", result);
}

sock = -1;
Expand Down Expand Up @@ -178,7 +178,7 @@ TcpSocket::TcpSocket(const char *host, int port)
if (sock == -1) {
err = errorNumber;
freeaddrinfo(ai);
throw SocketException("unable to create socket", err);
throw socket_error("unable to create socket", err);
}

/* Attempt to connect to the remote host */
Expand All @@ -205,7 +205,7 @@ TcpSocket::TcpSocket(const char *host, int port)
if (err == 0)
throw std::runtime_error("No useful address for host");
else
throw SocketException("unable to connect to socket", err);
throw socket_error("unable to connect to socket", err);
}

// Take proper ownership of the socket
Expand Down Expand Up @@ -302,15 +302,15 @@ TcpListener::TcpListener(const struct sockaddr *listenaddr,
int sock;

if ((sock = socket (listenaddr->sa_family, SOCK_STREAM, 0)) < 0)
throw SocketException("unable to create listening socket", errorNumber);
throw socket_error("unable to create listening socket", errorNumber);

memcpy (&sa, listenaddr, listenaddrlen);
#ifdef IPV6_V6ONLY
if (listenaddr->sa_family == AF_INET6) {
if (setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&one, sizeof(one))) {
int e = errorNumber;
closesocket(sock);
throw SocketException("unable to set IPV6_V6ONLY", e);
throw socket_error("unable to set IPV6_V6ONLY", e);
}
}
#endif /* defined(IPV6_V6ONLY) */
Expand All @@ -328,14 +328,14 @@ TcpListener::TcpListener(const struct sockaddr *listenaddr,
(char *)&one, sizeof(one)) < 0) {
int e = errorNumber;
closesocket(sock);
throw SocketException("unable to create listening socket", e);
throw socket_error("unable to create listening socket", e);
}
#endif

if (bind(sock, &sa.u.sa, listenaddrlen) == -1) {
int e = errorNumber;
closesocket(sock);
throw SocketException("failed to bind socket", e);
throw socket_error("failed to bind socket", e);
}

listen(sock);
Expand Down Expand Up @@ -446,7 +446,7 @@ void network::createTcpListeners(std::list<SocketListener*> *listeners,
snprintf (service, sizeof (service) - 1, "%d", port);
service[sizeof (service) - 1] = '\0';
if ((result = getaddrinfo(addr, service, &hints, &ai)) != 0)
throw GAIException("unable to resolve listening address", result);
throw getaddrinfo_error("unable to resolve listening address", result);

try {
createTcpListeners(listeners, ai);
Expand Down Expand Up @@ -485,7 +485,7 @@ void network::createTcpListeners(std::list<SocketListener*> *listeners,
try {
new_listeners.push_back(new TcpListener(current->ai_addr,
current->ai_addrlen));
} catch (SocketException& e) {
} catch (socket_error& e) {
// Ignore this if it is due to lack of address family support on
// the interface or on the system
if (e.err != EADDRNOTAVAIL && e.err != EAFNOSUPPORT) {
Expand Down Expand Up @@ -633,7 +633,7 @@ TcpFilter::Pattern TcpFilter::parsePattern(const char* p) {
}

if ((result = getaddrinfo (parts[0].c_str(), nullptr, &hints, &ai)) != 0) {
throw GAIException("unable to resolve host by name", result);
throw getaddrinfo_error("unable to resolve host by name", result);
}

memcpy (&pattern.address.u.sa, ai->ai_addr, ai->ai_addrlen);
Expand Down
14 changes: 7 additions & 7 deletions common/network/UnixSocket.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ UnixSocket::UnixSocket(const char *path)
socklen_t salen;

if (strlen(path) >= sizeof(addr.sun_path))
throw SocketException("socket path is too long", ENAMETOOLONG);
throw socket_error("socket path is too long", ENAMETOOLONG);

// - Create a socket
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock == -1)
throw SocketException("unable to create socket", errno);
throw socket_error("unable to create socket", errno);

// - Attempt to connect
memset(&addr, 0, sizeof(addr));
Expand All @@ -72,7 +72,7 @@ UnixSocket::UnixSocket(const char *path)
}

if (result == -1)
throw SocketException("unable to connect to socket", err);
throw socket_error("unable to connect to socket", err);

setFd(sock);
}
Expand Down Expand Up @@ -119,11 +119,11 @@ UnixListener::UnixListener(const char *path, int mode)
int err, result;

if (strlen(path) >= sizeof(addr.sun_path))
throw SocketException("socket path is too long", ENAMETOOLONG);
throw socket_error("socket path is too long", ENAMETOOLONG);

// - Create a socket
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
throw SocketException("unable to create listening socket", errno);
throw socket_error("unable to create listening socket", errno);

// - Delete existing socket (ignore result)
unlink(path);
Expand All @@ -138,14 +138,14 @@ UnixListener::UnixListener(const char *path, int mode)
umask(saved_umask);
if (result < 0) {
close(fd);
throw SocketException("unable to bind listening socket", err);
throw socket_error("unable to bind listening socket", err);
}

// - Set socket mode
if (chmod(path, mode) < 0) {
err = errno;
close(fd);
throw SocketException("unable to set socket mode", err);
throw socket_error("unable to set socket mode", err);
}

listen(fd);
Expand Down
16 changes: 8 additions & 8 deletions common/os/Mutex.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Mutex::Mutex()
systemMutex = new pthread_mutex_t;
ret = pthread_mutex_init((pthread_mutex_t*)systemMutex, nullptr);
if (ret != 0)
throw rdr::PosixException("Failed to create mutex", ret);
throw rdr::posix_error("Failed to create mutex", ret);
#endif
}

Expand All @@ -67,7 +67,7 @@ void Mutex::lock()

ret = pthread_mutex_lock((pthread_mutex_t*)systemMutex);
if (ret != 0)
throw rdr::PosixException("Failed to lock mutex", ret);
throw rdr::posix_error("Failed to lock mutex", ret);
#endif
}

Expand All @@ -80,7 +80,7 @@ void Mutex::unlock()

ret = pthread_mutex_unlock((pthread_mutex_t*)systemMutex);
if (ret != 0)
throw rdr::PosixException("Failed to unlock mutex", ret);
throw rdr::posix_error("Failed to unlock mutex", ret);
#endif
}

Expand All @@ -97,7 +97,7 @@ Condition::Condition(Mutex* mutex_)
systemCondition = new pthread_cond_t;
ret = pthread_cond_init((pthread_cond_t*)systemCondition, nullptr);
if (ret != 0)
throw rdr::PosixException("Failed to create condition variable", ret);
throw rdr::posix_error("Failed to create condition variable", ret);
#endif
}

Expand All @@ -120,14 +120,14 @@ void Condition::wait()
(CRITICAL_SECTION*)mutex->systemMutex,
INFINITE);
if (!ret)
throw rdr::Win32Exception("Failed to wait on condition variable", GetLastError());
throw rdr::win32_error("Failed to wait on condition variable", GetLastError());
#else
int ret;

ret = pthread_cond_wait((pthread_cond_t*)systemCondition,
(pthread_mutex_t*)mutex->systemMutex);
if (ret != 0)
throw rdr::PosixException("Failed to wait on condition variable", ret);
throw rdr::posix_error("Failed to wait on condition variable", ret);
#endif
}

Expand All @@ -140,7 +140,7 @@ void Condition::signal()

ret = pthread_cond_signal((pthread_cond_t*)systemCondition);
if (ret != 0)
throw rdr::PosixException("Failed to signal condition variable", ret);
throw rdr::posix_error("Failed to signal condition variable", ret);
#endif
}

Expand All @@ -153,6 +153,6 @@ void Condition::broadcast()

ret = pthread_cond_broadcast((pthread_cond_t*)systemCondition);
if (ret != 0)
throw rdr::PosixException("Failed to broadcast condition variable", ret);
throw rdr::posix_error("Failed to broadcast condition variable", ret);
#endif
}
10 changes: 5 additions & 5 deletions common/os/Thread.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void Thread::start()
#ifdef WIN32
*(HANDLE*)threadId = CreateThread(nullptr, 0, startRoutine, this, 0, nullptr);
if (*(HANDLE*)threadId == nullptr)
throw rdr::Win32Exception("Failed to create thread", GetLastError());
throw rdr::win32_error("Failed to create thread", GetLastError());
#else
int ret;
sigset_t all, old;
Expand All @@ -76,14 +76,14 @@ void Thread::start()
sigfillset(&all);
ret = pthread_sigmask(SIG_SETMASK, &all, &old);
if (ret != 0)
throw rdr::PosixException("Failed to mask signals", ret);
throw rdr::posix_error("Failed to mask signals", ret);

ret = pthread_create((pthread_t*)threadId, nullptr, startRoutine, this);

pthread_sigmask(SIG_SETMASK, &old, nullptr);

if (ret != 0)
throw rdr::PosixException("Failed to create thread", ret);
throw rdr::posix_error("Failed to create thread", ret);
#endif

running = true;
Expand All @@ -99,13 +99,13 @@ void Thread::wait()

ret = WaitForSingleObject(*(HANDLE*)threadId, INFINITE);
if (ret != WAIT_OBJECT_0)
throw rdr::Win32Exception("Failed to join thread", GetLastError());
throw rdr::win32_error("Failed to join thread", GetLastError());
#else
int ret;

ret = pthread_join(*(pthread_t*)threadId, nullptr);
if (ret != 0)
throw rdr::PosixException("Failed to join thread", ret);
throw rdr::posix_error("Failed to join thread", ret);
#endif
}

Expand Down
18 changes: 9 additions & 9 deletions common/rdr/Exception.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@
using namespace rdr;


GAIException::GAIException(const char* s, int err_)
getaddrinfo_error::getaddrinfo_error(const char* s, int err_)
: std::runtime_error(rfb::format("%s: %s (%d)", s,
strerror(err_).c_str(), err_)),
err(err_)
{
}

GAIException::GAIException(const std::string& s, int err_)
getaddrinfo_error::getaddrinfo_error(const std::string& s, int err_)
: std::runtime_error(rfb::format("%s: %s (%d)", s.c_str(),
strerror(err_).c_str(), err_)),
err(err_)
{
}

std::string GAIException::strerror(int err_) const
std::string getaddrinfo_error::strerror(int err_) const
{
#ifdef _WIN32
char str[256];
Expand All @@ -71,21 +71,21 @@ std::string GAIException::strerror(int err_) const
#endif
}

PosixException::PosixException(const char* what_arg, int err_)
posix_error::posix_error(const char* what_arg, int err_)
: std::runtime_error(rfb::format("%s: %s (%d)", what_arg,
strerror(err_).c_str(), err_)),
err(err_)
{
}

PosixException::PosixException(const std::string& what_arg, int err_)
posix_error::posix_error(const std::string& what_arg, int err_)
: std::runtime_error(rfb::format("%s: %s (%d)", what_arg.c_str(),
strerror(err_).c_str(), err_)),
err(err_)
{
}

std::string PosixException::strerror(int err_) const
std::string posix_error::strerror(int err_) const
{
#ifdef _WIN32
char str[256];
Expand All @@ -100,21 +100,21 @@ std::string PosixException::strerror(int err_) const
}

#ifdef WIN32
Win32Exception::Win32Exception(const char* what_arg, unsigned err_)
win32_error::win32_error(const char* what_arg, unsigned err_)
: std::runtime_error(rfb::format("%s: %s (%d)", what_arg,
strerror(err_).c_str(), err_)),
err(err_)
{
}

Win32Exception::Win32Exception(const std::string& what_arg, unsigned err_)
win32_error::win32_error(const std::string& what_arg, unsigned err_)
: std::runtime_error(rfb::format("%s: %s (%d)", what_arg.c_str(),
strerror(err_).c_str(), err_)),
err(err_)
{
}

std::string Win32Exception::strerror(unsigned err_) const
std::string win32_error::strerror(unsigned err_) const
{
wchar_t wstr[256];
char str[256];
Expand Down
Loading

0 comments on commit 2b78572

Please sign in to comment.