refactor(core): change return struct name
This commit is contained in:
@@ -31,9 +31,9 @@ typedef struct cws_server {
|
|||||||
cws_config_s *config;
|
cws_config_s *config;
|
||||||
} cws_server_s;
|
} cws_server_s;
|
||||||
|
|
||||||
cws_server_ret cws_server_setup(cws_server_s *server, cws_config_s *config);
|
cws_return cws_server_setup(cws_server_s *server, cws_config_s *config);
|
||||||
|
|
||||||
cws_server_ret cws_server_start(cws_server_s *server);
|
cws_return cws_server_start(cws_server_s *server);
|
||||||
|
|
||||||
void cws_server_shutdown(cws_server_s *server);
|
void cws_server_shutdown(cws_server_s *server);
|
||||||
|
|
||||||
|
|||||||
@@ -3,27 +3,27 @@
|
|||||||
|
|
||||||
#define ARR_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
|
#define ARR_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
|
||||||
|
|
||||||
typedef enum cws_server_ret {
|
typedef enum cws_ret {
|
||||||
CWS_SERVER_OK,
|
CWS_OK,
|
||||||
CWS_SERVER_FD_NONBLOCKING_ERROR,
|
CWS_FD_NONBLOCKING_ERROR,
|
||||||
CWS_SERVER_EPOLL_CREATE_ERROR,
|
CWS_EPOLL_CREATE_ERROR,
|
||||||
CWS_SERVER_CLIENT_DISCONNECTED_ERROR,
|
CWS_CLIENT_DISCONNECTED_ERROR,
|
||||||
CWS_SERVER_HTTP_PARSE_ERROR,
|
CWS_HTTP_PARSE_ERROR,
|
||||||
CWS_SERVER_CONFIG_ERROR,
|
CWS_CONFIG_ERROR,
|
||||||
CWS_SERVER_GETADDRINFO_ERROR,
|
CWS_GETADDRINFO_ERROR,
|
||||||
CWS_SERVER_SOCKET_ERROR,
|
CWS_SOCKET_ERROR,
|
||||||
CWS_SERVER_SETSOCKOPT_ERROR,
|
CWS_SETSOCKOPT_ERROR,
|
||||||
CWS_SERVER_BIND_ERROR,
|
CWS_BIND_ERROR,
|
||||||
CWS_SERVER_LISTEN_ERROR,
|
CWS_LISTEN_ERROR,
|
||||||
CWS_SERVER_WORKER_ERROR,
|
CWS_WORKER_ERROR,
|
||||||
CWS_SERVER_UNKNOWN_ERROR,
|
CWS_UNKNOWN_ERROR,
|
||||||
} cws_server_ret;
|
} cws_return;
|
||||||
|
|
||||||
typedef struct cws_error {
|
typedef struct cws_error {
|
||||||
cws_server_ret code;
|
cws_return code;
|
||||||
char *msg;
|
char *msg;
|
||||||
} cws_error_s;
|
} cws_error_s;
|
||||||
|
|
||||||
const char *cws_error_str(cws_server_ret code);
|
const char *cws_error_str(cws_return code);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
#include "utils/error.h"
|
#include "utils/error.h"
|
||||||
|
|
||||||
cws_server_ret cws_fd_set_nonblocking(int sockfd);
|
cws_return cws_fd_set_nonblocking(int sockfd);
|
||||||
|
|
||||||
void cws_utils_get_client_ip(struct sockaddr_storage *sa, char *ip);
|
void cws_utils_get_client_ip(struct sockaddr_storage *sa, char *ip);
|
||||||
|
|
||||||
|
|||||||
@@ -27,27 +27,27 @@ static void cws_server_setup_hints(struct addrinfo *hints, const char *hostname)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static cws_server_ret 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
cws_server_ret ret;
|
cws_return ret;
|
||||||
ret = cws_fd_set_nonblocking(server_fd);
|
ret = cws_fd_set_nonblocking(server_fd);
|
||||||
if (ret != CWS_SERVER_OK) {
|
if (ret != CWS_OK) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
cws_epoll_add(epfd, server_fd);
|
cws_epoll_add(epfd, server_fd);
|
||||||
*epfd_out = epfd;
|
*epfd_out = epfd;
|
||||||
|
|
||||||
return CWS_SERVER_OK;
|
return CWS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
cws_server_ret 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_SERVER_CONFIG_ERROR;
|
return CWS_CONFIG_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(server, 0, sizeof *server);
|
memset(server, 0, sizeof *server);
|
||||||
@@ -61,52 +61,52 @@ cws_server_ret cws_server_setup(cws_server_s *server, cws_config_s *config) {
|
|||||||
int status = getaddrinfo(config->hostname, config->port, &hints, &res);
|
int status = getaddrinfo(config->hostname, config->port, &hints, &res);
|
||||||
if (status != 0) {
|
if (status != 0) {
|
||||||
CWS_LOG_ERROR("getaddrinfo() error: %s", gai_strerror(status));
|
CWS_LOG_ERROR("getaddrinfo() error: %s", gai_strerror(status));
|
||||||
return CWS_SERVER_GETADDRINFO_ERROR;
|
return CWS_GETADDRINFO_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
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_SERVER_SOCKET_ERROR;
|
return CWS_SOCKET_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
CWS_LOG_ERROR("setsockopt(): %s", strerror(errno));
|
CWS_LOG_ERROR("setsockopt(): %s", strerror(errno));
|
||||||
return CWS_SERVER_SETSOCKOPT_ERROR;
|
return CWS_SETSOCKOPT_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
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));
|
||||||
return CWS_SERVER_BIND_ERROR;
|
return CWS_BIND_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
status = listen(server->sockfd, CWS_SERVER_BACKLOG);
|
status = listen(server->sockfd, CWS_SERVER_BACKLOG);
|
||||||
if (status != 0) {
|
if (status != 0) {
|
||||||
CWS_LOG_ERROR("listen(): %s", strerror(errno));
|
CWS_LOG_ERROR("listen(): %s", strerror(errno));
|
||||||
return CWS_SERVER_LISTEN_ERROR;
|
return CWS_LISTEN_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
freeaddrinfo(res);
|
freeaddrinfo(res);
|
||||||
|
|
||||||
/* Setup epoll */
|
/* Setup epoll */
|
||||||
cws_server_ret ret = cws_server_setup_epoll(server->sockfd, &server->epfd);
|
cws_return ret = cws_server_setup_epoll(server->sockfd, &server->epfd);
|
||||||
if (ret != CWS_SERVER_OK) {
|
if (ret != CWS_OK) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Setup workers */
|
/* Setup workers */
|
||||||
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_SERVER_WORKER_ERROR;
|
return CWS_WORKER_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
return CWS_SERVER_OK;
|
return CWS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
cws_server_ret 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);
|
||||||
int client_fd = 0;
|
int client_fd = 0;
|
||||||
@@ -138,7 +138,7 @@ cws_server_ret cws_server_start(cws_server_s *server) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return CWS_SERVER_OK;
|
return CWS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cws_server_handle_new_client(int server_fd) {
|
int cws_server_handle_new_client(int server_fd) {
|
||||||
|
|||||||
@@ -38,16 +38,16 @@ int main(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cws_server_s server;
|
cws_server_s server;
|
||||||
cws_server_ret ret;
|
cws_return ret;
|
||||||
|
|
||||||
ret = cws_server_setup(&server, config);
|
ret = cws_server_setup(&server, config);
|
||||||
if (ret != CWS_SERVER_OK) {
|
if (ret != CWS_OK) {
|
||||||
CWS_LOG_ERROR("Unable to setup web server: %s", cws_error_str(ret));
|
CWS_LOG_ERROR("Unable to setup web server: %s", cws_error_str(ret));
|
||||||
}
|
}
|
||||||
|
|
||||||
CWS_LOG_INFO("Running cws on http://%s:%s...", config->hostname, config->port);
|
CWS_LOG_INFO("Running cws on http://%s:%s...", config->hostname, config->port);
|
||||||
ret = cws_server_start(&server);
|
ret = cws_server_start(&server);
|
||||||
if (ret != CWS_SERVER_OK) {
|
if (ret != CWS_OK) {
|
||||||
CWS_LOG_ERROR("Unable to start web server: %s", cws_error_str(ret));
|
CWS_LOG_ERROR("Unable to start web server: %s", cws_error_str(ret));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,21 +3,21 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
static const cws_error_s errors[] = {
|
static const cws_error_s errors[] = {
|
||||||
{CWS_SERVER_OK, "No error"},
|
{CWS_OK, "No error"},
|
||||||
{CWS_SERVER_FD_NONBLOCKING_ERROR, "Failed to set socket as non-blocking"},
|
{CWS_FD_NONBLOCKING_ERROR, "Failed to set socket as non-blocking"},
|
||||||
{CWS_SERVER_EPOLL_CREATE_ERROR, "Failed to create epoll instance"},
|
{CWS_EPOLL_CREATE_ERROR, "Failed to create epoll instance"},
|
||||||
{CWS_SERVER_CLIENT_DISCONNECTED_ERROR, "Client disconnected"},
|
{CWS_CLIENT_DISCONNECTED_ERROR, "Client disconnected"},
|
||||||
{CWS_SERVER_HTTP_PARSE_ERROR, "Failed to parse HTTP request"},
|
{CWS_HTTP_PARSE_ERROR, "Failed to parse HTTP request"},
|
||||||
{CWS_SERVER_CONFIG_ERROR, "Invalid server configuration"},
|
{CWS_CONFIG_ERROR, "Invalid server configuration"},
|
||||||
{CWS_SERVER_GETADDRINFO_ERROR, "getaddrinfo() failed"},
|
{CWS_GETADDRINFO_ERROR, "getaddrinfo() failed"},
|
||||||
{CWS_SERVER_SOCKET_ERROR, "Failed to create socket"},
|
{CWS_SOCKET_ERROR, "Failed to create socket"},
|
||||||
{CWS_SERVER_SETSOCKOPT_ERROR, "setsockopt() failed"},
|
{CWS_SETSOCKOPT_ERROR, "setsockopt() failed"},
|
||||||
{CWS_SERVER_BIND_ERROR, "bind() failed"},
|
{CWS_BIND_ERROR, "bind() failed"},
|
||||||
{CWS_SERVER_LISTEN_ERROR, "listen() failed"},
|
{CWS_LISTEN_ERROR, "listen() failed"},
|
||||||
{CWS_SERVER_WORKER_ERROR, "Worker thread initialization failed"},
|
{CWS_WORKER_ERROR, "Worker thread initialization failed"},
|
||||||
{CWS_SERVER_UNKNOWN_ERROR, "Unknown error"}};
|
{CWS_UNKNOWN_ERROR, "Unknown error"}};
|
||||||
|
|
||||||
const char *cws_error_str(cws_server_ret code) {
|
const char *cws_error_str(cws_return code) {
|
||||||
for (size_t i = 0; i < ARR_SIZE(errors); ++i) {
|
for (size_t i = 0; i < ARR_SIZE(errors); ++i) {
|
||||||
if (errors[i].code == code)
|
if (errors[i].code == code)
|
||||||
return errors[i].msg;
|
return errors[i].msg;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "utils/net.h"
|
#include "utils/net.h"
|
||||||
|
#include "utils/error.h"
|
||||||
|
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
@@ -20,12 +21,12 @@ void cws_utils_get_client_ip(struct sockaddr_storage *sa, char *ip) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cws_server_ret cws_fd_set_nonblocking(int sockfd) {
|
cws_return cws_fd_set_nonblocking(int sockfd) {
|
||||||
const int status = fcntl(sockfd, F_SETFL, O_NONBLOCK);
|
const int status = fcntl(sockfd, F_SETFL, O_NONBLOCK);
|
||||||
|
|
||||||
if (status == -1) {
|
if (status == -1) {
|
||||||
return CWS_SERVER_FD_NONBLOCKING_ERROR;
|
return CWS_FD_NONBLOCKING_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
return CWS_SERVER_OK;
|
return CWS_OK;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user