2013-01-15 07:14:36 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 The Music Player Daemon Project
|
2013-01-15 07:14:36 +01:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MPD_MULTI_SOCKET_MONITOR_HXX
|
|
|
|
#define MPD_MULTI_SOCKET_MONITOR_HXX
|
|
|
|
|
|
|
|
#include "check.h"
|
2013-08-07 22:16:59 +02:00
|
|
|
#include "IdleMonitor.hxx"
|
2017-08-29 16:23:22 +02:00
|
|
|
#include "TimerEvent.hxx"
|
2013-08-07 22:16:59 +02:00
|
|
|
#include "SocketMonitor.hxx"
|
2018-08-20 16:19:17 +02:00
|
|
|
#include "util/Compiler.h"
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2013-01-15 07:14:36 +01:00
|
|
|
#include <forward_list>
|
2013-11-28 11:50:54 +01:00
|
|
|
#include <iterator>
|
2013-01-15 07:14:36 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifndef _WIN32
|
2014-01-05 01:28:36 +01:00
|
|
|
struct pollfd;
|
|
|
|
#endif
|
|
|
|
|
2013-01-15 07:14:36 +01:00
|
|
|
class EventLoop;
|
|
|
|
|
|
|
|
/**
|
2013-11-24 19:28:04 +01:00
|
|
|
* Similar to #SocketMonitor, but monitors multiple sockets. To use
|
|
|
|
* it, implement the methods PrepareSockets() and DispatchSockets().
|
|
|
|
* In PrepareSockets(), use UpdateSocketList() and AddSocket().
|
|
|
|
* DispatchSockets() will be called if at least one socket is ready.
|
2013-01-15 07:14:36 +01:00
|
|
|
*/
|
2017-08-29 16:23:22 +02:00
|
|
|
class MultiSocketMonitor : IdleMonitor
|
2013-08-07 22:16:59 +02:00
|
|
|
{
|
|
|
|
class SingleFD final : public SocketMonitor {
|
|
|
|
MultiSocketMonitor &multi;
|
|
|
|
|
|
|
|
unsigned revents;
|
|
|
|
|
|
|
|
public:
|
2017-08-10 18:25:22 +02:00
|
|
|
SingleFD(MultiSocketMonitor &_multi, SocketDescriptor _fd,
|
2017-11-12 17:40:12 +01:00
|
|
|
unsigned events) noexcept
|
2013-08-07 22:16:59 +02:00
|
|
|
:SocketMonitor(_fd, _multi.GetEventLoop()),
|
|
|
|
multi(_multi), revents(0) {
|
|
|
|
Schedule(events);
|
|
|
|
}
|
|
|
|
|
2017-11-12 17:40:12 +01:00
|
|
|
SocketDescriptor GetSocket() const noexcept {
|
2017-11-10 20:34:45 +01:00
|
|
|
return SocketMonitor::GetSocket();
|
2013-08-07 22:16:59 +02:00
|
|
|
}
|
|
|
|
|
2017-11-12 17:40:12 +01:00
|
|
|
unsigned GetEvents() const noexcept {
|
2013-08-07 22:16:59 +02:00
|
|
|
return SocketMonitor::GetScheduledFlags();
|
|
|
|
}
|
|
|
|
|
2017-11-12 17:40:12 +01:00
|
|
|
void SetEvents(unsigned _events) noexcept {
|
2013-08-07 22:16:59 +02:00
|
|
|
revents &= _events;
|
|
|
|
SocketMonitor::Schedule(_events);
|
|
|
|
}
|
|
|
|
|
2017-11-12 17:40:12 +01:00
|
|
|
unsigned GetReturnedEvents() const noexcept {
|
2013-08-07 22:16:59 +02:00
|
|
|
return revents;
|
|
|
|
}
|
|
|
|
|
2017-11-12 17:40:12 +01:00
|
|
|
void ClearReturnedEvents() noexcept {
|
2013-08-07 22:16:59 +02:00
|
|
|
revents = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2017-11-10 20:20:07 +01:00
|
|
|
bool OnSocketReady(unsigned flags) noexcept override {
|
2013-08-07 22:16:59 +02:00
|
|
|
revents = flags;
|
|
|
|
multi.SetReady();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
friend class SingleFD;
|
|
|
|
|
2017-08-29 16:23:22 +02:00
|
|
|
TimerEvent timeout_event;
|
|
|
|
|
2017-02-09 20:28:36 +01:00
|
|
|
/**
|
|
|
|
* DispatchSockets() should be called.
|
|
|
|
*/
|
2017-02-09 20:45:29 +01:00
|
|
|
bool ready = false;
|
2017-02-09 20:28:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PrepareSockets() should be called.
|
|
|
|
*
|
|
|
|
* Note that this doesn't need to be initialized by the
|
|
|
|
* constructor; this class is activated with the first
|
|
|
|
* InvalidateSockets() call, which initializes this flag.
|
|
|
|
*/
|
|
|
|
bool refresh;
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2013-08-10 10:10:40 +02:00
|
|
|
std::forward_list<SingleFD> fds;
|
2013-01-15 07:14:36 +01:00
|
|
|
|
|
|
|
public:
|
2013-08-07 22:16:59 +02:00
|
|
|
static constexpr unsigned READ = SocketMonitor::READ;
|
|
|
|
static constexpr unsigned WRITE = SocketMonitor::WRITE;
|
|
|
|
static constexpr unsigned ERROR = SocketMonitor::ERROR;
|
|
|
|
static constexpr unsigned HANGUP = SocketMonitor::HANGUP;
|
2013-01-15 07:14:36 +01:00
|
|
|
|
2017-11-12 17:40:12 +01:00
|
|
|
MultiSocketMonitor(EventLoop &_loop) noexcept;
|
2013-01-15 07:14:36 +01:00
|
|
|
|
2013-08-07 22:16:59 +02:00
|
|
|
using IdleMonitor::GetEventLoop;
|
2013-08-08 21:49:17 +02:00
|
|
|
|
2017-02-09 20:42:33 +01:00
|
|
|
/**
|
|
|
|
* Clear the socket list and disable all #EventLoop
|
|
|
|
* registrations. Run this in the #EventLoop thread before
|
|
|
|
* destroying this object.
|
|
|
|
*
|
|
|
|
* Later, this object can be reused and reactivated by calling
|
|
|
|
* InvalidateSockets().
|
|
|
|
*
|
|
|
|
* Note that this class doesn't have a destructor which calls
|
|
|
|
* this method, because this would be racy and thus pointless:
|
|
|
|
* at the time ~MultiSocketMonitor() is called, our virtual
|
|
|
|
* methods have been morphed to be pure again, and in the
|
|
|
|
* meantime the #EventLoop thread could invoke those pure
|
|
|
|
* methods.
|
|
|
|
*/
|
2017-11-12 17:40:12 +01:00
|
|
|
void Reset() noexcept;
|
2017-02-09 20:42:33 +01:00
|
|
|
|
2014-01-06 08:59:27 +01:00
|
|
|
/**
|
|
|
|
* Invalidate the socket list. A call to PrepareSockets() is
|
|
|
|
* scheduled which will then update the list.
|
|
|
|
*/
|
2017-11-12 17:40:12 +01:00
|
|
|
void InvalidateSockets() noexcept {
|
2013-08-07 22:16:59 +02:00
|
|
|
refresh = true;
|
|
|
|
IdleMonitor::Schedule();
|
2013-01-15 07:14:36 +01:00
|
|
|
}
|
|
|
|
|
2017-02-09 20:28:36 +01:00
|
|
|
/**
|
|
|
|
* Add one socket to the list of monitored sockets.
|
|
|
|
*
|
|
|
|
* May only be called from PrepareSockets().
|
|
|
|
*/
|
2017-11-12 17:40:12 +01:00
|
|
|
void AddSocket(SocketDescriptor fd, unsigned events) noexcept {
|
2013-08-10 10:10:40 +02:00
|
|
|
fds.emplace_front(*this, fd, events);
|
2013-01-15 07:14:36 +01:00
|
|
|
}
|
|
|
|
|
2014-01-05 22:00:33 +01:00
|
|
|
/**
|
|
|
|
* Remove all sockets.
|
2017-02-09 20:28:36 +01:00
|
|
|
*
|
|
|
|
* May only be called from PrepareSockets().
|
2014-01-05 22:00:33 +01:00
|
|
|
*/
|
2017-11-12 17:40:12 +01:00
|
|
|
void ClearSocketList() noexcept;
|
2014-01-05 22:00:33 +01:00
|
|
|
|
2014-01-06 08:59:27 +01:00
|
|
|
/**
|
|
|
|
* Update the known sockets by invoking the given function for
|
|
|
|
* each one; its return value is the events bit mask. A
|
|
|
|
* return value of 0 means the socket will be removed from the
|
|
|
|
* list.
|
2017-02-09 20:28:36 +01:00
|
|
|
*
|
|
|
|
* May only be called from PrepareSockets().
|
2014-01-06 08:59:27 +01:00
|
|
|
*/
|
2013-01-15 07:14:36 +01:00
|
|
|
template<typename E>
|
2017-11-12 17:40:12 +01:00
|
|
|
void UpdateSocketList(E &&e) noexcept {
|
2013-01-15 07:14:36 +01:00
|
|
|
for (auto prev = fds.before_begin(), end = fds.end(),
|
|
|
|
i = std::next(prev);
|
|
|
|
i != end; i = std::next(prev)) {
|
2013-08-10 10:10:40 +02:00
|
|
|
assert(i->GetEvents() != 0);
|
2013-01-15 07:14:36 +01:00
|
|
|
|
2017-11-10 20:34:45 +01:00
|
|
|
unsigned events = e(i->GetSocket());
|
2013-01-15 07:14:36 +01:00
|
|
|
if (events != 0) {
|
2013-08-10 10:10:40 +02:00
|
|
|
i->SetEvents(events);
|
2013-01-15 07:14:36 +01:00
|
|
|
prev = i;
|
|
|
|
} else {
|
|
|
|
fds.erase_after(prev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifndef _WIN32
|
2014-01-05 01:28:36 +01:00
|
|
|
/**
|
|
|
|
* Replace the socket list with the given file descriptors.
|
|
|
|
* The given pollfd array will be modified by this method.
|
2017-02-09 20:28:36 +01:00
|
|
|
*
|
|
|
|
* May only be called from PrepareSockets().
|
2014-01-05 01:28:36 +01:00
|
|
|
*/
|
2017-11-12 17:40:12 +01:00
|
|
|
void ReplaceSocketList(pollfd *pfds, unsigned n) noexcept;
|
2014-01-05 01:28:36 +01:00
|
|
|
#endif
|
|
|
|
|
2013-01-15 07:14:36 +01:00
|
|
|
protected:
|
2013-08-10 10:57:00 +02:00
|
|
|
/**
|
2017-02-09 20:28:36 +01:00
|
|
|
* Override this method and update the socket registrations.
|
|
|
|
* To do that, call AddSocket(), ClearSocketList(),
|
|
|
|
* UpdateSocketList() and ReplaceSocketList().
|
|
|
|
*
|
2016-12-27 23:06:34 +01:00
|
|
|
* @return timeout or a negative value for no timeout
|
2013-08-10 10:57:00 +02:00
|
|
|
*/
|
2017-11-12 17:40:12 +01:00
|
|
|
virtual std::chrono::steady_clock::duration PrepareSockets() noexcept = 0;
|
2017-02-09 20:28:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* At least one socket is ready or the timeout has expired.
|
|
|
|
* This method should be used to perform I/O.
|
|
|
|
*/
|
2017-11-12 17:40:12 +01:00
|
|
|
virtual void DispatchSockets() noexcept = 0;
|
2013-01-15 07:14:36 +01:00
|
|
|
|
2013-08-07 22:16:59 +02:00
|
|
|
private:
|
2017-11-12 17:40:12 +01:00
|
|
|
void SetReady() noexcept {
|
2013-08-07 22:16:59 +02:00
|
|
|
ready = true;
|
|
|
|
IdleMonitor::Schedule();
|
|
|
|
}
|
|
|
|
|
2017-11-12 17:40:12 +01:00
|
|
|
void Prepare() noexcept;
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2017-11-12 17:40:12 +01:00
|
|
|
void OnTimeout() noexcept {
|
2013-08-07 22:16:59 +02:00
|
|
|
SetReady();
|
|
|
|
IdleMonitor::Schedule();
|
|
|
|
}
|
|
|
|
|
2017-11-12 17:40:25 +01:00
|
|
|
virtual void OnIdle() noexcept final;
|
2013-01-15 07:14:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|