From ec1bcdcd0dd6e196303e8d9a30b3b2740e32c502 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 10 Feb 2025 17:50:58 +0100 Subject: Minor code improvements and split into multiple c files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libcoopgamma_send_message__.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 libcoopgamma_send_message__.c (limited to 'libcoopgamma_send_message__.c') diff --git a/libcoopgamma_send_message__.c b/libcoopgamma_send_message__.c new file mode 100644 index 0000000..63ff2b5 --- /dev/null +++ b/libcoopgamma_send_message__.c @@ -0,0 +1,43 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Send a message to the server and wait for response + * + * @param ctx The state of the library + * @param msg The message to send + * @param n The length of `msg` + * @return Zero on success, -1 on error + */ +int +libcoopgamma_send_message__(libcoopgamma_context_t *restrict ctx, char *msg, size_t n) +{ + void *new; + if (ctx->outbound_head == ctx->outbound_tail) { + free(ctx->outbound); + ctx->outbound = msg; + ctx->outbound_tail = 0; + ctx->outbound_head = n; + ctx->outbound_size = n; + } else { + if (ctx->outbound_head + n > ctx->outbound_size) { + memmove(ctx->outbound, ctx->outbound + ctx->outbound_tail, ctx->outbound_head -= ctx->outbound_tail); + ctx->outbound_tail = 0; + } + if (ctx->outbound_head + n > ctx->outbound_size) { + new = realloc(ctx->outbound, ctx->outbound_head + n); + if (!new) { + free(msg); + return -1; + } + ctx->outbound = new; + ctx->outbound_size = ctx->outbound_head + n; + } + memcpy(ctx->outbound + ctx->outbound_head, msg, n); + ctx->outbound_head += n; + free(msg); + } + ctx->message_id += 1; + return libcoopgamma_flush(ctx); +} -- cgit v1.2.3-70-g09d2