event/ServerSocket: migrate from class Error to C++ exceptions
This commit is contained in:
@@ -19,8 +19,7 @@
|
||||
|
||||
#include "config.h"
|
||||
#include "Resolver.hxx"
|
||||
#include "util/Error.hxx"
|
||||
#include "util/Domain.hxx"
|
||||
#include "util/RuntimeError.hxx"
|
||||
|
||||
#include <string>
|
||||
|
||||
@@ -35,12 +34,9 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
const Domain resolver_domain("resolver");
|
||||
|
||||
struct addrinfo *
|
||||
resolve_host_port(const char *host_port, unsigned default_port,
|
||||
int flags, int socktype,
|
||||
Error &error)
|
||||
int flags, int socktype)
|
||||
{
|
||||
std::string p(host_port);
|
||||
const char *host = p.c_str(), *port = nullptr;
|
||||
@@ -87,12 +83,9 @@ resolve_host_port(const char *host_port, unsigned default_port,
|
||||
|
||||
struct addrinfo *ai;
|
||||
int ret = getaddrinfo(host, port, &hints, &ai);
|
||||
if (ret != 0) {
|
||||
error.Format(resolver_domain, ret,
|
||||
"Failed to look up '%s': %s",
|
||||
host_port, gai_strerror(ret));
|
||||
return nullptr;
|
||||
}
|
||||
if (ret != 0)
|
||||
throw FormatRuntimeError("Failed to look up '%s': %s",
|
||||
host_port, gai_strerror(ret));
|
||||
|
||||
return ai;
|
||||
}
|
||||
|
@@ -24,24 +24,21 @@
|
||||
#include "Compiler.h"
|
||||
|
||||
struct addrinfo;
|
||||
class Error;
|
||||
class Domain;
|
||||
|
||||
extern const Domain resolver_domain;
|
||||
|
||||
/**
|
||||
* Resolve a specification in the form "host", "host:port",
|
||||
* "[host]:port". This is a convenience wrapper for getaddrinfo().
|
||||
*
|
||||
* Throws #std::runtime_error on error.
|
||||
*
|
||||
* @param default_port a default port number that will be used if none
|
||||
* is given in the string (if applicable); pass 0 to go without a
|
||||
* default
|
||||
* @return an #addrinfo linked list that must be freed with
|
||||
* freeaddrinfo(), or NULL on error
|
||||
* freeaddrinfo()
|
||||
*/
|
||||
addrinfo *
|
||||
resolve_host_port(const char *host_port, unsigned default_port,
|
||||
int flags, int socktype,
|
||||
Error &error);
|
||||
int flags, int socktype);
|
||||
|
||||
#endif
|
||||
|
@@ -21,6 +21,7 @@
|
||||
#define MPD_SOCKET_ERROR_HXX
|
||||
|
||||
#include "Compiler.h"
|
||||
#include "system/Error.hxx"
|
||||
#include "util/Error.hxx" // IWYU pragma: export
|
||||
|
||||
#ifdef WIN32
|
||||
@@ -136,4 +137,22 @@ NewSocketError()
|
||||
return NewSocketError(GetSocketError());
|
||||
}
|
||||
|
||||
gcc_const
|
||||
static inline std::system_error
|
||||
MakeSocketError(socket_error_t code, const char *msg)
|
||||
{
|
||||
#ifdef WIN32
|
||||
return MakeLastError(code, msg);
|
||||
#else
|
||||
return MakeErrno(code, msg);
|
||||
#endif
|
||||
}
|
||||
|
||||
gcc_pure
|
||||
static inline std::system_error
|
||||
MakeSocketError(const char *msg)
|
||||
{
|
||||
return MakeSocketError(GetSocketError(), msg);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@@ -26,41 +26,35 @@
|
||||
int
|
||||
socket_bind_listen(int domain, int type, int protocol,
|
||||
SocketAddress address,
|
||||
int backlog,
|
||||
Error &error)
|
||||
int backlog)
|
||||
{
|
||||
int fd, ret;
|
||||
const int reuse = 1;
|
||||
|
||||
fd = socket_cloexec_nonblock(domain, type, protocol);
|
||||
if (fd < 0) {
|
||||
SetSocketError(error);
|
||||
error.AddPrefix("Failed to create socket: ");
|
||||
return -1;
|
||||
}
|
||||
if (fd < 0)
|
||||
throw MakeSocketError("Failed to create socket");
|
||||
|
||||
ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
|
||||
(const char *) &reuse, sizeof(reuse));
|
||||
if (ret < 0) {
|
||||
SetSocketError(error);
|
||||
error.AddPrefix("setsockopt() failed: ");
|
||||
auto error = GetSocketError();
|
||||
close_socket(fd);
|
||||
return -1;
|
||||
throw MakeSocketError(error, "setsockopt() failed");
|
||||
}
|
||||
|
||||
ret = bind(fd, address.GetAddress(), address.GetSize());
|
||||
if (ret < 0) {
|
||||
SetSocketError(error);
|
||||
auto error = GetSocketError();
|
||||
close_socket(fd);
|
||||
return -1;
|
||||
throw MakeSocketError(error, "Failed to bind socket");
|
||||
}
|
||||
|
||||
ret = listen(fd, backlog);
|
||||
if (ret < 0) {
|
||||
SetSocketError(error);
|
||||
error.AddPrefix("listen() failed: ");
|
||||
auto error = GetSocketError();
|
||||
close_socket(fd);
|
||||
return -1;
|
||||
throw MakeSocketError(error, "Failed to listen on socket");
|
||||
}
|
||||
|
||||
#if defined(HAVE_STRUCT_UCRED) && defined(SO_PASSCRED)
|
||||
|
@@ -27,12 +27,13 @@
|
||||
#define MPD_SOCKET_UTIL_HXX
|
||||
|
||||
class SocketAddress;
|
||||
class Error;
|
||||
|
||||
/**
|
||||
* Creates a socket listening on the specified address. This is a
|
||||
* shortcut for socket(), bind() and listen().
|
||||
*
|
||||
* Throws #std::system_error on error.
|
||||
*
|
||||
* @param domain the socket domain, e.g. PF_INET6
|
||||
* @param type the socket type, e.g. SOCK_STREAM
|
||||
* @param protocol the protocol, usually 0 to let the kernel choose
|
||||
@@ -40,13 +41,12 @@ class Error;
|
||||
* @param backlog the backlog parameter for the listen() system call
|
||||
* @param error location to store the error occurring, or NULL to
|
||||
* ignore errors
|
||||
* @return the socket file descriptor or -1 on error
|
||||
* @return the socket file descriptor
|
||||
*/
|
||||
int
|
||||
socket_bind_listen(int domain, int type, int protocol,
|
||||
SocketAddress address,
|
||||
int backlog,
|
||||
Error &error);
|
||||
int backlog);
|
||||
|
||||
int
|
||||
socket_keepalive(int fd);
|
||||
|
Reference in New Issue
Block a user