util/Error: new error passing library
Replaces GLib's GError.
This commit is contained in:
@@ -17,7 +17,9 @@
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
#include "FatalError.hxx"
|
||||
#include "util/Error.hxx"
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
@@ -52,6 +54,12 @@ FormatFatalError(const char *fmt, ...)
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
void
|
||||
FatalError(const Error &error)
|
||||
{
|
||||
FatalError(error.GetMessage());
|
||||
}
|
||||
|
||||
void
|
||||
FatalError(GError *error)
|
||||
{
|
||||
|
@@ -20,9 +20,12 @@
|
||||
#ifndef MPD_FATAL_ERROR_HXX
|
||||
#define MPD_FATAL_ERROR_HXX
|
||||
|
||||
#include "check.h"
|
||||
#include "gerror.h"
|
||||
#include "gcc.h"
|
||||
|
||||
class Error;
|
||||
|
||||
/**
|
||||
* Log the specified message and abort the process.
|
||||
*/
|
||||
@@ -34,6 +37,10 @@ gcc_noreturn
|
||||
void
|
||||
FormatFatalError(const char *fmt, ...);
|
||||
|
||||
gcc_noreturn
|
||||
void
|
||||
FatalError(const Error &error);
|
||||
|
||||
gcc_noreturn
|
||||
void
|
||||
FatalError(GError *error);
|
||||
|
@@ -19,6 +19,10 @@
|
||||
|
||||
#include "config.h"
|
||||
#include "Resolver.hxx"
|
||||
#include "util/Error.hxx"
|
||||
#include "util/Domain.hxx"
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
#ifndef G_OS_WIN32
|
||||
#include <sys/socket.h>
|
||||
@@ -30,8 +34,10 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
const Domain resolver_domain("resolver");
|
||||
|
||||
char *
|
||||
sockaddr_to_string(const struct sockaddr *sa, size_t length, GError **error)
|
||||
sockaddr_to_string(const struct sockaddr *sa, size_t length, Error &error)
|
||||
{
|
||||
#if defined(HAVE_IPV6) && defined(IN6_IS_ADDR_V4MAPPED)
|
||||
const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *)sa;
|
||||
@@ -59,8 +65,7 @@ sockaddr_to_string(const struct sockaddr *sa, size_t length, GError **error)
|
||||
ret = getnameinfo(sa, length, host, sizeof(host), serv, sizeof(serv),
|
||||
NI_NUMERICHOST|NI_NUMERICSERV);
|
||||
if (ret != 0) {
|
||||
g_set_error(error, g_quark_from_static_string("netdb"), ret,
|
||||
"%s", gai_strerror(ret));
|
||||
error.Set(resolver_domain, ret, gai_strerror(ret));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -82,7 +87,7 @@ sockaddr_to_string(const struct sockaddr *sa, size_t length, GError **error)
|
||||
struct addrinfo *
|
||||
resolve_host_port(const char *host_port, unsigned default_port,
|
||||
int flags, int socktype,
|
||||
GError **error_r)
|
||||
Error &error)
|
||||
{
|
||||
char *p = g_strdup(host_port);
|
||||
const char *host = p, *port = NULL;
|
||||
@@ -130,9 +135,9 @@ resolve_host_port(const char *host_port, unsigned default_port,
|
||||
int ret = getaddrinfo(host, port, &hints, &ai);
|
||||
g_free(p);
|
||||
if (ret != 0) {
|
||||
g_set_error(error_r, resolver_quark(), ret,
|
||||
"Failed to look up '%s': %s",
|
||||
host_port, gai_strerror(ret));
|
||||
error.Format(resolver_domain, ret,
|
||||
"Failed to look up '%s': %s",
|
||||
host_port, gai_strerror(ret));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@@ -22,17 +22,13 @@
|
||||
|
||||
#include "gcc.h"
|
||||
|
||||
#include <glib.h>
|
||||
#include <stddef.h>
|
||||
|
||||
struct sockaddr;
|
||||
struct addrinfo;
|
||||
class Error;
|
||||
|
||||
gcc_const
|
||||
static inline GQuark
|
||||
resolver_quark(void)
|
||||
{
|
||||
return g_quark_from_static_string("resolver");
|
||||
}
|
||||
extern const class Domain resolver_domain;
|
||||
|
||||
/**
|
||||
* Converts the specified socket address into a string in the form
|
||||
@@ -46,7 +42,7 @@ resolver_quark(void)
|
||||
*/
|
||||
gcc_malloc
|
||||
char *
|
||||
sockaddr_to_string(const struct sockaddr *sa, size_t length, GError **error);
|
||||
sockaddr_to_string(const struct sockaddr *sa, size_t length, Error &error);
|
||||
|
||||
/**
|
||||
* Resolve a specification in the form "host", "host:port",
|
||||
@@ -61,6 +57,6 @@ sockaddr_to_string(const struct sockaddr *sa, size_t length, GError **error);
|
||||
struct addrinfo *
|
||||
resolve_host_port(const char *host_port, unsigned default_port,
|
||||
int flags, int socktype,
|
||||
GError **error_r);
|
||||
Error &error);
|
||||
|
||||
#endif
|
||||
|
24
src/system/SocketError.cxx
Normal file
24
src/system/SocketError.cxx
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
||||
* http://www.musicpd.org
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
#include "SocketError.hxx"
|
||||
#include "util/Domain.hxx"
|
||||
|
||||
const Domain socket_domain("socket");
|
@@ -21,6 +21,7 @@
|
||||
#define MPD_SOCKET_ERROR_HXX
|
||||
|
||||
#include "gcc.h"
|
||||
#include "util/Error.hxx"
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
@@ -33,15 +34,10 @@ typedef int socket_error_t;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* A GQuark for GError for socket I/O errors. The code is an errno
|
||||
* A #Domain for #Error for socket I/O errors. The code is an errno
|
||||
* value (or WSAGetLastError() on Windows).
|
||||
*/
|
||||
gcc_const
|
||||
static inline GQuark
|
||||
SocketErrorQuark(void)
|
||||
{
|
||||
return g_quark_from_static_string("socket");
|
||||
}
|
||||
extern const class Domain socket_domain;
|
||||
|
||||
gcc_pure
|
||||
static inline socket_error_t
|
||||
@@ -121,33 +117,29 @@ public:
|
||||
};
|
||||
|
||||
static inline void
|
||||
SetSocketError(GError **error_r, socket_error_t code)
|
||||
SetSocketError(Error &error, socket_error_t code)
|
||||
{
|
||||
#ifdef WIN32
|
||||
if (error_r == NULL)
|
||||
return;
|
||||
#endif
|
||||
|
||||
const SocketErrorMessage msg(code);
|
||||
g_set_error_literal(error_r, SocketErrorQuark(), code, msg);
|
||||
error.Set(socket_domain, code, msg);
|
||||
}
|
||||
|
||||
static inline void
|
||||
SetSocketError(GError **error_r)
|
||||
SetSocketError(Error &error)
|
||||
{
|
||||
SetSocketError(error_r, GetSocketError());
|
||||
SetSocketError(error, GetSocketError());
|
||||
}
|
||||
|
||||
gcc_malloc
|
||||
static inline GError *
|
||||
gcc_const
|
||||
static inline Error
|
||||
NewSocketError(socket_error_t code)
|
||||
{
|
||||
const SocketErrorMessage msg(code);
|
||||
return g_error_new_literal(SocketErrorQuark(), code, msg);
|
||||
Error error;
|
||||
SetSocketError(error, code);
|
||||
return error;
|
||||
}
|
||||
|
||||
gcc_malloc
|
||||
static inline GError *
|
||||
gcc_pure
|
||||
static inline Error
|
||||
NewSocketError()
|
||||
{
|
||||
return NewSocketError(GetSocketError());
|
||||
|
@@ -41,38 +41,38 @@ int
|
||||
socket_bind_listen(int domain, int type, int protocol,
|
||||
const struct sockaddr *address, size_t address_length,
|
||||
int backlog,
|
||||
GError **error_r)
|
||||
Error &error)
|
||||
{
|
||||
int fd, ret;
|
||||
const int reuse = 1;
|
||||
|
||||
fd = socket_cloexec_nonblock(domain, type, protocol);
|
||||
if (fd < 0) {
|
||||
SetSocketError(error_r);
|
||||
g_prefix_error(error_r, "Failed to create socket: ");
|
||||
SetSocketError(error);
|
||||
error.AddPrefix("Failed to create socket: ");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
|
||||
(const char *) &reuse, sizeof(reuse));
|
||||
if (ret < 0) {
|
||||
SetSocketError(error_r);
|
||||
g_prefix_error(error_r, "setsockopt() failed: ");
|
||||
SetSocketError(error);
|
||||
error.AddPrefix("setsockopt() failed: ");
|
||||
close_socket(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = bind(fd, address, address_length);
|
||||
if (ret < 0) {
|
||||
SetSocketError(error_r);
|
||||
SetSocketError(error);
|
||||
close_socket(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = listen(fd, backlog);
|
||||
if (ret < 0) {
|
||||
SetSocketError(error_r);
|
||||
g_prefix_error(error_r, "listen() failed: ");
|
||||
SetSocketError(error);
|
||||
error.AddPrefix("listen() failed: ");
|
||||
close_socket(fd);
|
||||
return -1;
|
||||
}
|
||||
|
@@ -26,11 +26,10 @@
|
||||
#ifndef MPD_SOCKET_UTIL_HXX
|
||||
#define MPD_SOCKET_UTIL_HXX
|
||||
|
||||
#include "gerror.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
struct sockaddr;
|
||||
class Error;
|
||||
|
||||
/**
|
||||
* Creates a socket listening on the specified address. This is a
|
||||
@@ -50,7 +49,7 @@ int
|
||||
socket_bind_listen(int domain, int type, int protocol,
|
||||
const struct sockaddr *address, size_t address_length,
|
||||
int backlog,
|
||||
GError **error);
|
||||
Error &error);
|
||||
|
||||
int
|
||||
socket_keepalive(int fd);
|
||||
|
Reference in New Issue
Block a user