#include <cstdlib>
#include <cstring>
+#include <mutex>
+
#include <unistd.h>
#include <systemd/sd-event.h>
onHangup, onCall, onEvent,
};
-void dispatch_internal(AFBClient *c, uint64_t timeout) {
+std::recursive_mutex dispatch_mutex;
+
+void dispatch_internal(struct sd_event *loop) {
+ std::lock_guard<std::recursive_mutex> guard(dispatch_mutex);
TRACE();
- c->dispatch(timeout);
+ sd_event_run(loop, -1);
}
/// object will be json_object_put
-int api_call(AFBClient *c, struct afb_wsj1 *wsj1, const char *verb,
+int api_call(struct sd_event *loop, struct afb_wsj1 *wsj1, const char *verb,
json_object *object,
std::function<void(bool, json_object *)> onReply) {
TRACE();
// if events get triggered by the call (and would be dispatched before
// the actual call-reply).
while (!returned) {
- dispatch_internal(c, -1);
+ dispatch_internal(loop);
}
// return the actual API call result
return rc;
}
-int AFBClient::dispatch(uint64_t timeout) {
- return sd_event_run(loop, timeout);
+int AFBClient::dispatch() {
+ std::lock_guard<std::recursive_mutex> guard(dispatch_mutex);
+ return sd_event_run(loop, 1);
}
int AFBClient::requestSurface(const char *label) {
int rc = -1;
/* send the request */
int rc2 = api_call(
- this, wsj1, "request_surface", jp, [&rc](bool ok, json_object *j) {
+ loop, wsj1, "request_surface", jp, [&rc](bool ok, json_object *j) {
if (ok) {
int id =
json_object_get_int(json_object_object_get(j, "response"));
TRACE();
json_object *j = json_object_new_object();
json_object_object_add(j, "drawing_name", json_object_new_string(label));
- return api_call(this, wsj1, "activate_surface", j, [](bool ok,
+ return api_call(loop, wsj1, "activate_surface", j, [](bool ok,
json_object *j) {
if (!ok) {
fprintf(
TRACE();
json_object *j = json_object_new_object();
json_object_object_add(j, "drawing_name", json_object_new_string(label));
- return api_call(this, wsj1, "deactivate_surface", j, [](bool ok,
+ return api_call(loop, wsj1, "deactivate_surface", j, [](bool ok,
json_object *j) {
if (!ok) {
fprintf(
TRACE();
json_object *j = json_object_new_object();
json_object_object_add(j, "drawing_name", json_object_new_string(label));
- return api_call(this, wsj1, "enddraw", j, [](bool ok, json_object *j) {
+ return api_call(loop, wsj1, "enddraw", j, [](bool ok, json_object *j) {
if (!ok) {
fprintf(
stderr, "API Call endDraw() failed: %s\n",