diff options
author | Jose Bollo <jose.bollo@iot.bzh> | 2018-08-20 17:45:42 +0200 |
---|---|---|
committer | José Bollo <jose.bollo@iot.bzh> | 2018-08-23 08:35:38 +0200 |
commit | e36f8d572ca660f5c06fe45297d13c7a6818cc65 (patch) | |
tree | 51cf8b93d3d7a636d2f6fbab8057f10ee0f3ef0d /src/afb-wsj1.c | |
parent | a1a507793efff92b35603e4948e9f6dff4fba99c (diff) |
Send error replies on disconnection
The pending calls receive an error notification
when the server hang up.
Bug-AGL: SPEC-1668
Change-Id: I052dca5e338a7650d7630856e21f1d3a81c6f265
Signed-off-by: Jose Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'src/afb-wsj1.c')
-rw-r--r-- | src/afb-wsj1.c | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/afb-wsj1.c b/src/afb-wsj1.c index 73242e34..7fb85169 100644 --- a/src/afb-wsj1.c +++ b/src/afb-wsj1.c @@ -44,6 +44,7 @@ static void wsj1_on_hangup(struct afb_wsj1 *wsj1); static void wsj1_on_text(struct afb_wsj1 *wsj1, char *text, size_t size); +static struct afb_wsj1_msg *wsj1_msg_make(struct afb_wsj1 *wsj1, char *text, size_t size); static struct afb_ws_itf wsj1_itf = { .on_hangup = (void*)wsj1_on_hangup, @@ -141,6 +142,32 @@ void afb_wsj1_unref(struct afb_wsj1 *wsj1) static void wsj1_on_hangup(struct afb_wsj1 *wsj1) { + struct wsj1_call *call, *ncall; + struct afb_wsj1_msg *msg; + char *text; + int len; + + static const char error_object_str[] = "{" + "\"jtype\":\"afb-reply\"," + "\"request\":{" + "\"status\":\"disconnected\"," + "\"info\":\"server hung up\"}}"; + + ncall = __atomic_exchange_n(&wsj1->calls, NULL, __ATOMIC_RELAXED); + while (ncall) { + call = ncall; + ncall = call->next; + len = asprintf(&text, "[%d,\"%s\",%s]", RETERR, call->id, error_object_str); + if (len > 0) { + msg = wsj1_msg_make(wsj1, text, (size_t)len); + if (msg != NULL) { + call->callback(call->closure, msg); + afb_wsj1_msg_unref(msg); + } + } + free(call); + } + if (wsj1->itf->on_hangup != NULL) wsj1->itf->on_hangup(wsj1->closure, wsj1); } |