lib/nfs/Manager: forward-declare class ManagedConnection

This commit is contained in:
Max Kellermann 2024-05-06 16:56:58 +02:00
parent 63920be17e
commit 028693c380
2 changed files with 36 additions and 30 deletions

@ -2,6 +2,7 @@
// Copyright The Music Player Daemon Project // Copyright The Music Player Daemon Project
#include "Manager.hxx" #include "Manager.hxx"
#include "Connection.hxx"
#include "lib/fmt/ExceptionFormatter.hxx" #include "lib/fmt/ExceptionFormatter.hxx"
#include "event/Loop.hxx" #include "event/Loop.hxx"
#include "util/DeleteDisposer.hxx" #include "util/DeleteDisposer.hxx"
@ -11,6 +12,24 @@
static constexpr Domain nfs_domain("nfs"); static constexpr Domain nfs_domain("nfs");
class NfsManager::ManagedConnection final
: public NfsConnection,
public IntrusiveListHook<>
{
NfsManager &manager;
public:
ManagedConnection(NfsManager &_manager, EventLoop &_loop,
const char *_server,
const char *_export_name) noexcept
:NfsConnection(_loop, _server, _export_name),
manager(_manager) {}
protected:
/* virtual methods from NfsConnection */
void OnNfsConnectionError(std::exception_ptr &&e) noexcept override;
};
void void
NfsManager::ManagedConnection::OnNfsConnectionError(std::exception_ptr &&e) noexcept NfsManager::ManagedConnection::OnNfsConnectionError(std::exception_ptr &&e) noexcept
{ {
@ -23,6 +42,9 @@ NfsManager::ManagedConnection::OnNfsConnectionError(std::exception_ptr &&e) noex
manager.ScheduleDelete(*this); manager.ScheduleDelete(*this);
} }
NfsManager::NfsManager(EventLoop &_loop) noexcept
:idle_event(_loop, BIND_THIS_METHOD(OnIdle)) {}
NfsManager::~NfsManager() noexcept NfsManager::~NfsManager() noexcept
{ {
assert(!GetEventLoop().IsAlive() || GetEventLoop().IsInside()); assert(!GetEventLoop().IsAlive() || GetEventLoop().IsInside());
@ -50,6 +72,14 @@ NfsManager::GetConnection(const char *server, const char *export_name) noexcept
return *c; return *c;
} }
inline void
NfsManager::ScheduleDelete(ManagedConnection &c) noexcept
{
connections.erase(connections.iterator_to(c));
garbage.push_front(c);
idle_event.Schedule();
}
void void
NfsManager::CollectGarbage() noexcept NfsManager::CollectGarbage() noexcept
{ {

@ -1,36 +1,19 @@
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project // Copyright The Music Player Daemon Project
#ifndef MPD_NFS_MANAGER_HXX #pragma once
#define MPD_NFS_MANAGER_HXX
#include "Connection.hxx"
#include "event/IdleEvent.hxx" #include "event/IdleEvent.hxx"
#include "util/IntrusiveList.hxx" #include "util/IntrusiveList.hxx"
class NfsConnection;
/** /**
* A manager for NFS connections. Handles multiple connections to * A manager for NFS connections. Handles multiple connections to
* multiple NFS servers. * multiple NFS servers.
*/ */
class NfsManager final { class NfsManager final {
class ManagedConnection final class ManagedConnection;
: public NfsConnection,
public IntrusiveListHook<>
{
NfsManager &manager;
public:
ManagedConnection(NfsManager &_manager, EventLoop &_loop,
const char *_server,
const char *_export_name) noexcept
:NfsConnection(_loop, _server, _export_name),
manager(_manager) {}
protected:
/* virtual methods from NfsConnection */
void OnNfsConnectionError(std::exception_ptr &&e) noexcept override;
};
using List = IntrusiveList<ManagedConnection>; using List = IntrusiveList<ManagedConnection>;
List connections; List connections;
@ -45,8 +28,7 @@ class NfsManager final {
IdleEvent idle_event; IdleEvent idle_event;
public: public:
explicit NfsManager(EventLoop &_loop) noexcept explicit NfsManager(EventLoop &_loop) noexcept;
:idle_event(_loop, BIND_THIS_METHOD(OnIdle)) {}
/** /**
* Must be run from EventLoop's thread. * Must be run from EventLoop's thread.
@ -62,11 +44,7 @@ public:
const char *export_name) noexcept; const char *export_name) noexcept;
private: private:
void ScheduleDelete(ManagedConnection &c) noexcept { void ScheduleDelete(ManagedConnection &c) noexcept;
connections.erase(connections.iterator_to(c));
garbage.push_front(c);
idle_event.Schedule();
}
/** /**
* Delete all connections on the #garbage list. * Delete all connections on the #garbage list.
@ -76,5 +54,3 @@ private:
/* virtual methods from IdleMonitor */ /* virtual methods from IdleMonitor */
void OnIdle() noexcept; void OnIdle() noexcept;
}; };
#endif