feat(server): implemented listen and accept for tcp servers

This commit is contained in:
Mineplay 2025-12-25 11:42:36 +01:00
parent 2e75ffaf88
commit 3c402fea5d
2 changed files with 47 additions and 5 deletions

View file

@ -11,8 +11,12 @@
#if defined(__linux__)
#include <arpa/inet.h>
#define COSMS_CORE_SERVER_INVALID_SOCKET -1
#elif defined(_WIN32)
#include <winsock2.h>
#define COSMS_CORE_SERVER_INVALID_SOCKET (SOCKET)(0)
#endif
typedef enum {
@ -23,7 +27,9 @@ typedef enum {
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_SEND = -6
COSMS_CORE_SERVER_FAILED_TO_SEND = -6,
COSMS_CORE_SERVER_FAILED_TO_ACCEPT = -7,
COSMS_CORE_SERVER_COULD_NOT_SET_MODE = -8
} CosmsCoreServerError;
struct cosms_core_server {
@ -66,10 +72,41 @@ CosmsCoreServerError cosms_core_server_create(struct cosms_core_server *new_serv
*/
CosmsCoreServerError cosms_core_server_destroy(struct cosms_core_server *current_server);
/**
* @brief Sets server in listening state so it can listen for incomming connections (tcp only).
*
* @param current_server Pointer to an active server struct (must not be NULL)
* @param backlog How many clients will be put in the backlog before connections are declined
* @return COSMS_CORE_SERVER_OK on success, or negative error code
*/
inline CosmsCoreServerError cosms_core_server_listen(struct cosms_core_server *current_server, unsigned int backlog) {
int result = listen(current_server->listening_socket, backlog);
if (result == -1) {
return COSMS_CORE_SERVER_COULD_NOT_SET_MODE;
}
return COSMS_CORE_SERVER_OK;
}
/**
* @brief Accepts incomming connection (tcp only)
*
* @param current_server Pointer to an active server struct (must not be NULL)
* @param client Pointer to client struct to store the connected client in
* @return COSMS_CORE_SERVER_OK on success, or negative error code
*/
inline CosmsCoreServerError cosms_core_server_accept(struct cosms_core_server *current_server, struct cosms_core_server_client *client) {
client->socket = accept(current_server->listening_socket, (struct sockaddr*)&client->address, &client->address_length);
if (client->socket == COSMS_CORE_SERVER_INVALID_SOCKET) {
return COSMS_CORE_SERVER_FAILED_TO_ACCEPT;
}
return COSMS_CORE_SERVER_OK;
}
/**
* @brief Receives message from client.
*
* @param current_server Pointer to active server struct (must not be NULL)
* @param current_server Pointer to an active server struct (must not be NULL)
* @param client Pointer to client struct (must be connected to server when using tcp)
* @param buffer to store the received message in (must not be NULL)
* @param buffer_size the size of the buffer to store the message in
@ -80,7 +117,7 @@ CosmsCoreServerError cosms_core_server_receive(struct cosms_core_server *current
/**
* @brief Sends message to client.
*
* @param current_server Pointer to active server struct (must not be NULL)
* @param current_server Pointer to an 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

View file

@ -7,7 +7,6 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include "cosms-core/networking/server.h"
#include <winsock2.h>
#if defined(__linux__)
#include <unistd.h>
@ -40,7 +39,7 @@ CosmsCoreServerError cosms_core_server_create(struct cosms_core_server *new_serv
#endif
new_server->listening_socket = socket(domain, type, 0);
if (new_server->listening_socket == INVALID_SOCKET) {
if (new_server->listening_socket == COSMS_CORE_SERVER_INVALID_SOCKET) {
return COSMS_CORE_SERVER_FAILED_TO_CREATE_SOCKET;
}
@ -140,6 +139,12 @@ const char *cosms_core_server_error_string(CosmsCoreServerError error) {
case COSMS_CORE_SERVER_UNKNOWN_ERROR:
return "cosms-core-server unkown error occured";
case COSMS_CORE_SERVER_FAILED_TO_ACCEPT:
return "cosms-core-server failed to accept new connection";
case COSMS_CORE_SERVER_COULD_NOT_SET_MODE:
return "cosms-core-server could not set mode of server socket";
default:
return NULL;
}