2023-03-12 19:57:20 +01:00
|
|
|
// SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
// author: Max Kellermann <max.kellermann@gmail.com>
|
2013-01-10 19:05:47 +01:00
|
|
|
|
2023-03-12 19:57:20 +01:00
|
|
|
#pragma once
|
2013-01-10 19:05:47 +01:00
|
|
|
|
2020-10-19 14:28:41 +02:00
|
|
|
#include "BackendEvents.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"
|
2020-10-18 19:31:58 +02:00
|
|
|
#include "util/IntrusiveList.hxx"
|
2020-10-08 20:53:43 +02:00
|
|
|
|
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
|
|
|
*/
|
2022-11-12 08:52:58 +01:00
|
|
|
class SocketEvent final
|
|
|
|
: IntrusiveListHook<IntrusiveHookMode::NORMAL>,
|
|
|
|
public EventPollBackendEvents
|
2020-12-02 15:14:36 +01:00
|
|
|
{
|
2020-10-08 20:53:43 +02:00
|
|
|
friend class EventLoop;
|
2022-06-08 10:31:09 +02:00
|
|
|
friend struct IntrusiveListBaseHookTraits<SocketEvent>;
|
2020-10-08 20:53:43 +02:00
|
|
|
|
2020-10-14 14:24:16 +02:00
|
|
|
EventLoop &loop;
|
|
|
|
|
|
|
|
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:
|
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;
|
|
|
|
|
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
|
|
|
|
2020-10-18 20:07:48 +02:00
|
|
|
SocketEvent(const SocketEvent &) = delete;
|
|
|
|
SocketEvent &operator=(const SocketEvent &) = delete;
|
|
|
|
|
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
|
|
|
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-12-04 09:55:36 +01: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
|
|
|
}
|
|
|
|
|
2023-02-09 15:55:21 +01:00
|
|
|
unsigned GetReadyFlags() const noexcept {
|
|
|
|
return ready_flags;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-15 17:01:13 +02:00
|
|
|
/**
|
|
|
|
* Schedule only the #IMPLICIT_FLAGS without #READ and #WRITE.
|
|
|
|
*/
|
2020-12-04 09:14:59 +01:00
|
|
|
void ScheduleImplicit() noexcept {
|
|
|
|
Schedule(IMPLICIT_FLAGS);
|
|
|
|
}
|
2020-10-15 17:01:13 +02:00
|
|
|
|
|
|
|
bool IsReadPending() const noexcept {
|
|
|
|
return GetScheduledFlags() & READ;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsWritePending() const noexcept {
|
|
|
|
return GetScheduledFlags() & WRITE;
|
|
|
|
}
|
|
|
|
|
2020-12-02 15:14:36 +01:00
|
|
|
private:
|
2020-12-02 15:24:22 +01:00
|
|
|
/**
|
|
|
|
* Dispatch the events that were passed to SetReadyFlags().
|
|
|
|
*/
|
2020-10-08 20:53:43 +02:00
|
|
|
void Dispatch() noexcept;
|
2013-01-10 19:05:47 +01:00
|
|
|
};
|