diff options
| author | Mattias Andrée <m@maandree.se> | 2025-02-10 17:50:58 +0100 | 
|---|---|---|
| committer | Mattias Andrée <m@maandree.se> | 2025-02-10 17:52:46 +0100 | 
| commit | ec1bcdcd0dd6e196303e8d9a30b3b2740e32c502 (patch) | |
| tree | dcc759aaf897c915827659e00644f12503cf1268 /libcoopgamma_send_message__.c | |
| parent | Improve makefile (diff) | |
| download | libcoopgamma-ec1bcdcd0dd6e196303e8d9a30b3b2740e32c502.tar.gz libcoopgamma-ec1bcdcd0dd6e196303e8d9a30b3b2740e32c502.tar.bz2 libcoopgamma-ec1bcdcd0dd6e196303e8d9a30b3b2740e32c502.tar.xz | |
Minor code improvements and split into multiple c files
Signed-off-by: Mattias Andrée <m@maandree.se>
Diffstat (limited to 'libcoopgamma_send_message__.c')
| -rw-r--r-- | libcoopgamma_send_message__.c | 43 | 
1 files changed, 43 insertions, 0 deletions
| 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); +} | 
