From b13efce73e506b0feb4bb7c275c273a54ae6e716 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 22 Oct 2019 15:04:45 +0200 Subject: Change license and style, reorganise file, make makefile portable, and fix bugs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- communication.h | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 communication.h (limited to 'communication.h') diff --git a/communication.h b/communication.h new file mode 100644 index 0000000..b40022b --- /dev/null +++ b/communication.h @@ -0,0 +1,110 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef COMMUNICATION_H +#define COMMUNICATION_H + +#include +#include + +#ifndef GCC_ONLY +# if defined(__GNUC__) && !defined(__clang__) +# define GCC_ONLY(...) __VA_ARGS__ +# else +# define GCC_ONLY(...) /* nothing */ +# endif +#endif + +/** + * Construct a message + * + * @param bufp:char** Output parameter for the buffer, must not have side-effects + * @param np:size_t* Output parameter for the size of the buffer sans `extra` + * @param extra:size_t The extra number for bytes to allocate to the buffer + * @param format:string-literal Message format string + * @param ... Message formatting arguments + */ +#define MAKE_MESSAGE(bufp, np, extra, format, ...)\ + do {\ + ssize_t m__;\ + snprintf(NULL, 0, format "%zn", __VA_ARGS__, &m__);\ + *(bufp) = malloc((size_t)(extra) + (size_t)m__ + (size_t)1);\ + if (!*(bufp))\ + return -1;\ + sprintf(*(bufp), format, __VA_ARGS__);\ + *(np) = (size_t)m__;\ + } while (0) + +/** + * Send a custom error without an error number + * + * @param ... The error description to send + * @return 1: Client disconnected + * 0: Success (possibily delayed) + * -1: An error occurred + */ +#define send_error(...) ((send_error)(conn, message_id, __VA_ARGS__)) + +/** + * Send a standard error + * + * @param ... The value of `errno`, 0 to indicate success + * @return 1: Client disconnected + * 0: Success (possibily delayed) + * -1: An error occurred + */ +#define send_errno(...) ((send_errno)(conn, message_id, __VA_ARGS__)) + +/** + * Send a message + * + * @param conn The index of the connection + * @param buf The data to send + * @param n The size of `buf` + * @return Zero on success, -1 on error, 1 if disconncted + * EINTR, EAGAIN, EWOULDBLOCK, and ECONNRESET count + * as success (ECONNRESET cause 1 to be returned), + * and are handled appropriately. + */ +int send_message(size_t conn, char *restrict buf, size_t n); + +/** + * Send a custom error without an error number + * + * @param conn The index of the connection + * @param message_id The ID of the message to which this message is a response + * @param desc The error description to send + * @return 1: Client disconnected + * 0: Success (possibily delayed) + * -1: An error occurred + */ +GCC_ONLY(__attribute__((__nonnull__))) +int (send_error)(size_t conn, const char *restrict message_id, const char *restrict desc); + +/** + * Send a standard error + * + * @param conn The index of the connection + * @param message_id The ID of the message to which this message is a response + * @param number The value of `errno`, 0 to indicate success + * @return 1: Client disconnected + * 0: Success (possibily delayed) + * -1: An error occurred + */ +GCC_ONLY(__attribute__((__nonnull__))) +int (send_errno)(size_t conn, const char *restrict message_id, int number); + +/** + * Continue sending the queued messages + * + * @param conn The index of the connection + * @return Zero on success, -1 on error, 1 if disconncted + * EINTR, EAGAIN, EWOULDBLOCK, and ECONNRESET count + * as success (ECONNRESET cause 1 to be returned), + * and are handled appropriately. + */ +static inline int +continue_send(size_t conn) +{ + return send_message(conn, NULL, 0); +} + +#endif -- cgit v1.2.3-70-g09d2