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

32
src/core/epoll_utils.c Normal file
View File

@@ -0,0 +1,32 @@
#include "core/epoll_utils.h"
#include <stdio.h>
#include <sys/epoll.h>
int cws_epoll_add(int epfd, int sockfd) {
struct epoll_event event;
event.events = EPOLLIN;
event.data.fd = sockfd;
const int status = epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &event);
return status;
}
int cws_epoll_del(int epfd, int sockfd) {
const int status = epoll_ctl(epfd, EPOLL_CTL_DEL, sockfd, NULL);
return status;
}
int cws_epoll_create_with_fd(int fd) {
int epfd = epoll_create1(0);
if (epfd == -1) {
return -1;
}
if (cws_epoll_add(epfd, fd) != 0) {
return -1;
}
return epfd;
}

190
src/core/server.c Normal file
View File

@@ -0,0 +1,190 @@
#include "core/server.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/epoll.h>
#include <unistd.h>
#include "core/epoll_utils.h"
#include "core/worker.h"
#include "utils/debug.h"
#include "utils/net_utils.h"
static void cws_server_setup_hints(struct addrinfo *hints, const char *hostname) {
memset(hints, 0, sizeof *hints);
/* IPv4 or IPv6 */
hints->ai_family = AF_UNSPEC;
/* TCP */
hints->ai_socktype = SOCK_STREAM;
if (hostname == NULL) {
/* Fill in IP for me */
hints->ai_flags = AI_PASSIVE;
}
}
static cws_server_ret cws_server_setup_epoll(int server_fd, int *epfd_out) {
int epfd = epoll_create1(0);
if (epfd < 0) {
return epfd;
}
cws_server_ret ret;
ret = cws_fd_set_nonblocking(server_fd);
if (ret != CWS_SERVER_OK) {
return ret;
}
cws_epoll_add(epfd, server_fd);
*epfd_out = epfd;
return CWS_SERVER_OK;
}
cws_server_ret cws_server_setup(cws_server_s *server, cws_config_s *config) {
if (!config || !config->hostname || !config->port) {
return CWS_SERVER_CONFIG;
}
memset(server, 0, sizeof *server);
/* Setup basic stuff */
struct addrinfo hints;
struct addrinfo *res;
cws_server_setup_hints(&hints, config->hostname);
int status = getaddrinfo(config->hostname, config->port, &hints, &res);
if (status != 0) {
CWS_LOG_ERROR("getaddrinfo() error: %s", gai_strerror(status));
return CWS_SERVER_GETADDRINFO_ERROR;
}
server->sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (server->sockfd < 0) {
CWS_LOG_ERROR("socket(): %s", strerror(errno));
return CWS_SERVER_SOCKET_ERROR;
}
const int opt = 1;
status = setsockopt(server->sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt);
if (status != 0) {
CWS_LOG_ERROR("setsockopt(): %s", strerror(errno));
return CWS_SERVER_SETSOCKOPT_ERROR;
}
status = bind(server->sockfd, res->ai_addr, res->ai_addrlen);
if (status != 0) {
CWS_LOG_ERROR("bind(): %s", strerror(errno));
return CWS_SERVER_BIND_ERROR;
}
status = listen(server->sockfd, CWS_SERVER_BACKLOG);
if (status != 0) {
CWS_LOG_ERROR("listen(): %s", strerror(errno));
return CWS_SERVER_LISTEN_ERROR;
}
freeaddrinfo(res);
/* Setup epoll */
cws_server_ret ret = cws_server_setup_epoll(server->sockfd, &server->epfd);
if (ret != CWS_SERVER_OK) {
return ret;
}
/* Setup workers */
server->workers = cws_worker_new(CWS_WORKERS_NUM, config);
if (server->workers == NULL) {
return CWS_SERVER_WORKER_ERROR;
}
return CWS_SERVER_OK;
}
cws_server_ret cws_server_start(cws_server_s *server) {
struct epoll_event events[128];
memset(events, 0, sizeof events);
int client_fd = 0;
size_t workers_index = 0;
while (cws_server_run) {
int nfds = epoll_wait(server->epfd, events, 128, -1);
/* epoll error */
if (nfds < 0) {
continue;
}
/* No events */
if (nfds == 0) {
continue;
}
for (int i = 0; i < nfds; ++i) {
if (events[i].data.fd == server->sockfd) {
client_fd = cws_server_handle_new_client(server->sockfd);
if (client_fd < 0) {
continue;
}
/* Add client to a worker */
cws_fd_set_nonblocking(client_fd);
cws_epoll_add(server->workers[workers_index]->epfd, client_fd);
workers_index = (workers_index + 1) % CWS_WORKERS_NUM;
}
}
}
return CWS_SERVER_OK;
}
int cws_server_handle_new_client(int server_fd) {
struct sockaddr_storage their_sa;
char ip[INET_ADDRSTRLEN];
int client_fd = cws_server_accept_client(server_fd, &their_sa);
if (client_fd < 0) {
return client_fd;
}
cws_utils_get_client_ip(&their_sa, ip);
CWS_LOG_INFO("Client (%s) (fd: %d) connected", ip, client_fd);
return client_fd;
}
int cws_server_accept_client(int server_fd, struct sockaddr_storage *their_sa) {
socklen_t theirsa_size = sizeof(struct sockaddr_storage);
const int client_fd = accept(server_fd, (struct sockaddr *)their_sa, &theirsa_size);
if (client_fd == -1) {
if (errno != EWOULDBLOCK) {
CWS_LOG_ERROR("accept(): %s", strerror(errno));
}
}
return client_fd;
}
void cws_server_shutdown(cws_server_s *server) {
if (!server) {
return;
}
if (server->sockfd > 0) {
sock_close(server->sockfd);
}
if (server->epfd > 0) {
sock_close(server->epfd);
}
if (server->workers) {
cws_worker_free(server->workers, CWS_WORKERS_NUM);
}
}

53
src/core/socket_utils.c Normal file
View File

@@ -0,0 +1,53 @@
#include "core/socket_utils.h"
#include <errno.h>
#include <sys/socket.h>
ssize_t cws_read_data(int sockfd, string_s *str) {
char tmp[4096] = {0};
ssize_t n = recv(sockfd, tmp, sizeof tmp, 0);
if (n < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
/* No data available right now */
return 0;
}
return -1;
}
if (n == 0) {
/* Connection closed */
return -1;
}
tmp[n] = '\0';
string_append(str, tmp);
return n;
}
ssize_t cws_send_data(int sockfd, char *buffer, int len, int flags) {
ssize_t total_sent = 0;
ssize_t n;
while (total_sent < len) {
n = send(sockfd, buffer + total_sent, len - total_sent, flags);
if (n < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
/* Buffer full, return bytes sent */
break;
}
return -1;
}
if (n == 0) {
break;
}
total_sent += n;
}
return total_sent;
}

141
src/core/worker.c Normal file
View File

@@ -0,0 +1,141 @@
#include "core/worker.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <unistd.h>
#include "core/epoll_utils.h"
#include "core/socket_utils.h"
#include "http/request.h"
#include "utils/net_utils.h"
static cws_server_ret cws_worker_setup_epoll(cws_worker_s *worker) {
worker->epfd = epoll_create1(0);
if (worker->epfd == -1) {
return CWS_SERVER_EPOLL_CREATE_ERROR;
}
return CWS_SERVER_OK;
}
cws_worker_s **cws_worker_new(size_t workers_num, cws_config_s *config) {
cws_worker_s **workers = malloc(workers_num * sizeof *workers);
if (workers == NULL) {
return NULL;
}
memset(workers, 0, workers_num * sizeof *workers);
for (size_t i = 0; i < workers_num; ++i) {
workers[i] = malloc(sizeof(cws_worker_s));
if (workers[i] == NULL) {
for (size_t j = 0; j < i; ++j) {
free(workers[j]);
}
free(workers);
return NULL;
}
memset(workers[i], 0, sizeof **workers);
workers[i]->config = config;
/* Setup worker's epoll */
int ret = cws_worker_setup_epoll(workers[i]);
if (ret == -1) {
for (size_t j = 0; j < i; ++j) {
free(workers[j]);
}
free(workers);
return NULL;
}
}
for (size_t i = 0; i < workers_num; ++i) {
pthread_create(&workers[i]->thread, NULL, cws_worker_loop, workers[i]);
}
return workers;
}
void cws_worker_free(cws_worker_s **workers, size_t workers_num) {
if (!workers) {
return;
}
for (size_t i = 0; i < workers_num; ++i) {
pthread_join(workers[i]->thread, NULL);
free(workers[i]);
}
free(workers);
}
void *cws_worker_loop(void *arg) {
cws_worker_s *worker = arg;
struct epoll_event events[64];
int nfds;
while (cws_server_run) {
nfds = epoll_wait(worker->epfd, events, 64, 250);
if (nfds < 0) {
continue;
}
if (nfds == 0) {
continue;
}
for (int i = 0; i < nfds; ++i) {
/* Handle client's data */
int client_fd = events[i].data.fd;
cws_server_handle_client_data(worker->epfd, client_fd);
}
}
return NULL;
}
void cws_server_close_client(int epfd, int client_fd) {
cws_epoll_del(epfd, client_fd);
close(client_fd);
}
cws_server_ret cws_server_handle_client_data(int epfd, int client_fd) {
string_s *data = string_new("", 4096);
ssize_t total_bytes = cws_read_data(client_fd, data);
if (total_bytes == 0) {
/* Request not completed yet */
string_free(data);
return CWS_SERVER_OK;
}
if (total_bytes <= 0) {
/* Something happened, close connection */
string_free(data);
cws_server_close_client(epfd, client_fd);
return CWS_SERVER_CLIENT_DISCONNECTED_ERROR;
}
cws_http_s *request = cws_http_parse(data);
string_free(data);
if (request == NULL) {
cws_server_close_client(epfd, client_fd);
return CWS_SERVER_HTTP_PARSE_ERROR;
}
request->sockfd = client_fd;
cws_http_send_response(request, HTTP_OK);
cws_http_free(request);
cws_server_close_client(epfd, client_fd);
return CWS_SERVER_OK;
}