2013-01-10 19:05:47 +01:00
|
|
|
/*
|
2020-01-18 19:22:19 +01:00
|
|
|
* Copyright 2003-2020 The Music Player Daemon Project
|
2013-01-10 19:05:47 +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.
|
|
|
|
*/
|
|
|
|
|
2020-10-14 14:24:16 +02:00
|
|
|
#ifndef MPD_SOCKET_EVENT_HXX
|
|
|
|
#define MPD_SOCKET_EVENT_HXX
|
2013-01-10 19:05:47 +01:00
|
|
|
|
2013-11-28 11:37:23 +01:00
|
|
|
#include "PollGroup.hxx"
|
2017-08-10 18:25:22 +02:00
|
|
|
#include "net/SocketDescriptor.hxx"
|
2020-10-14 14:24:16 +02:00
|
|
|
#include "util/BindMethod.hxx"
|
2013-01-10 19:05:47 +01:00
|
|
|
|
2020-10-08 20:53:43 +02:00
|
|
|
#include <boost/intrusive/list_hook.hpp>
|
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2020-03-13 00:46:28 +01:00
|
|
|
#include <cstddef>
|
2013-01-30 10:39:17 +01:00
|
|
|
#include <type_traits>
|
|
|
|
|
2013-01-10 19:05:47 +01:00
|
|
|
class EventLoop;
|
|
|
|
|
2013-11-24 19:28:04 +01:00
|
|
|
/**
|
|
|
|
* Monitor events on a socket. Call Schedule() to announce events
|
|
|
|
* you're interested in, or Cancel() to cancel your subscription. The
|
2020-10-14 14:24:16 +02:00
|
|
|
* #EventLoop will invoke the callback as soon as any of the
|
|
|
|
* subscribed events are ready.
|
2013-12-31 15:13:22 +01:00
|
|
|
*
|
2014-01-06 18:02:57 +01:00
|
|
|
* This class does not feel responsible for closing the socket. Call
|
|
|
|
* Close() to do it manually.
|
|
|
|
*
|
2013-12-31 15:13:22 +01:00
|
|
|
* This class is not thread-safe, all methods must be called from the
|
|
|
|
* thread that runs the #EventLoop, except where explicitly documented
|
|
|
|
* as thread-safe.
|
2013-11-24 19:28:04 +01:00
|
|
|
*/
|
2020-10-14 14:24:16 +02:00
|
|
|
class SocketEvent {
|
2020-10-08 20:53:43 +02:00
|
|
|
friend class EventLoop;
|
|
|
|
|
2020-10-14 14:24:16 +02:00
|
|
|
EventLoop &loop;
|
|
|
|
|
2020-10-08 20:53:43 +02:00
|
|
|
using ReadyListHook = boost::intrusive::list_member_hook<boost::intrusive::link_mode<boost::intrusive::auto_unlink>>;
|
|
|
|
ReadyListHook ready_siblings;
|
|
|
|
|
2020-10-14 14:24:16 +02:00
|
|
|
using Callback = BoundMethod<void(unsigned events) noexcept>;
|
|
|
|
const Callback callback;
|
|
|
|
|
2020-10-14 16:18:38 +02:00
|
|
|
SocketDescriptor fd;
|
2013-08-07 22:16:59 +02:00
|
|
|
|
|
|
|
/**
|
2020-10-14 16:30:37 +02:00
|
|
|
* A bit mask of events that are currently registered in the
|
2020-10-08 20:53:43 +02:00
|
|
|
* #EventLoop.
|
2013-08-07 22:16:59 +02:00
|
|
|
*/
|
2017-11-10 20:16:26 +01:00
|
|
|
unsigned scheduled_flags = 0;
|
2013-01-10 19:05:47 +01:00
|
|
|
|
2020-10-08 20:53:43 +02:00
|
|
|
/**
|
|
|
|
* A bit mask of events which have been reported as "ready" by
|
|
|
|
* epoll_wait(). If non-zero, then the #EventLoop will call
|
|
|
|
* Dispatch() soon.
|
|
|
|
*/
|
|
|
|
unsigned ready_flags = 0;
|
|
|
|
|
2013-01-10 19:05:47 +01:00
|
|
|
public:
|
2013-11-28 11:37:23 +01:00
|
|
|
static constexpr unsigned READ = PollGroup::READ;
|
|
|
|
static constexpr unsigned WRITE = PollGroup::WRITE;
|
|
|
|
static constexpr unsigned ERROR = PollGroup::ERROR;
|
|
|
|
static constexpr unsigned HANGUP = PollGroup::HANGUP;
|
2013-01-10 19:05:47 +01:00
|
|
|
|
2020-10-08 21:14:42 +02:00
|
|
|
/**
|
|
|
|
* These flags are always reported by epoll_wait() and don't
|
|
|
|
* need to be registered with epoll_ctl().
|
|
|
|
*/
|
|
|
|
static constexpr unsigned IMPLICIT_FLAGS = ERROR|HANGUP;
|
|
|
|
|
2013-01-30 10:39:17 +01:00
|
|
|
typedef std::make_signed<size_t>::type ssize_t;
|
|
|
|
|
2020-10-14 14:24:16 +02:00
|
|
|
SocketEvent(EventLoop &_loop, Callback _callback,
|
|
|
|
SocketDescriptor _fd=SocketDescriptor::Undefined()) noexcept
|
|
|
|
:loop(_loop),
|
|
|
|
callback(_callback),
|
|
|
|
fd(_fd) {}
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2020-10-14 16:19:08 +02:00
|
|
|
~SocketEvent() noexcept {
|
|
|
|
Cancel();
|
|
|
|
}
|
2013-01-10 19:05:47 +01:00
|
|
|
|
2019-04-04 19:48:28 +02:00
|
|
|
auto &GetEventLoop() const noexcept {
|
2013-08-08 21:49:17 +02:00
|
|
|
return loop;
|
|
|
|
}
|
|
|
|
|
2017-11-10 20:20:07 +01:00
|
|
|
bool IsDefined() const noexcept {
|
2017-08-10 18:25:22 +02:00
|
|
|
return fd.IsDefined();
|
2013-01-10 19:05:47 +01:00
|
|
|
}
|
|
|
|
|
2017-11-10 20:34:45 +01:00
|
|
|
SocketDescriptor GetSocket() const noexcept {
|
2013-01-10 19:05:47 +01:00
|
|
|
assert(IsDefined());
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2020-10-15 16:52:36 +02:00
|
|
|
SocketDescriptor ReleaseSocket() noexcept {
|
|
|
|
Cancel();
|
|
|
|
return std::exchange(fd, SocketDescriptor::Undefined());
|
|
|
|
}
|
2013-01-15 22:48:38 +01:00
|
|
|
|
2020-10-15 16:52:36 +02:00
|
|
|
void Open(SocketDescriptor _fd) noexcept;
|
2013-01-27 22:37:01 +01:00
|
|
|
|
2020-10-15 16:55:29 +02:00
|
|
|
/**
|
|
|
|
* Close the socket (and cancel all scheduled events).
|
|
|
|
*/
|
2017-11-10 20:20:07 +01:00
|
|
|
void Close() noexcept;
|
2013-01-10 19:05:47 +01:00
|
|
|
|
2020-10-15 16:55:43 +02:00
|
|
|
/**
|
|
|
|
* Call this instead of Cancel() to unregister this object
|
|
|
|
* after the underlying socket has already been closed. This
|
|
|
|
* skips the `EPOLL_CTL_DEL` call because the kernel
|
|
|
|
* automatically removes closed file descriptors from epoll.
|
|
|
|
*
|
|
|
|
* Doing `EPOLL_CTL_DEL` on a closed file descriptor usually
|
|
|
|
* fails with `-EBADF` or could unregister a different socket
|
|
|
|
* which happens to be on the same file descriptor number.
|
|
|
|
*/
|
|
|
|
void Abandon() noexcept;
|
|
|
|
|
2017-11-10 20:20:07 +01:00
|
|
|
unsigned GetScheduledFlags() const noexcept {
|
2013-08-07 22:16:59 +02:00
|
|
|
return scheduled_flags;
|
2013-01-10 19:05:47 +01:00
|
|
|
}
|
|
|
|
|
2020-10-08 20:53:43 +02:00
|
|
|
void SetReadyFlags(unsigned flags) noexcept {
|
|
|
|
ready_flags = flags;
|
|
|
|
}
|
|
|
|
|
2019-12-18 17:46:33 +01:00
|
|
|
/**
|
|
|
|
* @return true on success, false on error (with errno set if
|
|
|
|
* USE_EPOLL is defined)
|
|
|
|
*/
|
|
|
|
bool Schedule(unsigned flags) noexcept;
|
2013-08-07 23:57:30 +02:00
|
|
|
|
2017-11-10 20:20:07 +01:00
|
|
|
void Cancel() noexcept {
|
2013-08-07 23:57:30 +02:00
|
|
|
Schedule(0);
|
2013-01-10 19:05:47 +01:00
|
|
|
}
|
|
|
|
|
2019-12-18 17:46:33 +01:00
|
|
|
bool ScheduleRead() noexcept {
|
2020-04-23 15:15:53 +02:00
|
|
|
return Schedule(GetScheduledFlags() | READ);
|
2013-01-10 19:05:47 +01:00
|
|
|
}
|
|
|
|
|
2019-12-18 17:46:33 +01:00
|
|
|
bool ScheduleWrite() noexcept {
|
|
|
|
return Schedule(GetScheduledFlags() | WRITE);
|
2013-01-10 19:05:47 +01:00
|
|
|
}
|
|
|
|
|
2017-11-10 20:20:07 +01:00
|
|
|
void CancelRead() noexcept {
|
2020-04-23 15:15:53 +02:00
|
|
|
Schedule(GetScheduledFlags() & ~READ);
|
2013-01-10 19:05:47 +01:00
|
|
|
}
|
|
|
|
|
2017-11-10 20:20:07 +01:00
|
|
|
void CancelWrite() noexcept {
|
2013-08-07 23:57:30 +02:00
|
|
|
Schedule(GetScheduledFlags() & ~WRITE);
|
2013-01-10 19:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2020-10-08 20:53:43 +02:00
|
|
|
void Dispatch() noexcept;
|
2013-01-10 19:05:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|