improve server and fix memory leaks

This commit is contained in:
2025-08-04 00:39:23 +02:00
parent 4d8d901bd3
commit 2934de12dd
10 changed files with 251 additions and 174 deletions

View File

@@ -7,12 +7,12 @@
#include "myclib/string/mystring.h"
#include "utils/config.h"
#define CWS_HTTP_HEADER_MAX 64
#define CWS_HTTP_HEADER_CONTENT_MAX 512
#define CWS_HTTP_HEADER_MAX 512
#define CWS_HTTP_HEADER_CONTENT_MAX 1024
typedef enum cws_http_method_t {
CWS_HTTP_GET, /**< GET method */
CWS_HTTP_POST, /**< POST method */
CWS_HTTP_GET,
CWS_HTTP_POST,
CWS_HTTP_PUT,
CWS_HTTP_DELETE,
CWS_HTTP_HEAD,

View File

@@ -1,2 +1,3 @@
myclib = files('myclib/hashmap/myhashmap.c')
myclib += files('myclib/string/mystring.c')
myclib += files('myclib/queue/myqueue.c')

View File

@@ -4,7 +4,6 @@
#include <netdb.h>
#include <signal.h>
#include <sys/socket.h>
#include <time.h>
#include "myclib/hashmap/myhashmap.h"
#include "utils/config.h"
@@ -42,29 +41,10 @@ typedef enum cws_server_ret_t {
CWS_SERVER_HASHMAP_INIT,
CWS_SERVER_MALLOC_ERROR,
CWS_SERVER_REQUEST_TOO_LARGE,
CWS_SERVER_THREADPOOL_ERROR,
CWS_SERVER_EPOLL_CREATE_ERROR,
} cws_server_ret;
/* TODO: use last_activity as keep-alive */
typedef struct cws_client_t {
struct sockaddr_storage addr;
time_t last_activity;
} cws_client;
typedef struct cws_pthread_data_t {
int client_fd;
int epfd;
cws_config *config;
mcl_hashmap *clients;
} cws_pthread_data;
/**
* @brief Setups hints object
*
* @param[out] hints The hints addrinfo
* @param[in] hostname The hostname (could be NULL)
*/
void cws_server_setup_hints(struct addrinfo *hints, const char *hostname);
/**
* @brief Runs the server
*
@@ -77,7 +57,7 @@ cws_server_ret cws_server_start(cws_config *config);
*
* @param[in,out] sockfd Socket of the commincation endpoint
*/
cws_server_ret cws_server_loop(int sockfd, cws_config *config);
cws_server_ret cws_server_loop(int server_fd, cws_config *config);
/**
* @brief Adds a file descriptor to the interest list
@@ -110,7 +90,7 @@ cws_server_ret cws_fd_set_nonblocking(int sockfd);
* @param[out] their_sa Populates the struct with client's information
* @param[in] theirsa_size Size of the struct
*/
int cws_server_accept_client(int sockfd, struct sockaddr_storage *their_sa, socklen_t *theirsa_size);
int cws_server_accept_client(int server_fd, struct sockaddr_storage *their_sa, socklen_t *theirsa_size);
/**
* @brief Disconnect a client
@@ -121,7 +101,7 @@ int cws_server_accept_client(int sockfd, struct sockaddr_storage *their_sa, sock
*/
void cws_server_close_client(int epfd, int client_fd, mcl_hashmap *hashmap);
cws_server_ret cws_server_handle_new_client(int sockfd, int epfd, mcl_hashmap *clients);
int cws_server_handle_new_client(int server_fd, int epfd, mcl_hashmap *clients);
void *cws_server_handle_client_data(void *arg);
#endif

View File

@@ -1,7 +1,7 @@
#ifndef CWS_THREADPOOL_H
#define CWS_THREADPOOL_H
#include "myclib/hashmap/myhashmap.h"
#include "myclib/queue/myqueue.h"
#include "server.h"
#include "utils/config.h"
@@ -10,25 +10,26 @@
typedef struct cws_task_t {
int client_fd;
int epfd;
mcl_hashmap *clients;
cws_config *config;
} cws_task;
typedef struct cws_thread_task_t {
void (*function)(void *);
void *arg;
} cws_thread_task;
typedef struct cws_threadpool_t {
cws_task queue[CWS_MAX_QUEUE_TASKS];
int front, rear;
mcl_queue *queue;
pthread_mutex_t lock;
pthread_cond_t cond;
pthread_t threads[CWS_MAX_THREADS];
pthread_cond_t notify;
size_t threads_num;
pthread_t *threads;
int shutdown;
} cws_threadpool;
cws_threadpool *cws_threadpool_init();
cws_server_ret cws_threadpool_add_task(cws_threadpool *pool, cws_task *task);
cws_server_ret cws_threadpool_worker(cws_threadpool *pool);
cws_server_ret cws_threadpool_destroy(cws_threadpool *pool);
cws_threadpool *cws_threadpool_init(size_t threads_num, size_t queue_size);
cws_server_ret cws_threadpool_submit(cws_threadpool *pool, cws_thread_task *task);
void *cws_threadpool_worker(void *arg);
void cws_threadpool_destroy(cws_threadpool *pool);
#endif

View File

@@ -27,7 +27,7 @@
#define CWS_LOG_DEBUG(msg, ...)
#endif
#define CWS_LOG_ERROR(msg, ...) fprintf(stderr, _ERR " " msg "\n", ##__VA_ARGS__)
#define CWS_LOG_ERROR(msg, ...) fprintf(stderr, _ERR " [%s:%d] " msg "\n", __FILE__, __LINE__, ##__VA_ARGS__)
#define CWS_LOG_WARNING(msg, ...) fprintf(stdout, _WARNING " " msg "\n", ##__VA_ARGS__)
#define CWS_LOG_INFO(msg, ...) fprintf(stdout, _INFO " " msg "\n", ##__VA_ARGS__)