aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2013-01-15 21:42:55 +0100
committerMax Kellermann <max@duempel.org>2013-01-15 21:42:55 +0100
commitbc66dc45e6450c67320ba0408d5c34ebcafa0cb6 (patch)
tree1c1950c8c6c567325be09d4f5ed2780d6008d35f
parent70879356560d9b7f0343cc0f0cbd8331984cf96a (diff)
downloadmpd-bc66dc45e6450c67320ba0408d5c34ebcafa0cb6.tar.gz
mpd-bc66dc45e6450c67320ba0408d5c34ebcafa0cb6.tar.xz
mpd-bc66dc45e6450c67320ba0408d5c34ebcafa0cb6.zip
server_socket: convert to C++
-rw-r--r--Makefile.am5
-rw-r--r--src/Listen.cxx5
-rw-r--r--src/ServerSocket.cxx (renamed from src/server_socket.c)280
-rw-r--r--src/ServerSocket.hxx (renamed from src/server_socket.h)15
-rw-r--r--src/output/HttpdOutputPlugin.cxx2
-rw-r--r--src/socket_util.h8
6 files changed, 160 insertions, 155 deletions
diff --git a/Makefile.am b/Makefile.am
index 371c43c2b..9782cef5b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -88,7 +88,6 @@ mpd_headers = \
src/despotify_utils.h \
src/text_input_stream.h \
src/icy_server.h \
- src/server_socket.h \
src/ls.h \
src/mixer_api.h \
src/mixer_control.h \
@@ -233,7 +232,7 @@ src_mpd_SOURCES = \
src/ClientMessage.cxx src/ClientMessage.hxx \
src/ClientSubscribe.cxx src/ClientSubscribe.hxx \
src/ClientFile.cxx src/ClientFile.hxx \
- src/server_socket.c \
+ src/ServerSocket.cxx src/ServerSocket.hxx \
src/Listen.cxx src/Listen.hxx \
src/Log.cxx src/Log.hxx \
src/ls.cxx \
@@ -1301,7 +1300,7 @@ test_run_output_SOURCES = test/run_output.cxx \
src/AudioCompress/compress.c \
src/ReplayGainInfo.cxx \
src/fd_util.c \
- src/server_socket.c
+ src/ServerSocket.cxx
test_read_mixer_LDADD = \
libpcm.a \
diff --git a/src/Listen.cxx b/src/Listen.cxx
index 157b7992c..6b298769a 100644
--- a/src/Listen.cxx
+++ b/src/Listen.cxx
@@ -22,10 +22,7 @@
#include "Main.hxx"
#include "Client.hxx"
#include "conf.h"
-
-extern "C" {
-#include "server_socket.h"
-}
+#include "ServerSocket.hxx"
#include <string.h>
#include <assert.h>
diff --git a/src/server_socket.c b/src/ServerSocket.cxx
index 396399596..7127d3bdb 100644
--- a/src/server_socket.c
+++ b/src/ServerSocket.cxx
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2003-2011 The Music Player Daemon Project
+ * 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
@@ -23,12 +23,14 @@
#define _GNU_SOURCE 1
#endif
-#include "server_socket.h"
+#include "ServerSocket.hxx"
#include "socket_util.h"
#include "resolver.h"
#include "fd_util.h"
#include "glib_socket.h"
+#include <forward_list>
+
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
@@ -53,11 +55,10 @@
#define DEFAULT_PORT 6600
-struct one_socket {
- struct one_socket *next;
- struct server_socket *parent;
+struct OneServerSocket {
+ const server_socket &parent;
- unsigned serial;
+ const unsigned serial;
int fd;
guint source_id;
@@ -65,15 +66,52 @@ struct one_socket {
char *path;
size_t address_length;
- struct sockaddr address;
+ struct sockaddr *address;
+
+ OneServerSocket(const server_socket &_parent, unsigned _serial,
+ const struct sockaddr *_address,
+ size_t _address_length)
+ :parent(_parent), serial(_serial),
+ fd(-1),
+ path(nullptr),
+ address_length(_address_length),
+ address((sockaddr *)g_memdup(_address, _address_length))
+ {
+ assert(_address != nullptr);
+ assert(_address_length > 0);
+ }
+
+ OneServerSocket(const OneServerSocket &other) = delete;
+ OneServerSocket &operator=(const OneServerSocket &other) = delete;
+
+ ~OneServerSocket() {
+ Close();
+ g_free(path);
+ g_free(address);
+ }
+
+ bool Open(GError **error_r);
+
+ void Close();
+
+ char *ToString() const;
+
+ void SetFD(int fd);
};
struct server_socket {
server_socket_callback_t callback;
void *callback_ctx;
- struct one_socket *sockets, **sockets_tail_r;
+ std::forward_list<OneServerSocket> sockets;
+
unsigned next_serial;
+
+ server_socket(server_socket_callback_t _callback, void *_callback_ctx)
+ :callback(_callback), callback_ctx(_callback_ctx),
+ next_serial(1) {}
+
+ void Close();
};
static GQuark
@@ -85,41 +123,23 @@ server_socket_quark(void)
struct server_socket *
server_socket_new(server_socket_callback_t callback, void *callback_ctx)
{
- struct server_socket *ss = g_new(struct server_socket, 1);
- ss->callback = callback;
- ss->callback_ctx = callback_ctx;
- ss->sockets = NULL;
- ss->sockets_tail_r = &ss->sockets;
- ss->next_serial = 1;
- return ss;
+ return new server_socket(callback, callback_ctx);
}
void
server_socket_free(struct server_socket *ss)
{
- server_socket_close(ss);
-
- while (ss->sockets != NULL) {
- struct one_socket *s = ss->sockets;
- ss->sockets = s->next;
-
- assert(s->fd < 0);
-
- g_free(s->path);
- g_free(s);
- }
-
- g_free(ss);
+ delete ss;
}
/**
* Wraper for sockaddr_to_string() which never fails.
*/
-static char *
-one_socket_to_string(const struct one_socket *s)
+char *
+OneServerSocket::ToString() const
{
- char *p = sockaddr_to_string(&s->address, s->address_length, NULL);
- if (p == NULL)
+ char *p = sockaddr_to_string(address, address_length, nullptr);
+ if (p == nullptr)
p = g_strdup("[unknown]");
return p;
}
@@ -154,7 +174,7 @@ server_socket_in_event(G_GNUC_UNUSED GIOChannel *source,
G_GNUC_UNUSED GIOCondition condition,
gpointer data)
{
- struct one_socket *s = data;
+ OneServerSocket *s = (OneServerSocket *)data;
struct sockaddr_storage address;
size_t address_length = sizeof(address);
@@ -164,9 +184,9 @@ server_socket_in_event(G_GNUC_UNUSED GIOChannel *source,
if (socket_keepalive(fd))
g_warning("Could not set TCP keepalive option: %s",
g_strerror(errno));
- s->parent->callback(fd, (const struct sockaddr*)&address,
- address_length, get_remote_uid(fd),
- s->parent->callback_ctx);
+ s->parent.callback(fd, (const struct sockaddr*)&address,
+ address_length, get_remote_uid(fd),
+ s->parent.callback_ctx);
} else {
g_warning("accept() failed: %s", g_strerror(errno));
}
@@ -174,47 +194,65 @@ server_socket_in_event(G_GNUC_UNUSED GIOChannel *source,
return true;
}
-static void
-set_fd(struct one_socket *s, int fd)
+void
+OneServerSocket::SetFD(int _fd)
{
- assert(s != NULL);
- assert(s->fd < 0);
- assert(fd >= 0);
+ assert(fd < 0);
+ assert(_fd >= 0);
- s->fd = fd;
+ fd = _fd;
- GIOChannel *channel = g_io_channel_new_socket(s->fd);
- s->source_id = g_io_add_watch(channel, G_IO_IN,
- server_socket_in_event, s);
+ GIOChannel *channel = g_io_channel_new_socket(fd);
+ source_id = g_io_add_watch(channel, G_IO_IN,
+ server_socket_in_event, this);
g_io_channel_unref(channel);
}
+inline bool
+OneServerSocket::Open(GError **error_r)
+{
+ assert(fd < 0);
+
+ int _fd = socket_bind_listen(address->sa_family,
+ SOCK_STREAM, 0,
+ address, address_length, 5,
+ error_r);
+ if (_fd < 0)
+ return false;
+
+ /* allow everybody to connect */
+
+ if (path != nullptr)
+ chmod(path, 0666);
+
+ /* register in the GLib main loop */
+
+ SetFD(_fd);
+
+ return true;
+}
+
bool
server_socket_open(struct server_socket *ss, GError **error_r)
{
- struct one_socket *good = NULL, *bad = NULL;
- GError *last_error = NULL;
+ struct OneServerSocket *good = nullptr, *bad = nullptr;
+ GError *last_error = nullptr;
- for (struct one_socket *s = ss->sockets; s != NULL; s = s->next) {
- assert(s->serial > 0);
- assert(good == NULL || s->serial >= good->serial);
- assert(s->fd < 0);
+ for (auto &i : ss->sockets) {
+ assert(i.serial > 0);
+ assert(good == nullptr || i.serial <= good->serial);
- if (bad != NULL && s->serial != bad->serial) {
+ if (bad != nullptr && i.serial != bad->serial) {
server_socket_close(ss);
g_propagate_error(error_r, last_error);
return false;
}
- GError *error = NULL;
- int fd = socket_bind_listen(s->address.sa_family,
- SOCK_STREAM, 0,
- &s->address, s->address_length, 5,
- &error);
- if (fd < 0) {
- if (good != NULL && good->serial == s->serial) {
- char *address_string = one_socket_to_string(s);
- char *good_string = one_socket_to_string(good);
+ GError *error = nullptr;
+ if (!i.Open(&error)) {
+ if (good != nullptr && good->serial == i.serial) {
+ char *address_string = i.ToString();
+ char *good_string = good->ToString();
g_warning("bind to '%s' failed: %s "
"(continuing anyway, because "
"binding to '%s' succeeded)",
@@ -223,10 +261,10 @@ server_socket_open(struct server_socket *ss, GError **error_r)
g_free(address_string);
g_free(good_string);
g_error_free(error);
- } else if (bad == NULL) {
- bad = s;
+ } else if (bad == nullptr) {
+ bad = &i;
- char *address_string = one_socket_to_string(s);
+ char *address_string = i.ToString();
g_propagate_prefixed_error(&last_error, error,
"Failed to bind to '%s': ",
address_string);
@@ -236,28 +274,19 @@ server_socket_open(struct server_socket *ss, GError **error_r)
continue;
}
- /* allow everybody to connect */
-
- if (s->path != NULL)
- chmod(s->path, 0666);
-
- /* register in the GLib main loop */
-
- set_fd(s, fd);
-
/* mark this socket as "good", and clear previous
errors */
- good = s;
+ good = &i;
- if (bad != NULL) {
- bad = NULL;
+ if (bad != nullptr) {
+ bad = nullptr;
g_error_free(last_error);
- last_error = NULL;
+ last_error = nullptr;
}
}
- if (bad != NULL) {
+ if (bad != nullptr) {
server_socket_close(ss);
g_propagate_error(error_r, last_error);
return false;
@@ -267,43 +296,46 @@ server_socket_open(struct server_socket *ss, GError **error_r)
}
void
-server_socket_close(struct server_socket *ss)
+OneServerSocket::Close()
{
- for (struct one_socket *s = ss->sockets; s != NULL; s = s->next) {
- if (s->fd < 0)
- continue;
+ if (fd < 0)
+ return;
- g_source_remove(s->source_id);
- close_socket(s->fd);
- s->fd = -1;
- }
+ g_source_remove(source_id);
+ close_socket(fd);
+ fd = -1;
+}
+
+void
+server_socket::Close()
+{
+ for (auto &i : sockets)
+ i.Close();
+}
+
+void
+server_socket_close(struct server_socket *ss)
+{
+ ss->Close();
}
-static struct one_socket *
-one_socket_new(unsigned serial, const struct sockaddr *address,
- size_t address_length)
+static OneServerSocket &
+server_socket_add_address(struct server_socket *ss,
+ const struct sockaddr *address,
+ size_t address_length)
{
- assert(address != NULL);
- assert(address_length > 0);
-
- struct one_socket *s = g_malloc(sizeof(*s) - sizeof(s->address) +
- address_length);
- s->next = NULL;
- s->serial = serial;
- s->fd = -1;
- s->path = NULL;
- s->address_length = address_length;
- memcpy(&s->address, address, address_length);
-
- return s;
+ assert(ss != nullptr);
+
+ ss->sockets.emplace_front(*ss, ss->next_serial,
+ address, address_length);
+
+ return ss->sockets.front();
}
bool
server_socket_add_fd(struct server_socket *ss, int fd, GError **error_r)
{
- assert(ss != NULL);
- assert(ss->sockets_tail_r != NULL);
- assert(*ss->sockets_tail_r == NULL);
+ assert(ss != nullptr);
assert(fd >= 0);
struct sockaddr_storage address;
@@ -316,36 +348,14 @@ server_socket_add_fd(struct server_socket *ss, int fd, GError **error_r)
return false;
}
- struct one_socket *s = one_socket_new(ss->next_serial,
- (struct sockaddr *)&address,
- address_length);
- s->parent = ss;
- *ss->sockets_tail_r = s;
- ss->sockets_tail_r = &s->next;
-
- set_fd(s, fd);
+ OneServerSocket &s =
+ server_socket_add_address(ss, (struct sockaddr *)&address,
+ address_length);
+ s.SetFD(fd);
return true;
}
-static struct one_socket *
-server_socket_add_address(struct server_socket *ss,
- const struct sockaddr *address,
- size_t address_length)
-{
- assert(ss != NULL);
- assert(ss->sockets_tail_r != NULL);
- assert(*ss->sockets_tail_r == NULL);
-
- struct one_socket *s = one_socket_new(ss->next_serial,
- address, address_length);
- s->parent = ss;
- *ss->sockets_tail_r = s;
- ss->sockets_tail_r = &s->next;
-
- return s;
-}
-
#ifdef HAVE_TCP
/**
@@ -424,10 +434,10 @@ server_socket_add_host(struct server_socket *ss, const char *hostname,
struct addrinfo *ai = resolve_host_port(hostname, port,
AI_PASSIVE, SOCK_STREAM,
error_r);
- if (ai == NULL)
+ if (ai == nullptr)
return false;
- for (const struct addrinfo *i = ai; i != NULL; i = i->ai_next)
+ for (const struct addrinfo *i = ai; i != nullptr; i = i->ai_next)
server_socket_add_address(ss, i->ai_addr, i->ai_addrlen);
freeaddrinfo(ai);
@@ -465,10 +475,10 @@ server_socket_add_path(struct server_socket *ss, const char *path,
s_un.sun_family = AF_UNIX;
memcpy(s_un.sun_path, path, path_length + 1);
- struct one_socket *s =
+ OneServerSocket &s =
server_socket_add_address(ss, (const struct sockaddr *)&s_un,
sizeof(s_un));
- s->path = g_strdup(path);
+ s.path = g_strdup(path);
return true;
#else /* !HAVE_UN */
diff --git a/src/server_socket.h b/src/ServerSocket.hxx
index 5dc684013..f09fd7e9e 100644
--- a/src/server_socket.h
+++ b/src/ServerSocket.hxx
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2003-2011 The Music Player Daemon Project
+ * 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
@@ -17,12 +17,11 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#ifndef MPD_SERVER_SOCKET_H
-#define MPD_SERVER_SOCKET_H
+#ifndef MPD_SERVER_SOCKET_HXX
+#define MPD_SERVER_SOCKET_HXX
#include "gerror.h"
-#include <stdbool.h>
#include <stddef.h>
struct sockaddr;
@@ -32,10 +31,6 @@ typedef void (*server_socket_callback_t)(int fd,
size_t address_length, int uid,
void *ctx);
-#ifdef __cplusplus
-extern "C" {
-#endif
-
struct server_socket *
server_socket_new(server_socket_callback_t callback, void *callback_ctx);
@@ -94,8 +89,4 @@ bool
server_socket_add_path(struct server_socket *ss, const char *path,
GError **error_r);
-#ifdef __cplusplus
-}
-#endif
-
#endif
diff --git a/src/output/HttpdOutputPlugin.cxx b/src/output/HttpdOutputPlugin.cxx
index c686104c1..075e0ea5e 100644
--- a/src/output/HttpdOutputPlugin.cxx
+++ b/src/output/HttpdOutputPlugin.cxx
@@ -28,7 +28,7 @@
#include "page.h"
#include "icy_server.h"
#include "fd_util.h"
-#include "server_socket.h"
+#include "ServerSocket.hxx"
#include <assert.h>
diff --git a/src/socket_util.h b/src/socket_util.h
index 4cf845d8e..3f602cd53 100644
--- a/src/socket_util.h
+++ b/src/socket_util.h
@@ -32,6 +32,10 @@
struct sockaddr;
+#ifdef __cplusplus
+extern "C" {
+#endif
+
/**
* Creates a socket listening on the specified address. This is a
* shortcut for socket(), bind() and listen().
@@ -55,4 +59,8 @@ socket_bind_listen(int domain, int type, int protocol,
int
socket_keepalive(int fd);
+#ifdef __cplusplus
+}
+#endif
+
#endif