From 29030b54c98b0aee65fbc10ebf7ba36bed98c02c Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sat, 10 Aug 2013 18:02:44 +0200 Subject: util/Error: new error passing library Replaces GLib's GError. --- src/event/BufferedSocket.cxx | 9 +++-- src/event/BufferedSocket.hxx | 4 +-- src/event/FullyBufferedSocket.cxx | 8 +++-- src/event/ServerSocket.cxx | 76 ++++++++++++++++++--------------------- src/event/ServerSocket.hxx | 13 ++++--- 5 files changed, 56 insertions(+), 54 deletions(-) (limited to 'src/event') diff --git a/src/event/BufferedSocket.cxx b/src/event/BufferedSocket.cxx index ac3d2cf0e..f333a5987 100644 --- a/src/event/BufferedSocket.cxx +++ b/src/event/BufferedSocket.cxx @@ -21,6 +21,8 @@ #include "BufferedSocket.hxx" #include "system/SocketError.hxx" #include "util/fifo_buffer.h" +#include "util/Error.hxx" +#include "util/Domain.hxx" #include #include @@ -97,8 +99,11 @@ BufferedSocket::ResumeInput() case InputResult::MORE: if (fifo_buffer_is_full(input)) { // TODO - OnSocketError(g_error_new_literal(g_quark_from_static_string("buffered_socket"), - 0, "Input buffer is full")); + static constexpr Domain buffered_socket_domain("buffered_socket"); + Error error; + error.Set(buffered_socket_domain, + "Input buffer is full"); + OnSocketError(std::move(error)); return false; } diff --git a/src/event/BufferedSocket.hxx b/src/event/BufferedSocket.hxx index 07b0b903b..cc763c164 100644 --- a/src/event/BufferedSocket.hxx +++ b/src/event/BufferedSocket.hxx @@ -22,10 +22,10 @@ #include "check.h" #include "SocketMonitor.hxx" -#include "gerror.h" #include "gcc.h" struct fifo_buffer; +class Error; /** * A #SocketMonitor specialization that adds an input buffer. @@ -96,7 +96,7 @@ protected: }; virtual InputResult OnSocketInput(const void *data, size_t length) = 0; - virtual void OnSocketError(GError *error) = 0; + virtual void OnSocketError(Error &&error) = 0; virtual void OnSocketClosed() = 0; virtual bool OnSocketReady(unsigned flags) override; diff --git a/src/event/FullyBufferedSocket.cxx b/src/event/FullyBufferedSocket.cxx index f8b149500..3ffd9f416 100644 --- a/src/event/FullyBufferedSocket.cxx +++ b/src/event/FullyBufferedSocket.cxx @@ -21,6 +21,8 @@ #include "FullyBufferedSocket.hxx" #include "system/SocketError.hxx" #include "util/fifo_buffer.h" +#include "util/Error.hxx" +#include "util/Domain.hxx" #include #include @@ -98,8 +100,10 @@ FullyBufferedSocket::Write(const void *data, size_t length) if (!output.Append(data, length)) { // TODO - OnSocketError(g_error_new_literal(g_quark_from_static_string("buffered_socket"), - 0, "Output buffer is full")); + static constexpr Domain buffered_socket_domain("buffered_socket"); + Error error; + error.Set(buffered_socket_domain, "Output buffer is full"); + OnSocketError(std::move(error)); return false; } diff --git a/src/event/ServerSocket.cxx b/src/event/ServerSocket.cxx index d22790de5..f0b02e34c 100644 --- a/src/event/ServerSocket.cxx +++ b/src/event/ServerSocket.cxx @@ -29,6 +29,8 @@ #include "event/SocketMonitor.hxx" #include "system/Resolver.hxx" #include "system/fd_util.h" +#include "util/Error.hxx" +#include "util/Domain.hxx" #include #include @@ -96,7 +98,7 @@ public: path = g_strdup(_path); } - bool Open(GError **error_r); + bool Open(Error &error); using SocketMonitor::IsDefined; using SocketMonitor::Close; @@ -114,11 +116,7 @@ private: virtual bool OnSocketReady(unsigned flags) override; }; -static GQuark -server_socket_quark(void) -{ - return g_quark_from_static_string("server_socket"); -} +static constexpr Domain server_socket_domain("server_socket"); /** * Wraper for sockaddr_to_string() which never fails. @@ -126,7 +124,7 @@ server_socket_quark(void) char * OneServerSocket::ToString() const { - char *p = sockaddr_to_string(address, address_length, nullptr); + char *p = sockaddr_to_string(address, address_length, IgnoreError()); if (p == nullptr) p = g_strdup("[unknown]"); return p; @@ -190,14 +188,14 @@ OneServerSocket::OnSocketReady(gcc_unused unsigned flags) } inline bool -OneServerSocket::Open(GError **error_r) +OneServerSocket::Open(Error &error) { assert(!IsDefined()); int _fd = socket_bind_listen(address->sa_family, SOCK_STREAM, 0, address, address_length, 5, - error_r); + error); if (_fd < 0) return false; @@ -221,10 +219,10 @@ ServerSocket::ServerSocket(EventLoop &_loop) ServerSocket::~ServerSocket() {} bool -ServerSocket::Open(GError **error_r) +ServerSocket::Open(Error &error) { OneServerSocket *good = nullptr, *bad = nullptr; - GError *last_error = nullptr; + Error last_error; for (auto &i : sockets) { assert(i.GetSerial() > 0); @@ -232,33 +230,33 @@ ServerSocket::Open(GError **error_r) if (bad != nullptr && i.GetSerial() != bad->GetSerial()) { Close(); - g_propagate_error(error_r, last_error); + error = std::move(last_error); return false; } - GError *error = nullptr; - if (!i.Open(&error)) { + Error error2; + if (!i.Open(error2)) { if (good != nullptr && good->GetSerial() == i.GetSerial()) { char *address_string = i.ToString(); char *good_string = good->ToString(); g_warning("bind to '%s' failed: %s " "(continuing anyway, because " "binding to '%s' succeeded)", - address_string, error->message, + address_string, error2.GetMessage(), good_string); g_free(address_string); g_free(good_string); - g_error_free(error); } else if (bad == nullptr) { bad = &i; char *address_string = i.ToString(); - g_propagate_prefixed_error(&last_error, error, - "Failed to bind to '%s': ", - address_string); + error2.FormatPrefix("Failed to bind to '%s': ", + address_string); g_free(address_string); - } else - g_error_free(error); + + last_error = std::move(error2); + } + continue; } @@ -269,14 +267,13 @@ ServerSocket::Open(GError **error_r) if (bad != nullptr) { bad = nullptr; - g_error_free(last_error); - last_error = nullptr; + last_error.Clear(); } } if (bad != nullptr) { Close(); - g_propagate_error(error_r, last_error); + error = std::move(last_error); return false; } @@ -301,7 +298,7 @@ ServerSocket::AddAddress(const sockaddr &address, size_t address_length) } bool -ServerSocket::AddFD(int fd, GError **error_r) +ServerSocket::AddFD(int fd, Error &error) { assert(fd >= 0); @@ -309,8 +306,8 @@ ServerSocket::AddFD(int fd, GError **error_r) socklen_t address_length = sizeof(address); if (getsockname(fd, (struct sockaddr *)&address, &address_length) < 0) { - SetSocketError(error_r); - g_prefix_error(error_r, "Failed to get socket address"); + SetSocketError(error); + error.AddPrefix("Failed to get socket address: "); return false; } @@ -351,12 +348,11 @@ ServerSocket::AddPortIPv6(unsigned port) #endif /* HAVE_TCP */ bool -ServerSocket::AddPort(unsigned port, GError **error_r) +ServerSocket::AddPort(unsigned port, Error &error) { #ifdef HAVE_TCP if (port == 0 || port > 0xffff) { - g_set_error(error_r, server_socket_quark(), 0, - "Invalid TCP port"); + error.Set(server_socket_domain, "Invalid TCP port"); return false; } @@ -371,19 +367,18 @@ ServerSocket::AddPort(unsigned port, GError **error_r) #else /* HAVE_TCP */ (void)port; - g_set_error(error_r, server_socket_quark(), 0, - "TCP support is disabled"); + error.Set(server_socket_domain, "TCP support is disabled"); return false; #endif /* HAVE_TCP */ } bool -ServerSocket::AddHost(const char *hostname, unsigned port, GError **error_r) +ServerSocket::AddHost(const char *hostname, unsigned port, Error &error) { #ifdef HAVE_TCP struct addrinfo *ai = resolve_host_port(hostname, port, AI_PASSIVE, SOCK_STREAM, - error_r); + error); if (ai == nullptr) return false; @@ -399,22 +394,21 @@ ServerSocket::AddHost(const char *hostname, unsigned port, GError **error_r) (void)hostname; (void)port; - g_set_error(error_r, server_socket_quark(), 0, - "TCP support is disabled"); + error.Set(server_socket_domain, "TCP support is disabled"); return false; #endif /* HAVE_TCP */ } bool -ServerSocket::AddPath(const char *path, GError **error_r) +ServerSocket::AddPath(const char *path, Error &error) { #ifdef HAVE_UN struct sockaddr_un s_un; size_t path_length = strlen(path); if (path_length >= sizeof(s_un.sun_path)) { - g_set_error(error_r, server_socket_quark(), 0, - "UNIX socket path is too long"); + error.Set(server_socket_domain, + "UNIX socket path is too long"); return false; } @@ -430,8 +424,8 @@ ServerSocket::AddPath(const char *path, GError **error_r) #else /* !HAVE_UN */ (void)path; - g_set_error(error_r, server_socket_quark(), 0, - "UNIX domain socket support is disabled"); + error.Set(server_socket_domain, + "UNIX domain socket support is disabled"); return false; #endif /* !HAVE_UN */ } diff --git a/src/event/ServerSocket.hxx b/src/event/ServerSocket.hxx index 600cdf8a7..9f0745708 100644 --- a/src/event/ServerSocket.hxx +++ b/src/event/ServerSocket.hxx @@ -20,14 +20,13 @@ #ifndef MPD_SERVER_SOCKET_HXX #define MPD_SERVER_SOCKET_HXX -#include "gerror.h" - #include #include struct sockaddr; class EventLoop; +class Error; typedef void (*server_socket_callback_t)(int fd, const struct sockaddr *address, @@ -79,7 +78,7 @@ public: * ignore errors * @return true on success */ - bool AddPort(unsigned port, GError **error_r); + bool AddPort(unsigned port, Error &error); /** * Resolves a host name, and adds listeners on all addresses in the @@ -91,7 +90,7 @@ public: * ignore errors * @return true on success */ - bool AddHost(const char *hostname, unsigned port, GError **error_r); + bool AddHost(const char *hostname, unsigned port, Error &error); /** * Add a listener on a Unix domain socket. @@ -101,16 +100,16 @@ public: * ignore errors * @return true on success */ - bool AddPath(const char *path, GError **error_r); + bool AddPath(const char *path, Error &error); /** * Add a socket descriptor that is accepting connections. After this * has been called, don't call server_socket_open(), because the * socket is already open. */ - bool AddFD(int fd, GError **error_r); + bool AddFD(int fd, Error &error); - bool Open(GError **error_r); + bool Open(Error &error); void Close(); protected: -- cgit v1.2.3