mpd/src/event/SocketEvent.hxx

163 lines
3.9 KiB
C++
Raw Normal View History

/*
2020-01-18 19:22:19 +01:00
* Copyright 2003-2020 The Music Player Daemon Project
* 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_SOCKET_EVENT_HXX
#define MPD_SOCKET_EVENT_HXX
#include "PollGroup.hxx"
#include "net/SocketDescriptor.hxx"
#include "util/BindMethod.hxx"
#include <boost/intrusive/list_hook.hpp>
#include <cassert>
#include <cstddef>
#include <type_traits>
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
* #EventLoop will invoke the callback as soon as any of the
* subscribed events are ready.
*
* This class does not feel responsible for closing the socket. Call
* Close() to do it manually.
*
* 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
*/
class SocketEvent {
friend class EventLoop;
EventLoop &loop;
using ReadyListHook = boost::intrusive::list_member_hook<boost::intrusive::link_mode<boost::intrusive::auto_unlink>>;
ReadyListHook ready_siblings;
using Callback = BoundMethod<void(unsigned events) noexcept>;
const Callback callback;
SocketDescriptor fd;
/**
* A bit mask of events that is currently registered in the
* #EventLoop.
*/
unsigned scheduled_flags = 0;
/**
* 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;
public:
static constexpr unsigned READ = PollGroup::READ;
static constexpr unsigned WRITE = PollGroup::WRITE;
static constexpr unsigned ERROR = PollGroup::ERROR;
static constexpr unsigned HANGUP = PollGroup::HANGUP;
/**
* 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;
typedef std::make_signed<size_t>::type ssize_t;
SocketEvent(EventLoop &_loop, Callback _callback,
SocketDescriptor _fd=SocketDescriptor::Undefined()) noexcept
:loop(_loop),
callback(_callback),
fd(_fd) {}
~SocketEvent() noexcept {
Cancel();
}
auto &GetEventLoop() const noexcept {
return loop;
}
2017-11-10 20:20:07 +01:00
bool IsDefined() const noexcept {
return fd.IsDefined();
}
SocketDescriptor GetSocket() const noexcept {
assert(IsDefined());
return fd;
}
2017-11-10 20:20:07 +01:00
void Open(SocketDescriptor _fd) noexcept;
/**
* "Steal" the socket descriptor. This abandons the socket
* and returns it.
*/
2017-11-10 20:20:07 +01:00
SocketDescriptor Steal() noexcept;
2017-11-10 20:20:07 +01:00
void Close() noexcept;
2017-11-10 20:20:07 +01:00
unsigned GetScheduledFlags() const noexcept {
2013-08-07 23:56:49 +02:00
assert(IsDefined());
return scheduled_flags;
}
void SetReadyFlags(unsigned flags) noexcept {
ready_flags = flags;
}
/**
* @return true on success, false on error (with errno set if
* USE_EPOLL is defined)
*/
bool Schedule(unsigned flags) noexcept;
2017-11-10 20:20:07 +01:00
void Cancel() noexcept {
Schedule(0);
}
bool ScheduleRead() noexcept {
return Schedule(GetScheduledFlags() | READ);
}
bool ScheduleWrite() noexcept {
return Schedule(GetScheduledFlags() | WRITE);
}
2017-11-10 20:20:07 +01:00
void CancelRead() noexcept {
Schedule(GetScheduledFlags() & ~READ);
}
2017-11-10 20:20:07 +01:00
void CancelWrite() noexcept {
Schedule(GetScheduledFlags() & ~WRITE);
}
public:
void Dispatch() noexcept;
};
#endif