-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathloop.cpp
328 lines (266 loc) · 9.1 KB
/
loop.cpp
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
* .============.
* // M A K E / \
* // C++ DEV / \
* // E A S Y / \/ \
* ++ ----------. \/\ .
* \\ \ \ /\ /
* \\ \ \ /
* \\ \ \ /
* -============'
*
* Copyright (c) 2023 Hevake and contributors, all rights reserved.
*
* This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
* Use of this source code is governed by MIT license that can be found
* in the LICENSE file in the root of the source tree. All contributing
* project authors may be found in the CONTRIBUTORS.md file in the root
* of the source tree.
*/
#include "loop.h"
#include <tbox/base/log.h>
#include <tbox/base/assert.h>
#include <tbox/event/fd_event.h>
#include <tbox/event/timer_event.h>
namespace tbox {
namespace dbus {
namespace {
class Context {
public:
Context(event::Loop *loop, DBusConnection *dbus_conn)
: loop_(loop), dbus_conn_(dbus_conn)
{
::dbus_connection_ref(dbus_conn_);
}
~Context()
{
::dbus_connection_unref(dbus_conn_);
}
inline event::Loop* loop() const { return loop_; }
inline DBusConnection* dbus_conn() const { return dbus_conn_; }
private:
event::Loop *loop_;
DBusConnection *dbus_conn_;
};
void _ContextDeleter(void *p)
{
TBOX_ASSERT(p != nullptr);
delete static_cast<Context*>(p);
}
//////////////////////////////////////////////////////////////////////
// 异步派发动作
//////////////////////////////////////////////////////////////////////
void _QueueDispatch(Context *ctx, DBusDispatchStatus status)
{
if (status == DBUS_DISPATCH_DATA_REMAINS) {
auto dbus_conn = ctx->dbus_conn();
dbus_connection_ref(dbus_conn);
ctx->loop()->runNext(
[dbus_conn] {
while (dbus_connection_dispatch(dbus_conn) == DBUS_DISPATCH_DATA_REMAINS);
dbus_connection_unref(dbus_conn);
},
"dbus_dispatch"
);
}
}
void _DispatchStatus(DBusConnection *dbus_conn, DBusDispatchStatus status, void *data)
{
TBOX_ASSERT(dbus_conn != nullptr);
TBOX_ASSERT(data != nullptr);
if (dbus_connection_get_is_connected(dbus_conn)) {
Context *ctx = static_cast<Context*>(data);
_QueueDispatch(ctx, status);
}
}
//////////////////////////////////////////////////////////////////////////////
// 文件读写事件
//////////////////////////////////////////////////////////////////////////////
class WatchHandler {
public:
WatchHandler(Context *ctx, DBusWatch *dbus_watch)
: ctx_(ctx)
, dbus_watch_(dbus_watch)
, tbox_fd_(ctx->loop()->newFdEvent("dbus_watch"))
{
auto fd = ::dbus_watch_get_unix_fd(dbus_watch);
auto dbus_flags = ::dbus_watch_get_flags(dbus_watch);
//! 仅允许读事件或写事件中二选一,不能同时两种事件
TBOX_ASSERT((dbus_flags & (dbus_flags - 1)) == 0);
short tbox_flags = 0;
event::Event::Mode mode;
if (dbus_flags & DBUS_WATCH_READABLE) {
tbox_flags |= tbox::event::FdEvent::kReadEvent;
mode = event::Event::Mode::kPersist;
}
if (dbus_flags & DBUS_WATCH_WRITABLE) {
tbox_flags |= tbox::event::FdEvent::kWriteEvent;
mode = event::Event::Mode::kOneshot;
}
tbox_fd_->initialize(fd, tbox_flags, mode);
tbox_fd_->setCallback([this](short events) { onWatch(events); });
}
~WatchHandler()
{
delete tbox_fd_;
}
void enable() { tbox_fd_->enable(); }
void disable() { tbox_fd_->disable(); }
protected:
void onWatch(short events)
{
unsigned int dbus_flags = 0;
if (events & tbox::event::FdEvent::kReadEvent)
dbus_flags |= DBUS_WATCH_READABLE;
if (events & tbox::event::FdEvent::kWriteEvent)
dbus_flags |= DBUS_WATCH_WRITABLE;
if (events & tbox::event::FdEvent::kExceptEvent)
dbus_flags |= DBUS_WATCH_ERROR;
dbus_watch_handle(dbus_watch_, dbus_flags);
dbus_connection_ref(ctx_->dbus_conn());
auto status = dbus_connection_get_dispatch_status(ctx_->dbus_conn());
_QueueDispatch(ctx_, status);
dbus_connection_unref(ctx_->dbus_conn());
}
private:
Context *ctx_;
DBusWatch *dbus_watch_;
event::FdEvent *tbox_fd_;
};
void _WatchHandlerDeleter(void *p)
{
TBOX_ASSERT(p != nullptr);
delete static_cast<WatchHandler*>(p);
}
dbus_bool_t _AddWatch(DBusWatch *watch, void *data)
{
TBOX_ASSERT(watch != nullptr);
TBOX_ASSERT(data != nullptr);
auto handler = static_cast<WatchHandler*>(::dbus_watch_get_data(watch));
if (handler == nullptr) {
auto ctx = static_cast<Context*>(data);
handler = new WatchHandler(ctx, watch);
::dbus_watch_set_data(watch, handler, _WatchHandlerDeleter);
}
handler->enable();
return true;
}
void _RemoveWatch(DBusWatch *watch, void *)
{
TBOX_ASSERT(watch != nullptr);
auto handler = static_cast<WatchHandler*>(::dbus_watch_get_data(watch));
TBOX_ASSERT(handler != nullptr);
handler->disable();
}
void _ToggledWatch(DBusWatch *watch, void *)
{
TBOX_ASSERT(watch != nullptr);
auto handler = static_cast<WatchHandler*>(::dbus_watch_get_data(watch));
TBOX_ASSERT(handler != nullptr);
if (::dbus_watch_get_enabled(watch))
handler->enable();
else
handler->disable();
}
//////////////////////////////////////////////////////////////////////
// 超时事件
//////////////////////////////////////////////////////////////////////
class TimeoutHandler {
public:
TimeoutHandler(event::Loop *loop, DBusTimeout *dbus_timeout)
: tbox_timer_(loop->newTimerEvent("dbus_timeout"))
, dbus_timeout_(dbus_timeout)
{
int interval_ms = ::dbus_timeout_get_interval(dbus_timeout);
tbox_timer_->initialize(std::chrono::milliseconds(interval_ms), event::Event::Mode::kOneshot);
tbox_timer_->setCallback([this] { onTimeout(); });
}
~TimeoutHandler()
{
tbox_timer_->disable();
delete tbox_timer_;
}
void enable() { tbox_timer_->enable(); }
void disable() { tbox_timer_->disable(); }
protected:
void onTimeout()
{
if (dbus_timeout_get_enabled(dbus_timeout_)) {
dbus_timeout_handle(dbus_timeout_);
}
}
private:
event::TimerEvent *tbox_timer_;
DBusTimeout *dbus_timeout_;
};
void _TimeoutHandlerDeleter(void *p)
{
TBOX_ASSERT(p != nullptr);
delete static_cast<TimeoutHandler*>(p);
}
dbus_bool_t _AddTimeout(DBusTimeout *timeout, void *data)
{
TBOX_ASSERT(timeout != nullptr);
TBOX_ASSERT(data != nullptr);
auto handler = static_cast<TimeoutHandler*>(::dbus_timeout_get_data(timeout));
if (handler == nullptr) {
auto ctx = static_cast<Context*>(data);
handler = new TimeoutHandler(ctx->loop(), timeout);
::dbus_timeout_set_data(timeout, handler, _TimeoutHandlerDeleter);
}
handler->enable();
return true;
}
void _RemoveTimeout(DBusTimeout *timeout, void *)
{
TBOX_ASSERT(timeout != nullptr);
auto handler = static_cast<TimeoutHandler*>(::dbus_timeout_get_data(timeout));
TBOX_ASSERT(handler != nullptr);
handler->disable();
}
void _ToggledTimeout(DBusTimeout *timeout, void *)
{
TBOX_ASSERT(timeout != nullptr);
auto handler = static_cast<TimeoutHandler*>(::dbus_timeout_get_data(timeout));
TBOX_ASSERT(handler != nullptr);
if (::dbus_timeout_get_enabled(timeout))
handler->enable();
else
handler->disable();
}
}
//////////////////////////////////////////////////////////////////////
// AttachLoop() 与 DetachLoop() 实现
//////////////////////////////////////////////////////////////////////
void AttachLoop(DBusConnection *dbus_conn, event::Loop *loop)
{
TBOX_ASSERT(dbus_conn != nullptr);
TBOX_ASSERT(loop != nullptr);
//! 注册文件读写事件的创建与删除方法函数与数据
auto watch_ctx = new Context(loop, dbus_conn);
::dbus_connection_set_watch_functions(dbus_conn,
_AddWatch, _RemoveWatch, _ToggledWatch,
watch_ctx, _ContextDeleter);
//! 注册超时事件的创建与删除方法函数与数据
auto timeout_ctx = new Context(loop, dbus_conn);
::dbus_connection_set_timeout_functions(dbus_conn,
_AddTimeout, _RemoveTimeout, _ToggledTimeout,
timeout_ctx, _ContextDeleter);
//! 注册异步派发动作的方法函数与数据
auto dispatch_ctx = new Context(loop, dbus_conn);
::dbus_connection_set_dispatch_status_function(dbus_conn,
_DispatchStatus, dispatch_ctx, _ContextDeleter);
//! 尝试处理队列中的事件
auto dispatch_status = dbus_connection_get_dispatch_status(dbus_conn);
_QueueDispatch(dispatch_ctx, dispatch_status);
}
void DetachLoop(DBusConnection *dbus_conn)
{
TBOX_ASSERT(dbus_conn != nullptr);
//! 逐一清空AttachLoop()中设置的内容
::dbus_connection_set_watch_functions(dbus_conn, nullptr, nullptr, nullptr, nullptr, nullptr);
::dbus_connection_set_timeout_functions(dbus_conn, nullptr, nullptr, nullptr, nullptr, nullptr);
::dbus_connection_set_dispatch_status_function(dbus_conn, nullptr, nullptr, nullptr);
}
}
}