diff options
Diffstat (limited to 'AFBClient.cpp')
-rw-r--r-- | AFBClient.cpp | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/AFBClient.cpp b/AFBClient.cpp index d3e3a5e..0452d16 100644 --- a/AFBClient.cpp +++ b/AFBClient.cpp @@ -7,6 +7,8 @@ #include <cstdlib> #include <cstring> +#include <mutex> + #include <unistd.h> #include <systemd/sd-event.h> @@ -87,13 +89,16 @@ static struct afb_wsj1_itf itf = { 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(); @@ -142,7 +147,7 @@ int api_call(AFBClient *c, struct afb_wsj1 *wsj1, const char *verb, // 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 @@ -219,8 +224,9 @@ fail: 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) { @@ -230,7 +236,7 @@ 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")); @@ -259,7 +265,7 @@ int AFBClient::activateSurface(const char *label) { 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( @@ -274,7 +280,7 @@ int AFBClient::deactivateSurface(const char *label) { 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( @@ -289,7 +295,7 @@ int AFBClient::endDraw(const char *label) { 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", |