refactor: change project structure

This commit is contained in:
2025-10-26 17:51:41 +01:00
parent 33a12aaf73
commit 0293b0f5c0
25 changed files with 585 additions and 555 deletions

View File

@@ -0,0 +1,10 @@
#ifndef CWS_EPOLL_UTILS_H
#define CWS_EPOLL_UTILS_H
int cws_epoll_add(int epfd, int sockfd);
int cws_epoll_del(int epfd, int sockfd);
int cws_epoll_create_with_fd(int fd);
#endif

44
include/core/server.h Normal file
View File

@@ -0,0 +1,44 @@
#ifndef CWS_SERVER_H
#define CWS_SERVER_H
#include <myclib/myhashmap.h>
#include <myclib/mysocket.h>
#include <signal.h>
#include "config/config.h"
#include "core/worker.h"
#include "utils/net_utils.h"
/* Clients max queue */
#define CWS_SERVER_BACKLOG 128
/* Size of the epoll_event array */
#define CWS_SERVER_EPOLL_MAXEVENTS 64
#define CWS_SERVER_EPOLL_TIMEOUT 3000
#define CWS_SERVER_MAX_REQUEST_SIZE (16 * 1024) /* 16KB */
#define CWS_WORKERS_NUM 6
/* Main server loop */
extern volatile sig_atomic_t cws_server_run;
typedef struct cws_server {
int epfd;
int sockfd;
cws_worker_s **workers;
cws_config_s *config;
} cws_server_s;
cws_server_ret cws_server_setup(cws_server_s *server, cws_config_s *config);
cws_server_ret cws_server_start(cws_server_s *server);
void cws_server_shutdown(cws_server_s *server);
int cws_server_handle_new_client(int server_fd);
int cws_server_accept_client(int server_fd, struct sockaddr_storage *their_sa);
#endif

View File

@@ -0,0 +1,10 @@
#ifndef CWS_SOCKET_H
#define CWS_SOCKET_H
#include <myclib/mystring.h>
#include <sys/types.h>
ssize_t cws_read_data(int sockfd, string_s *str);
ssize_t cws_send_data(int sockfd, char *buffer, int len, int flags);
#endif

30
include/core/worker.h Normal file
View File

@@ -0,0 +1,30 @@
#ifndef CWS_WORKER_H
#define CWS_WORKER_H
#include <myclib/myhashmap.h>
#include <pthread.h>
#include <signal.h>
#include "config/config.h"
#include "utils/net_utils.h"
extern volatile sig_atomic_t cws_server_run;
typedef struct cws_worker {
int epfd;
size_t clients_num;
pthread_t thread;
cws_config_s *config;
} cws_worker_s;
cws_worker_s **cws_worker_new(size_t workers_num, cws_config_s *config);
void cws_worker_free(cws_worker_s **workers, size_t workers_num);
void *cws_worker_loop(void *arg);
void cws_server_close_client(int epfd, int client_fd);
cws_server_ret cws_server_handle_client_data(int epfd, int client_fd);
#endif