refactor(server): remove useless comments
This commit is contained in:
@@ -12,26 +12,23 @@
|
||||
#include "utils/net.h"
|
||||
#include <unistd.h>
|
||||
|
||||
/* Prepare addrinfo hints for getaddrinfo() */
|
||||
static void cws_server_setup_hints(struct addrinfo *hints, const char *hostname) {
|
||||
memset(hints, 0, sizeof *hints);
|
||||
|
||||
hints->ai_family = AF_UNSPEC; /* Accept IPv4 or IPv6 */
|
||||
hints->ai_socktype = SOCK_STREAM; /* TCP socket */
|
||||
hints->ai_family = AF_UNSPEC;
|
||||
hints->ai_socktype = SOCK_STREAM;
|
||||
|
||||
if (hostname == NULL) {
|
||||
hints->ai_flags = AI_PASSIVE; /* Bind to all interfaces */
|
||||
hints->ai_flags = AI_PASSIVE;
|
||||
}
|
||||
}
|
||||
|
||||
/* Create epoll, set listening socket nonblocking, and register it */
|
||||
static cws_return cws_server_setup_epoll(int server_fd, int *epfd_out) {
|
||||
int epfd = epoll_create1(0);
|
||||
if (epfd < 0) {
|
||||
return epfd;
|
||||
}
|
||||
|
||||
/* Listening socket must be nonblocking for epoll-driven accept loop */
|
||||
cws_return ret = cws_fd_set_nonblocking(server_fd);
|
||||
if (ret != CWS_OK) {
|
||||
return ret;
|
||||
@@ -43,7 +40,6 @@ static cws_return cws_server_setup_epoll(int server_fd, int *epfd_out) {
|
||||
return CWS_OK;
|
||||
}
|
||||
|
||||
/* Initialize listening socket, epoll instance, and worker threads */
|
||||
cws_return cws_server_setup(cws_server_s *server, cws_config_s *config) {
|
||||
if (!config || !config->hostname || !config->port) {
|
||||
return CWS_CONFIG_ERROR;
|
||||
@@ -51,7 +47,6 @@ cws_return cws_server_setup(cws_server_s *server, cws_config_s *config) {
|
||||
|
||||
memset(server, 0, sizeof *server);
|
||||
|
||||
/* Resolve hostname/port */
|
||||
struct addrinfo hints;
|
||||
struct addrinfo *res;
|
||||
cws_server_setup_hints(&hints, config->hostname);
|
||||
@@ -62,14 +57,12 @@ cws_return cws_server_setup(cws_server_s *server, cws_config_s *config) {
|
||||
return CWS_GETADDRINFO_ERROR;
|
||||
}
|
||||
|
||||
/* Create listening socket */
|
||||
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_SOCKET_ERROR;
|
||||
}
|
||||
|
||||
/* Allow fast reuse of the port on restart */
|
||||
const int opt = 1;
|
||||
status = setsockopt(server->sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt);
|
||||
if (status != 0) {
|
||||
@@ -77,7 +70,6 @@ cws_return cws_server_setup(cws_server_s *server, cws_config_s *config) {
|
||||
return CWS_SETSOCKOPT_ERROR;
|
||||
}
|
||||
|
||||
/* Bind + listen on the configured address */
|
||||
status = bind(server->sockfd, res->ai_addr, res->ai_addrlen);
|
||||
if (status != 0) {
|
||||
CWS_LOG_ERROR("bind(): %s", strerror(errno));
|
||||
@@ -92,13 +84,11 @@ cws_return cws_server_setup(cws_server_s *server, cws_config_s *config) {
|
||||
|
||||
freeaddrinfo(res);
|
||||
|
||||
/* Setup epoll for accepting new clients */
|
||||
cws_return ret = cws_server_setup_epoll(server->sockfd, &server->epfd);
|
||||
if (ret != CWS_OK) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Spawn worker threads */
|
||||
server->workers = cws_worker_new(CWS_WORKERS_NUM, config);
|
||||
if (server->workers == NULL) {
|
||||
return CWS_WORKER_ERROR;
|
||||
@@ -107,7 +97,6 @@ cws_return cws_server_setup(cws_server_s *server, cws_config_s *config) {
|
||||
return CWS_OK;
|
||||
}
|
||||
|
||||
/* Main event loop: accept clients and distribute them among workers */
|
||||
cws_return cws_server_start(cws_server_s *server) {
|
||||
struct epoll_event events[128];
|
||||
memset(events, 0, sizeof events);
|
||||
@@ -117,7 +106,6 @@ cws_return cws_server_start(cws_server_s *server) {
|
||||
while (cws_server_run) {
|
||||
int nfds = epoll_wait(server->epfd, events, 128, -1);
|
||||
|
||||
/* Epoll error */
|
||||
if (nfds < 0) {
|
||||
continue;
|
||||
}
|
||||
@@ -127,13 +115,11 @@ cws_return cws_server_start(cws_server_s *server) {
|
||||
}
|
||||
|
||||
for (int i = 0; i < nfds; ++i) {
|
||||
/* Accept incoming connection */
|
||||
int client_fd = cws_server_handle_new_client(server->sockfd);
|
||||
if (client_fd < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Assign client to next worker using round-robin */
|
||||
cws_fd_set_nonblocking(client_fd);
|
||||
cws_epoll_add(server->workers[workers_index]->epfd, client_fd);
|
||||
workers_index = (workers_index + 1) % CWS_WORKERS_NUM;
|
||||
@@ -143,7 +129,6 @@ cws_return cws_server_start(cws_server_s *server) {
|
||||
return CWS_OK;
|
||||
}
|
||||
|
||||
/* Accept client + log IP */
|
||||
int cws_server_handle_new_client(int server_fd) {
|
||||
struct sockaddr_storage their_sa;
|
||||
char ip[INET_ADDRSTRLEN];
|
||||
@@ -159,7 +144,6 @@ int cws_server_handle_new_client(int server_fd) {
|
||||
return client_fd;
|
||||
}
|
||||
|
||||
/* Wrapper around accept() with logging and error handling */
|
||||
int cws_server_accept_client(int server_fd, struct sockaddr_storage *their_sa) {
|
||||
socklen_t theirsa_size = sizeof(struct sockaddr_storage);
|
||||
|
||||
@@ -174,7 +158,6 @@ int cws_server_accept_client(int server_fd, struct sockaddr_storage *their_sa) {
|
||||
return client_fd;
|
||||
}
|
||||
|
||||
/* Close sockets, epoll, and workers */
|
||||
void cws_server_shutdown(cws_server_s *server) {
|
||||
if (!server) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user