Files
cws/src/core/server.c
T

178 lines
3.9 KiB
C

#include "core/server.h"
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/epoll.h>
#include "core/epoll.h"
#include "core/worker.h"
#include "utils/debug.h"
#include "utils/error.h"
#include "utils/net.h"
#include <unistd.h>
static void cws_server_setup_hints(struct addrinfo *hints, const char *hostname) {
memset(hints, 0, sizeof *hints);
hints->ai_family = AF_UNSPEC;
hints->ai_socktype = SOCK_STREAM;
if (hostname == NULL) {
hints->ai_flags = AI_PASSIVE;
}
}
static cws_return cws_server_setup_epoll(int server_fd, int *epfd_out) {
int epfd = epoll_create1(0);
if (epfd < 0) {
return epfd;
}
cws_return ret = cws_fd_set_nonblocking(server_fd);
if (ret != CWS_OK) {
return ret;
}
cws_epoll_add(epfd, server_fd);
*epfd_out = epfd;
return CWS_OK;
}
cws_return cws_server_setup(cws_server_s *server, cws_config_s *config) {
if (!config || !config->host || !config->port) {
return CWS_CONFIG_ERROR;
}
memset(server, 0, sizeof *server);
struct addrinfo hints;
struct addrinfo *res;
cws_server_setup_hints(&hints, config->host);
int status = getaddrinfo(config->host, config->port, &hints, &res);
if (status != 0) {
cws_log_error("getaddrinfo() error: %s", gai_strerror(status));
return CWS_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_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_SETSOCKOPT_ERROR;
}
status = bind(server->sockfd, res->ai_addr, res->ai_addrlen);
if (status != 0) {
cws_log_error("bind(): %s", strerror(errno));
return CWS_BIND_ERROR;
}
status = listen(server->sockfd, CWS_SERVER_BACKLOG);
if (status != 0) {
cws_log_error("listen(): %s", strerror(errno));
return CWS_LISTEN_ERROR;
}
freeaddrinfo(res);
cws_return ret = cws_server_setup_epoll(server->sockfd, &server->epfd);
if (ret != CWS_OK) {
return ret;
}
server->workers = cws_worker_new(config->workers, config);
if (server->workers == NULL) {
return CWS_WORKER_ERROR;
}
return CWS_OK;
}
cws_return cws_server_start(cws_server_s *server) {
struct epoll_event events[128];
memset(events, 0, sizeof events);
size_t workers_index = 0;
while (cws_server_run) {
int nfds = epoll_wait(server->epfd, events, CWS_SERVER_EPOLL_MAXEVENTS, CWS_SERVER_EPOLL_TIMEOUT);
if (nfds < 0) {
continue;
}
if (nfds == 0) {
continue;
}
for (int i = 0; i < nfds; ++i) {
int client_fd = cws_server_handle_new_client(server->sockfd);
if (client_fd < 0) {
continue;
}
cws_fd_set_nonblocking(client_fd);
cws_epoll_add(server->workers[workers_index]->epfd, client_fd);
workers_index = (workers_index + 1) % server->config->workers;
}
}
return CWS_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) {
close(server->sockfd);
}
if (server->epfd > 0) {
close(server->epfd);
}
if (server->workers) {
cws_worker_free(server->workers, server->config->workers);
}
}