Listen: move ClientListener pointer to struct Partition

This commit is contained in:
Max Kellermann 2018-01-29 23:53:52 +01:00
parent 1df5c5a76e
commit 7d16d8c887
5 changed files with 33 additions and 49 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2003-2017 The Music Player Daemon Project * Copyright 2003-2018 The Music Player Daemon Project
* http://www.musicpd.org * http://www.musicpd.org
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -23,14 +23,9 @@
#include "config/Param.hxx" #include "config/Param.hxx"
#include "config/ConfigGlobal.hxx" #include "config/ConfigGlobal.hxx"
#include "config/ConfigOption.hxx" #include "config/ConfigOption.hxx"
#include "net/SocketAddress.hxx"
#include "net/UniqueSocketDescriptor.hxx"
#include "event/ServerSocket.hxx"
#include "system/Error.hxx" #include "system/Error.hxx"
#include "util/RuntimeError.hxx" #include "util/RuntimeError.hxx"
#include "util/Domain.hxx"
#include "fs/AllocatedPath.hxx" #include "fs/AllocatedPath.hxx"
#include "Log.hxx"
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
@ -39,35 +34,33 @@
#include <systemd/sd-daemon.h> #include <systemd/sd-daemon.h>
#endif #endif
static constexpr Domain listen_domain("listen");
#define DEFAULT_PORT 6600 #define DEFAULT_PORT 6600
static ClientListener *listen_socket;
int listen_port; int listen_port;
/** /**
* Throws #std::runtime_error on error. * Throws #std::runtime_error on error.
*/ */
static void static void
listen_add_config_param(unsigned int port, listen_add_config_param(ClientListener &listener,
unsigned int port,
const ConfigParam *param) const ConfigParam *param)
{ {
assert(param != nullptr); assert(param != nullptr);
if (0 == strcmp(param->value.c_str(), "any")) { if (0 == strcmp(param->value.c_str(), "any")) {
listen_socket->AddPort(port); listener.AddPort(port);
} else if (param->value[0] == '/' || param->value[0] == '~') { } else if (param->value[0] == '/' || param->value[0] == '~') {
listen_socket->AddPath(param->GetPath()); listener.AddPath(param->GetPath());
} else { } else {
listen_socket->AddHost(param->value.c_str(), port); listener.AddHost(param->value.c_str(), port);
} }
} }
#ifdef ENABLE_SYSTEMD_DAEMON #ifdef ENABLE_SYSTEMD_DAEMON
static bool static bool
listen_systemd_activation() listen_systemd_activation(ClientListener &listener)
{ {
int n = sd_listen_fds(true); int n = sd_listen_fds(true);
if (n <= 0) { if (n <= 0) {
@ -78,7 +71,7 @@ listen_systemd_activation()
for (int i = SD_LISTEN_FDS_START, end = SD_LISTEN_FDS_START + n; for (int i = SD_LISTEN_FDS_START, end = SD_LISTEN_FDS_START + n;
i != end; ++i) i != end; ++i)
listen_socket->AddFD(i); listener.AddFD(i);
return true; return true;
} }
@ -86,15 +79,13 @@ listen_systemd_activation()
#endif #endif
void void
listen_global_init(EventLoop &loop, Partition &partition) listen_global_init(ClientListener &listener)
{ {
int port = config_get_positive(ConfigOption::PORT, DEFAULT_PORT); int port = config_get_positive(ConfigOption::PORT, DEFAULT_PORT);
const auto *param = config_get_param(ConfigOption::BIND_TO_ADDRESS); const auto *param = config_get_param(ConfigOption::BIND_TO_ADDRESS);
listen_socket = new ClientListener(loop, partition);
#ifdef ENABLE_SYSTEMD_DAEMON #ifdef ENABLE_SYSTEMD_DAEMON
if (listen_systemd_activation()) if (listen_systemd_activation(listener))
return; return;
#endif #endif
@ -104,9 +95,8 @@ listen_global_init(EventLoop &loop, Partition &partition)
do { do {
try { try {
listen_add_config_param(port, param); listen_add_config_param(listener, port, param);
} catch (...) { } catch (...) {
delete listen_socket;
std::throw_with_nested(FormatRuntimeError("Failed to listen on %s (line %i)", std::throw_with_nested(FormatRuntimeError("Failed to listen on %s (line %i)",
param->value.c_str(), param->value.c_str(),
param->line)); param->line));
@ -117,28 +107,13 @@ listen_global_init(EventLoop &loop, Partition &partition)
configured port on all interfaces */ configured port on all interfaces */
try { try {
listen_socket->AddPort(port); listener.AddPort(port);
} catch (...) { } catch (...) {
delete listen_socket;
std::throw_with_nested(FormatRuntimeError("Failed to listen on *:%d: ", port)); std::throw_with_nested(FormatRuntimeError("Failed to listen on *:%d: ", port));
} }
} }
try { listener.Open();
listen_socket->Open();
} catch (...) {
delete listen_socket;
throw;
}
listen_port = port; listen_port = port;
} }
void listen_global_finish(void)
{
LogDebug(listen_domain, "listen_global_finish called");
assert(listen_socket != nullptr);
delete listen_socket;
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2003-2017 The Music Player Daemon Project * Copyright 2003-2018 The Music Player Daemon Project
* http://www.musicpd.org * http://www.musicpd.org
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -20,15 +20,11 @@
#ifndef MPD_LISTEN_HXX #ifndef MPD_LISTEN_HXX
#define MPD_LISTEN_HXX #define MPD_LISTEN_HXX
class EventLoop; class ClientListener;
struct Partition;
extern int listen_port; extern int listen_port;
void void
listen_global_init(EventLoop &loop, Partition &partition); listen_global_init(ClientListener &listener);
void
listen_global_finish();
#endif #endif

View File

@ -28,6 +28,7 @@
#include "Mapper.hxx" #include "Mapper.hxx"
#include "Permission.hxx" #include "Permission.hxx"
#include "Listen.hxx" #include "Listen.hxx"
#include "client/Listener.hxx"
#include "client/Client.hxx" #include "client/Client.hxx"
#include "client/ClientList.hxx" #include "client/ClientList.hxx"
#include "command/AllCommands.hxx" #include "command/AllCommands.hxx"
@ -442,8 +443,10 @@ Instance::ShutdownDatabase() noexcept
inline void inline void
Instance::BeginShutdownPartitions() noexcept Instance::BeginShutdownPartitions() noexcept
{ {
for (auto &partition : partitions) for (auto &partition : partitions) {
partition.pc.Kill(); partition.pc.Kill();
partition.listener.reset();
}
} }
inline void inline void
@ -548,7 +551,7 @@ try {
initialize_decoder_and_player(config.replay_gain); initialize_decoder_and_player(config.replay_gain);
listen_global_init(instance->event_loop, instance->partitions.front()); listen_global_init(*instance->partitions.front().listener);
#ifdef ENABLE_DAEMON #ifdef ENABLE_DAEMON
daemonize_set_user(); daemonize_set_user();
@ -697,10 +700,10 @@ try {
delete instance->state_file; delete instance->state_file;
} }
ZeroconfDeinit();
instance->BeginShutdownPartitions(); instance->BeginShutdownPartitions();
ZeroconfDeinit();
listen_global_finish();
delete instance->client_list; delete instance->client_list;
#ifdef ENABLE_NEIGHBOR_PLUGINS #ifdef ENABLE_NEIGHBOR_PLUGINS

View File

@ -23,6 +23,7 @@
#include "DetachedSong.hxx" #include "DetachedSong.hxx"
#include "mixer/Volume.hxx" #include "mixer/Volume.hxx"
#include "IdleFlags.hxx" #include "IdleFlags.hxx"
#include "client/Listener.hxx"
Partition::Partition(Instance &_instance, Partition::Partition(Instance &_instance,
const char *_name, const char *_name,
@ -33,6 +34,7 @@ Partition::Partition(Instance &_instance,
const ReplayGainConfig &replay_gain_config) const ReplayGainConfig &replay_gain_config)
:instance(_instance), :instance(_instance),
name(_name), name(_name),
listener(new ClientListener(instance.event_loop, *this)),
global_events(instance.event_loop, BIND_THIS_METHOD(OnGlobalEvent)), global_events(instance.event_loop, BIND_THIS_METHOD(OnGlobalEvent)),
playlist(max_length, *this), playlist(max_length, *this),
outputs(*this), outputs(*this),
@ -42,6 +44,8 @@ Partition::Partition(Instance &_instance,
UpdateEffectiveReplayGainMode(); UpdateEffectiveReplayGainMode();
} }
Partition::~Partition() noexcept = default;
void void
Partition::EmitIdle(unsigned mask) Partition::EmitIdle(unsigned mask)
{ {

View File

@ -32,10 +32,12 @@
#include "Compiler.h" #include "Compiler.h"
#include <string> #include <string>
#include <memory>
struct Instance; struct Instance;
class MultipleOutputs; class MultipleOutputs;
class SongLoader; class SongLoader;
class ClientListener;
/** /**
* A partition of the Music Player Daemon. It is a separate unit with * A partition of the Music Player Daemon. It is a separate unit with
@ -49,6 +51,8 @@ struct Partition final : QueueListener, PlayerListener, MixerListener {
const std::string name; const std::string name;
std::unique_ptr<ClientListener> listener;
MaskMonitor global_events; MaskMonitor global_events;
struct playlist playlist; struct playlist playlist;
@ -67,6 +71,8 @@ struct Partition final : QueueListener, PlayerListener, MixerListener {
AudioFormat configured_audio_format, AudioFormat configured_audio_format,
const ReplayGainConfig &replay_gain_config); const ReplayGainConfig &replay_gain_config);
~Partition() noexcept;
void EmitGlobalEvent(unsigned mask) { void EmitGlobalEvent(unsigned mask) {
global_events.OrMask(mask); global_events.OrMask(mask);
} }