feat(server): implemented send for udp and tcp server

This commit is contained in:
Mineplay 2025-12-23 22:37:50 +01:00
parent 8c4cf93f33
commit 2e75ffaf88
2 changed files with 31 additions and 1 deletions

View file

@ -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.

View file

@ -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.
*/