From 6f2cfc118701b59d96862199204aef7834f9db86 Mon Sep 17 00:00:00 2001 From: Trumeet Date: Tue, 9 Aug 2022 18:46:45 -0700 Subject: fix(libacron/acronc/helloworld): ac_receive: 'read' should be the delta value of bytes read in the call, not the absolute position Also clearify that 'read' is undefined if the function fails. --- client/acronc/handler_socket.c | 3 ++- client/helloworld/main.c | 13 ++++++++----- client/libacron/README.md | 3 ++- client/libacron/net.c | 4 +++- 4 files changed, 15 insertions(+), 8 deletions(-) (limited to 'client') diff --git a/client/acronc/handler_socket.c b/client/acronc/handler_socket.c index c0a9923..8a2dc4d 100644 --- a/client/acronc/handler_socket.c +++ b/client/acronc/handler_socket.c @@ -128,7 +128,7 @@ static void on_read(uv_stream_t *tcp, ssize_t nread, const uv_buf_t *buf) { size_t pos = 0; size_t read = 0; bool again = false; - while (read < nread || again) { + while (pos < nread || again) { if (again) { LOGD("Clearing backlog."); r = ac_receive(ac_conn, NULL, 0, 0, &obj, NULL); @@ -146,6 +146,7 @@ static void on_read(uv_stream_t *tcp, ssize_t nread, const uv_buf_t *buf) { free(buf->base); return; } + LOGDV("Read %u / %u bytes.", pos, nread); if (!ready) { enum ac_connection_state state; diff --git a/client/helloworld/main.c b/client/helloworld/main.c index 079b957..d744851 100644 --- a/client/helloworld/main.c +++ b/client/helloworld/main.c @@ -349,12 +349,15 @@ int main(int argc, char **argv) { } } if ((r = lock())) { goto end; } - if ((r = ac_receive(connection, buffer, pos, bytes, &obj, &read))) { - bool again = r == AC_E_AGAIN; - if (!again) { - unlock(); - goto end; + while (pos < bytes) { + if ((r = ac_receive(connection, buffer, pos, bytes, &obj, &read))) { + bool again = r == AC_E_AGAIN; + if (!again) { + unlock(); + goto end; + } } + pos += read; } r = process(connection, obj); if ((r = unlock())) { goto end; } diff --git a/client/libacron/README.md b/client/libacron/README.md index 736b44a..bc7e879 100644 --- a/client/libacron/README.md +++ b/client/libacron/README.md @@ -215,9 +215,10 @@ size_t read; while (1) { if ((r = ac_receive(connection, buffer, pos, len, &obj, &read))) { /* Handle error. */ + /* read is undefined. */ break; } - pos += read; + pos += read; /* read is the bytes read in the last call. */ /* The obj is now referencing to a library allocated event or response. */ /* Do things with the event or response. */ ac_obj_free(obj); diff --git a/client/libacron/net.c b/client/libacron/net.c index cbbe113..62ee4a6 100644 --- a/client/libacron/net.c +++ b/client/libacron/net.c @@ -229,7 +229,9 @@ int ac_receive(void *connection, /* In case the deserializer does not run at all. */ memset(res, 0, sizeof(struct ac_result)); if (len) { + size_t pos_starting = pos; for (; pos < len; pos += retval) { + LOGDV("wic_parse[%lu]", pos); retval = wic_parse(inst, &ptr[pos], len - pos); if (wic_get_state(inst) == WIC_STATE_CLOSED) { LOGE("Connection closed."); @@ -242,7 +244,7 @@ int ac_receive(void *connection, break; } } - *len_read = pos; + *len_read = pos - pos_starting; } else { retval = wic_parse(inst, &ptr[pos], len - pos); if (wic_get_state(inst) == WIC_STATE_CLOSED) { -- cgit v1.2.3