1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* 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 "BackendEvents.hxx"
#include "net/SocketDescriptor.hxx"
#include "util/BindMethod.hxx"
#include "util/IntrusiveList.hxx"
#include <cassert>
#include <cstddef>
#include <type_traits>
class EventLoop;
/**
* 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.
*/
class SocketEvent final : IntrusiveListHook, public EventPollBackendEvents {
friend class EventLoop;
friend class IntrusiveList<SocketEvent>;
EventLoop &loop;
using Callback = BoundMethod<void(unsigned events) noexcept>;
const Callback callback;
SocketDescriptor fd;
/**
* A bit mask of events that are 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:
/**
* 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();
}
SocketEvent(const SocketEvent &) = delete;
SocketEvent &operator=(const SocketEvent &) = delete;
auto &GetEventLoop() const noexcept {
return loop;
}
bool IsDefined() const noexcept {
return fd.IsDefined();
}
SocketDescriptor GetSocket() const noexcept {
assert(IsDefined());
return fd;
}
SocketDescriptor ReleaseSocket() noexcept {
Cancel();
return std::exchange(fd, SocketDescriptor::Undefined());
}
void Open(SocketDescriptor _fd) noexcept;
/**
* Close the socket (and cancel all scheduled events).
*/
void Close() noexcept;
/**
* 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;
unsigned GetScheduledFlags() const noexcept {
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;
void Cancel() noexcept {
Schedule(0);
}
bool ScheduleRead() noexcept {
return Schedule(GetScheduledFlags() | READ);
}
bool ScheduleWrite() noexcept {
return Schedule(GetScheduledFlags() | WRITE);
}
void CancelRead() noexcept {
Schedule(GetScheduledFlags() & ~READ);
}
void CancelWrite() noexcept {
Schedule(GetScheduledFlags() & ~WRITE);
}
/**
* Schedule only the #IMPLICIT_FLAGS without #READ and #WRITE.
* This is not possible with Schedule(), and no other
* ScheduleX()/CancelX() method may be called on this object.
*/
void ScheduleImplicit() noexcept;
bool IsReadPending() const noexcept {
return GetScheduledFlags() & READ;
}
bool IsWritePending() const noexcept {
return GetScheduledFlags() & WRITE;
}
public:
void Dispatch() noexcept;
};
#endif