diff --git a/include/core/worker.h b/include/core/worker.h index 6175bda..29aa82e 100644 --- a/include/core/worker.h +++ b/include/core/worker.h @@ -20,6 +20,4 @@ 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); - #endif diff --git a/src/core/worker.c b/src/core/worker.c index ae67855..2066f9e 100644 --- a/src/core/worker.c +++ b/src/core/worker.c @@ -11,52 +11,86 @@ #include "http/response.h" #include "utils/error.h" -static cws_server_ret cws_worker_setup_epoll(cws_worker_s *worker) { +static cws_return worker_setup_epoll(cws_worker_s *worker) { worker->epfd = epoll_create1(0); if (worker->epfd == -1) { - return CWS_SERVER_EPOLL_CREATE_ERROR; + return CWS_EPOLL_CREATE_ERROR; } - return CWS_SERVER_OK; + return CWS_OK; } -static void cws_server_close_client(int epfd, int client_fd) { +static void worker_close_client(int epfd, int client_fd) { cws_epoll_del(epfd, client_fd); close(client_fd); } -static int cws_server_handle_client_data(int epfd, int client_fd) { - string_s *data = string_new("", 4096); - +static cws_return worker_read_data(int epfd, int client_fd, string_s *data) { 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; + return CWS_OK; } if (total_bytes < 0) { /* Something happened, close connection */ - string_free(data); - cws_server_close_client(epfd, client_fd); + worker_close_client(epfd, client_fd); - return CWS_SERVER_CLIENT_DISCONNECTED_ERROR; + return CWS_CLIENT_DISCONNECTED_ERROR; + } + + return CWS_OK; +} + +static cws_return worker_handle_client_data(int epfd, int client_fd) { + string_s *data = string_new("", 4096); + cws_return ret = worker_read_data(epfd, client_fd, data); + if (ret != CWS_OK) { + string_free(data); + return ret; } cws_request_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; + worker_close_client(epfd, client_fd); + return CWS_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); + worker_close_client(epfd, client_fd); - return CWS_SERVER_OK; + return CWS_OK; +} + +static 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; + worker_handle_client_data(worker->epfd, client_fd); + } + } + + return NULL; } cws_worker_s **cws_worker_new(size_t workers_num, cws_config_s *config) { @@ -81,7 +115,7 @@ cws_worker_s **cws_worker_new(size_t workers_num, cws_config_s *config) { workers[i]->config = config; /* Setup worker's epoll */ - int ret = cws_worker_setup_epoll(workers[i]); + int ret = worker_setup_epoll(workers[i]); if (ret == -1) { for (size_t j = 0; j < i; ++j) { free(workers[j]); @@ -111,30 +145,3 @@ void cws_worker_free(cws_worker_s **workers, size_t workers_num) { 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; -}