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