From 2e75ffaf88457e7025a63cbdb13a770d35e69d21 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Tue, 23 Dec 2025 22:37:50 +0100 Subject: [PATCH] feat(server): implemented send for udp and tcp server --- include/cosms-core/networking/server.h | 14 +++++++++++++- src/networking/server.c | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/include/cosms-core/networking/server.h b/include/cosms-core/networking/server.h index 9cc9a38..b4db279 100644 --- a/include/cosms-core/networking/server.h +++ b/include/cosms-core/networking/server.h @@ -22,7 +22,8 @@ typedef enum { COSMS_CORE_SERVER_COULD_NOT_BIND_ADDRESS = -2, COSMS_CORE_SERVER_UNKNOWN_ERROR = -3, COSMS_CORE_SERVER_TYPE_NOT_SUPPORTED = -4, - COSMS_CORE_SERVER_FAILED_TO_RECEIVE = -5 + COSMS_CORE_SERVER_FAILED_TO_RECEIVE = -5, + COSMS_CORE_SERVER_FAILED_TO_SEND = -6 } CosmsCoreServerError; struct cosms_core_server { @@ -76,6 +77,17 @@ CosmsCoreServerError cosms_core_server_destroy(struct cosms_core_server *current * @return COSMS_CORE_SERVER_OK or COSMS_CORE_SERVER_CONNECTION_CLOSED on success, or negative error code */ CosmsCoreServerError cosms_core_server_receive(struct cosms_core_server *current_server, struct cosms_core_server_client *client, char *buffer, unsigned int buffer_size, unsigned int *received_bytes); +/** + * @brief Sends message to client. + * + * @param current_server Pointer to active server struct (must not be NULL) + * @param client Pointer to client struct (must be connected to server when using tcp) + * @param buffer containing the message to send to the client + * @param buffer_size the size of the buffer to send to the client + * @param send_bytes the amount of bytes send to the client (can be NULL) + * @return COSMS_CORE_SERVER_OK on success, or negative error code + */ +CosmsCoreServerError cosms_core_server_send(struct cosms_core_server *current_server, struct cosms_core_server_client *client, char *buffer, unsigned int buffer_size, unsigned int *send_bytes); /** * @brief Converts error code to a string that can be used for printing/logging errors. diff --git a/src/networking/server.c b/src/networking/server.c index 5ea9207..c0fafcb 100644 --- a/src/networking/server.c +++ b/src/networking/server.c @@ -102,6 +102,24 @@ CosmsCoreServerError cosms_core_server_receive(struct cosms_core_server *current return COSMS_CORE_SERVER_OK; } +/** + * @brief Sends message to client. + */ +CosmsCoreServerError cosms_core_server_send(struct cosms_core_server *current_server, struct cosms_core_server_client *client, char *buffer, unsigned int buffer_size, unsigned int *send_bytes) { + int bytes_send = 0; + if (current_server->type == SOCK_STREAM) { + bytes_send = send(client->socket, buffer, buffer_size, 0); + } else { + bytes_send = sendto(current_server->listening_socket, buffer, buffer_size, 0, (struct sockaddr*)&client->address, client->address_length); + } + + if (send_bytes != NULL) { + (*send_bytes) = bytes_send; + } + + return COSMS_CORE_SERVER_OK; +} + /** * @brief Converts error code to a string that can be used for printing/logging errors. */