-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Message.cpp
270 lines (230 loc) · 8.43 KB
/
Message.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
#include <ranges>
#include "DBusUtils.hpp"
UDBus::Message::~Message() noexcept
{
unref();
}
DBusMessage* UDBus::Message::get() noexcept
{
return message;
}
DBusMessage** UDBus::Message::getMessagePointer() noexcept
{
return &message;
}
void UDBus::Message::new_method_call(const char* bus_name, const char* path, const char* interface, const char* func) noexcept
{
message = dbus_message_new_method_call(bus_name, path, interface, func);
}
void UDBus::Message::new_1(int messageType) noexcept
{
message = dbus_message_new(messageType);
}
void UDBus::Message::new_method_return(UDBus::Message& method_call) noexcept
{
message = dbus_message_new_method_return(UDBUS_GET_MESSAGE(method_call));
}
void UDBus::Message::new_method_return(DBusMessage* method_call) noexcept
{
message = dbus_message_new_method_return(method_call);
}
void UDBus::Message::new_signal(const char* path, const char* interface, const char* name) noexcept
{
message = dbus_message_new_signal(path, interface, name);
}
void UDBus::Message::new_error(UDBus::Message& reply_to, const char* error_name, const char* error_message) noexcept
{
message = dbus_message_new_error(UDBUS_GET_MESSAGE(reply_to), error_name, error_message);
}
void UDBus::Message::new_error_raw(DBusMessage* reply_to, const char* error_name, const char* error_message) noexcept
{
message = dbus_message_new_error(reply_to, error_name, error_message);
}
void UDBus::Message::copy(UDBus::Message& reply_to) noexcept
{
message = dbus_message_copy(UDBUS_GET_MESSAGE(reply_to));
}
void UDBus::Message::copy(DBusMessage* reply_to) noexcept
{
message = dbus_message_copy(reply_to);
}
void UDBus::Message::ref(UDBus::Message& reply_to) noexcept
{
message = dbus_message_ref(UDBUS_GET_MESSAGE(reply_to));
}
void UDBus::Message::ref(DBusMessage* reply_to) noexcept
{
message = dbus_message_ref(reply_to);
}
void UDBus::Message::demarshal(const char* str, int len, DBusError* error) noexcept
{
message = dbus_message_demarshal(str, len, error);
}
UDBus::Message::operator DBusMessage*() noexcept
{
return message;
}
void UDBus::Message::pending_call_steal_reply(DBusPendingCall* pending) noexcept
{
message = dbus_pending_call_steal_reply(pending);
}
UDBus::Message::Message(DBusMessage* msg) noexcept
{
message = msg;
}
int UDBus::Message::get_type() noexcept
{
return dbus_message_get_type(message);
}
const char *UDBus::Message::get_error_name() noexcept
{
return dbus_message_get_error_name(message);
}
udbus_bool_t UDBus::Message::set_error_name(const char* name) noexcept
{
return dbus_message_set_error_name(message, name);
}
void UDBus::Message::unref() noexcept
{
if (message != nullptr)
dbus_message_unref(message);
message = nullptr;
}
void UDBus::Message::appendGenericBasic(char type, void* data) noexcept
{
const auto f = [this, type, data]() -> void {
if (iteratorStack.empty())
{
DBusMessageIter iter;
dbus_message_iter_init_append(message, &iter);
dbus_message_iter_append_basic(&iter, type, data);
return;
}
iteratorStack.back().append_basic(type, data);
};
char tmp[2] = { type, '\0' }; // We just get a char here :/
if (signatureAccumulationDepth > 0)
handleContainerTypeWithInnerSignature(*this, tmp, f);
else
f();
}
void UDBus::Message::appendArrayBasic(char type, void* data, size_t size, size_t typeSize) noexcept
{
const auto f = [type, data, this, size, typeSize]() -> void {
if (iteratorStack.empty())
{
dbus_message_append_args(message, DBUS_TYPE_ARRAY, type, &data, size, DBUS_TYPE_INVALID);
return;
}
auto& parent = iteratorStack.back(); // Standard iterators bullshit
auto& child = iteratorStack.emplace_back(); // Push child
char signature[] = { type, '\0' };
// Set append mode
parent.setAppend(*this, DBUS_TYPE_ARRAY, signature, child, false);
for (size_t i = 0; i < size; i++)
{
void* tmp = (void*)((intptr_t)data + (i * typeSize)); // Evil pointer magic
child.append_basic(type, tmp);// Append
}
handleClosingContainers(*this);
};
char signature[] = { DBUS_TYPE_ARRAY, type, '\0' };
if (signatureAccumulationDepth > 0)
handleContainerTypeWithInnerSignature(*this, signature, f);
else
f();
}
UDBus::Message& UDBus::Message::BeginStruct(UDBus::Message& message, char type, const char* innerType) noexcept
{
const auto f = [&message, type]() -> void {
pushToIteratorStack(message, type, nullptr);
};
if (message.signatureAccumulationDepth > 0)
handleContainerTypeWithInnerSignature(message, innerType, f);
else
f();
return message;
}
UDBus::Message& UDBus::Message::EndStruct(UDBus::Message& message, const char* type) noexcept
{
const auto f = [&message]() -> void {
handleClosingContainers(message);
};
if (message.signatureAccumulationDepth > 0)
handleContainerTypeWithInnerSignature(message, type, f);
else
f();
return message;
}
UDBus::Message& UDBus::Message::BeginVariant(UDBus::Message& message) noexcept
{
message.signatureAccumulationDepth++; // Increase the depth of the variant because variants can be nested
message.eventList.emplace_back(); // Add new events list
return message;
}
UDBus::Message& UDBus::Message::EndVariant(UDBus::Message& message, std::string containedSignature) noexcept
{
// We only get an empty signature if we're appending to the message directly(not using ArrayBuilder)
if (containedSignature.empty())
containedSignature = message.eventList[message.signatureAccumulationDepth - 1].first;
pushToIteratorStack(message, DBUS_TYPE_VARIANT, containedSignature.c_str());
for (auto& a : message.eventList[message.signatureAccumulationDepth - 1].second)
a();
message.signatureAccumulationDepth--;
handleClosingContainers(message);
message.eventList.pop_back();
return message;
}
void UDBus::Message::pushToIteratorStack(UDBus::Message& message, char type, const char* containedSignature) noexcept
{
// The following 3 lines do the following:
// We create a boolean variable that is passed to setAppend and controls whether the container is initialised or
// simply opened. Types that require a contained_signature should always be opened, since their container is already
// a root iterator in all cases, so we set it to false in that case directly.
//
// We make it true, only when the container doesn't require a contained signature and is the iterator is added to
// the root of the dbus arguments tree. This mostly happens when appending structs to the root of the message call.
bool bRootIterator = false;
if (message.iteratorStack.empty())
bRootIterator = true;
auto& parent = bRootIterator ? message.iteratorStack.emplace_back() : message.iteratorStack.back();
auto& child = message.iteratorStack.emplace_back();
parent.setAppend(message, type, containedSignature, child, bRootIterator);
}
void UDBus::Message::handleContainerTypeWithInnerSignature(UDBus::Message& message, const char* type, const std::function<void(void)>& f) noexcept
{
auto& ref = message.eventList[message.signatureAccumulationDepth - 1];
ref.first += type;
ref.second.emplace_back(f);
}
void UDBus::Message::handleClosingContainers(UDBus::Message& message) noexcept
{
(message.iteratorStack.end() - 2)->close_container(); // Close parent first
message.iteratorStack.pop_back(); // Pop child, this will close it too
if (message.iteratorStack.size() == 1)
message.iteratorStack.pop_back(); // Pop parent only if it's the last iterator
}
UDBus::Message& UDBus::operator<<(UDBus::Message& message, UDBus::MessageManipulators manipulators) noexcept
{
switch (manipulators)
{
case BeginStruct:
return Message::BeginStruct(message);
case EndStruct:
return Message::EndStruct(message);
case BeginVariant:
return Message::BeginVariant(message);
case EndVariant:
return Message::EndVariant(message);
default:
return message;
}
}
template<>
void UDBus::Message::append<UDBus::ArrayBuilder>(const UDBus::ArrayBuilder& t) noexcept
{
pushToIteratorStack(*this, DBUS_TYPE_ARRAY, t.signature.c_str());
for (auto& a : t.eventList)
a();
handleClosingContainers(*this);
}