Listen: move ClientListener pointer to struct Partition
This commit is contained in:
parent
1df5c5a76e
commit
7d16d8c887
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2003-2017 The Music Player Daemon Project
|
||||
* Copyright 2003-2018 The Music Player Daemon Project
|
||||
* http://www.musicpd.org
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
@ -23,14 +23,9 @@
|
||||
#include "config/Param.hxx"
|
||||
#include "config/ConfigGlobal.hxx"
|
||||
#include "config/ConfigOption.hxx"
|
||||
#include "net/SocketAddress.hxx"
|
||||
#include "net/UniqueSocketDescriptor.hxx"
|
||||
#include "event/ServerSocket.hxx"
|
||||
#include "system/Error.hxx"
|
||||
#include "util/RuntimeError.hxx"
|
||||
#include "util/Domain.hxx"
|
||||
#include "fs/AllocatedPath.hxx"
|
||||
#include "Log.hxx"
|
||||
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
@ -39,35 +34,33 @@
|
||||
#include <systemd/sd-daemon.h>
|
||||
#endif
|
||||
|
||||
static constexpr Domain listen_domain("listen");
|
||||
|
||||
#define DEFAULT_PORT 6600
|
||||
|
||||
static ClientListener *listen_socket;
|
||||
int listen_port;
|
||||
|
||||
/**
|
||||
* Throws #std::runtime_error on error.
|
||||
*/
|
||||
static void
|
||||
listen_add_config_param(unsigned int port,
|
||||
listen_add_config_param(ClientListener &listener,
|
||||
unsigned int port,
|
||||
const ConfigParam *param)
|
||||
{
|
||||
assert(param != nullptr);
|
||||
|
||||
if (0 == strcmp(param->value.c_str(), "any")) {
|
||||
listen_socket->AddPort(port);
|
||||
listener.AddPort(port);
|
||||
} else if (param->value[0] == '/' || param->value[0] == '~') {
|
||||
listen_socket->AddPath(param->GetPath());
|
||||
listener.AddPath(param->GetPath());
|
||||
} else {
|
||||
listen_socket->AddHost(param->value.c_str(), port);
|
||||
listener.AddHost(param->value.c_str(), port);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef ENABLE_SYSTEMD_DAEMON
|
||||
|
||||
static bool
|
||||
listen_systemd_activation()
|
||||
listen_systemd_activation(ClientListener &listener)
|
||||
{
|
||||
int n = sd_listen_fds(true);
|
||||
if (n <= 0) {
|
||||
@ -78,7 +71,7 @@ listen_systemd_activation()
|
||||
|
||||
for (int i = SD_LISTEN_FDS_START, end = SD_LISTEN_FDS_START + n;
|
||||
i != end; ++i)
|
||||
listen_socket->AddFD(i);
|
||||
listener.AddFD(i);
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -86,15 +79,13 @@ listen_systemd_activation()
|
||||
#endif
|
||||
|
||||
void
|
||||
listen_global_init(EventLoop &loop, Partition &partition)
|
||||
listen_global_init(ClientListener &listener)
|
||||
{
|
||||
int port = config_get_positive(ConfigOption::PORT, DEFAULT_PORT);
|
||||
const auto *param = config_get_param(ConfigOption::BIND_TO_ADDRESS);
|
||||
|
||||
listen_socket = new ClientListener(loop, partition);
|
||||
|
||||
#ifdef ENABLE_SYSTEMD_DAEMON
|
||||
if (listen_systemd_activation())
|
||||
if (listen_systemd_activation(listener))
|
||||
return;
|
||||
#endif
|
||||
|
||||
@ -104,9 +95,8 @@ listen_global_init(EventLoop &loop, Partition &partition)
|
||||
|
||||
do {
|
||||
try {
|
||||
listen_add_config_param(port, param);
|
||||
listen_add_config_param(listener, port, param);
|
||||
} catch (...) {
|
||||
delete listen_socket;
|
||||
std::throw_with_nested(FormatRuntimeError("Failed to listen on %s (line %i)",
|
||||
param->value.c_str(),
|
||||
param->line));
|
||||
@ -117,28 +107,13 @@ listen_global_init(EventLoop &loop, Partition &partition)
|
||||
configured port on all interfaces */
|
||||
|
||||
try {
|
||||
listen_socket->AddPort(port);
|
||||
listener.AddPort(port);
|
||||
} catch (...) {
|
||||
delete listen_socket;
|
||||
std::throw_with_nested(FormatRuntimeError("Failed to listen on *:%d: ", port));
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
listen_socket->Open();
|
||||
} catch (...) {
|
||||
delete listen_socket;
|
||||
throw;
|
||||
}
|
||||
listener.Open();
|
||||
|
||||
listen_port = port;
|
||||
}
|
||||
|
||||
void listen_global_finish(void)
|
||||
{
|
||||
LogDebug(listen_domain, "listen_global_finish called");
|
||||
|
||||
assert(listen_socket != nullptr);
|
||||
|
||||
delete listen_socket;
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2003-2017 The Music Player Daemon Project
|
||||
* Copyright 2003-2018 The Music Player Daemon Project
|
||||
* http://www.musicpd.org
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
@ -20,15 +20,11 @@
|
||||
#ifndef MPD_LISTEN_HXX
|
||||
#define MPD_LISTEN_HXX
|
||||
|
||||
class EventLoop;
|
||||
struct Partition;
|
||||
class ClientListener;
|
||||
|
||||
extern int listen_port;
|
||||
|
||||
void
|
||||
listen_global_init(EventLoop &loop, Partition &partition);
|
||||
|
||||
void
|
||||
listen_global_finish();
|
||||
listen_global_init(ClientListener &listener);
|
||||
|
||||
#endif
|
||||
|
11
src/Main.cxx
11
src/Main.cxx
@ -28,6 +28,7 @@
|
||||
#include "Mapper.hxx"
|
||||
#include "Permission.hxx"
|
||||
#include "Listen.hxx"
|
||||
#include "client/Listener.hxx"
|
||||
#include "client/Client.hxx"
|
||||
#include "client/ClientList.hxx"
|
||||
#include "command/AllCommands.hxx"
|
||||
@ -442,8 +443,10 @@ Instance::ShutdownDatabase() noexcept
|
||||
inline void
|
||||
Instance::BeginShutdownPartitions() noexcept
|
||||
{
|
||||
for (auto &partition : partitions)
|
||||
for (auto &partition : partitions) {
|
||||
partition.pc.Kill();
|
||||
partition.listener.reset();
|
||||
}
|
||||
}
|
||||
|
||||
inline void
|
||||
@ -548,7 +551,7 @@ try {
|
||||
|
||||
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
|
||||
daemonize_set_user();
|
||||
@ -697,10 +700,10 @@ try {
|
||||
delete instance->state_file;
|
||||
}
|
||||
|
||||
ZeroconfDeinit();
|
||||
|
||||
instance->BeginShutdownPartitions();
|
||||
|
||||
ZeroconfDeinit();
|
||||
listen_global_finish();
|
||||
delete instance->client_list;
|
||||
|
||||
#ifdef ENABLE_NEIGHBOR_PLUGINS
|
||||
|
@ -23,6 +23,7 @@
|
||||
#include "DetachedSong.hxx"
|
||||
#include "mixer/Volume.hxx"
|
||||
#include "IdleFlags.hxx"
|
||||
#include "client/Listener.hxx"
|
||||
|
||||
Partition::Partition(Instance &_instance,
|
||||
const char *_name,
|
||||
@ -33,6 +34,7 @@ Partition::Partition(Instance &_instance,
|
||||
const ReplayGainConfig &replay_gain_config)
|
||||
:instance(_instance),
|
||||
name(_name),
|
||||
listener(new ClientListener(instance.event_loop, *this)),
|
||||
global_events(instance.event_loop, BIND_THIS_METHOD(OnGlobalEvent)),
|
||||
playlist(max_length, *this),
|
||||
outputs(*this),
|
||||
@ -42,6 +44,8 @@ Partition::Partition(Instance &_instance,
|
||||
UpdateEffectiveReplayGainMode();
|
||||
}
|
||||
|
||||
Partition::~Partition() noexcept = default;
|
||||
|
||||
void
|
||||
Partition::EmitIdle(unsigned mask)
|
||||
{
|
||||
|
@ -32,10 +32,12 @@
|
||||
#include "Compiler.h"
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
struct Instance;
|
||||
class MultipleOutputs;
|
||||
class SongLoader;
|
||||
class ClientListener;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
std::unique_ptr<ClientListener> listener;
|
||||
|
||||
MaskMonitor global_events;
|
||||
|
||||
struct playlist playlist;
|
||||
@ -67,6 +71,8 @@ struct Partition final : QueueListener, PlayerListener, MixerListener {
|
||||
AudioFormat configured_audio_format,
|
||||
const ReplayGainConfig &replay_gain_config);
|
||||
|
||||
~Partition() noexcept;
|
||||
|
||||
void EmitGlobalEvent(unsigned mask) {
|
||||
global_events.OrMask(mask);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user