add custom return codes
This commit is contained in:
@@ -34,7 +34,7 @@ void cws_server_setup_hints(struct addrinfo *hints, size_t len, const char *host
|
||||
}
|
||||
}
|
||||
|
||||
int cws_server_start(cws_config *config) {
|
||||
cws_server_ret cws_server_start(cws_config *config) {
|
||||
struct addrinfo hints;
|
||||
struct addrinfo *res;
|
||||
|
||||
@@ -43,44 +43,44 @@ int cws_server_start(cws_config *config) {
|
||||
int status = getaddrinfo(config->hostname, config->port, &hints, &res);
|
||||
if (status != 0) {
|
||||
CWS_LOG_ERROR("getaddrinfo() error: %s", gai_strerror(status));
|
||||
return -1;
|
||||
return CWS_SERVER_GETADDRINFO_ERROR;
|
||||
}
|
||||
|
||||
int sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
||||
if (sockfd < 0) {
|
||||
CWS_LOG_ERROR("socket(): %s", strerror(errno));
|
||||
return -1;
|
||||
return CWS_SERVER_SOCKET_ERROR;
|
||||
}
|
||||
|
||||
const int opt = 1;
|
||||
status = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt);
|
||||
if (status != 0) {
|
||||
CWS_LOG_ERROR("setsockopt(): %s", strerror(errno));
|
||||
return -1;
|
||||
return CWS_SERVER_SETSOCKOPT_ERROR;
|
||||
}
|
||||
|
||||
status = bind(sockfd, res->ai_addr, res->ai_addrlen);
|
||||
if (status != 0) {
|
||||
CWS_LOG_ERROR("bind(): %s", strerror(errno));
|
||||
return -1;
|
||||
return CWS_SERVER_BIND_ERROR;
|
||||
}
|
||||
|
||||
status = listen(sockfd, CWS_SERVER_BACKLOG);
|
||||
if (status != 0) {
|
||||
CWS_LOG_ERROR("listen(): %s", strerror(errno));
|
||||
return -1;
|
||||
return CWS_SERVER_LISTEN_ERROR;
|
||||
}
|
||||
|
||||
int ret = cws_server_loop(sockfd, config);
|
||||
cws_server_ret ret = cws_server_loop(sockfd, config);
|
||||
CWS_LOG_DEBUG("cws_server_loop ret: %d", ret);
|
||||
|
||||
freeaddrinfo(res);
|
||||
close(sockfd);
|
||||
|
||||
return 0;
|
||||
return CWS_SERVER_OK;
|
||||
}
|
||||
|
||||
int cws_server_loop(int sockfd, cws_config *config) {
|
||||
cws_server_ret cws_server_loop(int sockfd, cws_config *config) {
|
||||
mcl_hashmap *clients = mcl_hm_init(my_int_hash_fn, my_int_equal_fn, my_int_free_key_fn, my_str_free_fn);
|
||||
if (!clients) {
|
||||
return -1;
|
||||
@@ -119,13 +119,13 @@ int cws_server_loop(int sockfd, cws_config *config) {
|
||||
for (int i = 0; i < nfds; ++i) {
|
||||
if (revents[i].data.fd == sockfd) {
|
||||
ret = cws_server_handle_new_client(sockfd, epfd, clients);
|
||||
if (ret < 0) {
|
||||
CWS_LOG_DEBUG("handle new client error");
|
||||
if (ret != CWS_SERVER_OK) {
|
||||
CWS_LOG_DEBUG("%d", ret);
|
||||
}
|
||||
} else {
|
||||
ret = cws_server_handle_client_data(revents[i].data.fd, epfd, clients, config);
|
||||
if (ret < 0) {
|
||||
CWS_LOG_DEBUG("handle client data error");
|
||||
if (ret != CWS_SERVER_OK) {
|
||||
CWS_LOG_DEBUG("%d", ret);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -139,14 +139,14 @@ int cws_server_loop(int sockfd, cws_config *config) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cws_server_handle_new_client(int sockfd, int epfd, mcl_hashmap *clients) {
|
||||
cws_server_ret cws_server_handle_new_client(int sockfd, int epfd, mcl_hashmap *clients) {
|
||||
struct sockaddr_storage their_sa;
|
||||
socklen_t theirsa_size = sizeof their_sa;
|
||||
char ip[INET_ADDRSTRLEN];
|
||||
|
||||
int client_fd = cws_server_accept_client(sockfd, &their_sa, &theirsa_size);
|
||||
if (client_fd < 0) {
|
||||
return -1;
|
||||
return CWS_SERVER_FD_ERROR;
|
||||
}
|
||||
|
||||
cws_utils_get_client_ip(&their_sa, ip);
|
||||
@@ -162,10 +162,10 @@ int cws_server_handle_new_client(int sockfd, int epfd, mcl_hashmap *clients) {
|
||||
|
||||
mcl_hm_set(clients, key, value);
|
||||
|
||||
return 0;
|
||||
return CWS_SERVER_OK;
|
||||
}
|
||||
|
||||
int cws_server_handle_client_data(int client_fd, int epfd, mcl_hashmap *clients, cws_config *config) {
|
||||
cws_server_ret cws_server_handle_client_data(int client_fd, int epfd, mcl_hashmap *clients, cws_config *config) {
|
||||
char data[4096] = {0};
|
||||
char ip[INET_ADDRSTRLEN] = {0};
|
||||
|
||||
@@ -183,7 +183,7 @@ int cws_server_handle_client_data(int client_fd, int epfd, mcl_hashmap *clients,
|
||||
cws_epoll_del(epfd, client_fd);
|
||||
close(client_fd);
|
||||
|
||||
return -1;
|
||||
return CWS_SERVER_CLIENT_NOT_FOUND;
|
||||
}
|
||||
|
||||
struct sockaddr_storage client_sas = *(struct sockaddr_storage *)client->value;
|
||||
@@ -194,7 +194,7 @@ int cws_server_handle_client_data(int client_fd, int epfd, mcl_hashmap *clients,
|
||||
CWS_LOG_INFO("Client (%s) disconnected", ip);
|
||||
cws_server_close_client(epfd, client_fd, clients);
|
||||
|
||||
return -1;
|
||||
return CWS_SERVER_CLIENT_DISCONNECTED;
|
||||
}
|
||||
|
||||
if (bytes_read < 0) {
|
||||
@@ -204,7 +204,7 @@ int cws_server_handle_client_data(int client_fd, int epfd, mcl_hashmap *clients,
|
||||
cws_server_close_client(epfd, client_fd, clients);
|
||||
}
|
||||
|
||||
return -1;
|
||||
return CWS_SERVER_CLIENT_DISCONNECTED_ERROR;
|
||||
}
|
||||
|
||||
/* Parse HTTP request */
|
||||
@@ -214,16 +214,19 @@ int cws_server_handle_client_data(int client_fd, int epfd, mcl_hashmap *clients,
|
||||
CWS_LOG_INFO("Client (%s) disconnected (request NULL)", ip);
|
||||
cws_server_close_client(epfd, client_fd, clients);
|
||||
|
||||
return -1;
|
||||
return CWS_SERVER_HTTP_PARSE_ERROR;
|
||||
}
|
||||
|
||||
cws_http_send_resource(request);
|
||||
int keepalive = cws_http_send_resource(request);
|
||||
cws_http_free(request);
|
||||
if (!keepalive) {
|
||||
cws_server_close_client(epfd, client_fd, clients);
|
||||
}
|
||||
|
||||
return 0;
|
||||
return CWS_SERVER_OK;
|
||||
}
|
||||
|
||||
int cws_epoll_add(int epfd, int sockfd, uint32_t events) {
|
||||
cws_server_ret cws_epoll_add(int epfd, int sockfd, uint32_t events) {
|
||||
struct epoll_event event;
|
||||
event.events = events;
|
||||
event.data.fd = sockfd;
|
||||
@@ -231,32 +234,32 @@ int cws_epoll_add(int epfd, int sockfd, uint32_t events) {
|
||||
|
||||
if (status != 0) {
|
||||
CWS_LOG_ERROR("epoll_ctl_add(): %s", strerror(errno));
|
||||
return -1;
|
||||
return CWS_SERVER_EPOLL_ADD_ERROR;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return CWS_SERVER_OK;
|
||||
}
|
||||
|
||||
int cws_epoll_del(int epfd, int sockfd) {
|
||||
cws_server_ret cws_epoll_del(int epfd, int sockfd) {
|
||||
const int status = epoll_ctl(epfd, EPOLL_CTL_DEL, sockfd, NULL);
|
||||
|
||||
if (status != 0) {
|
||||
CWS_LOG_ERROR("epoll_ctl_del(): %s", strerror(errno));
|
||||
return -1;
|
||||
return CWS_SERVER_EPOLL_DEL_ERROR;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return CWS_SERVER_OK;
|
||||
}
|
||||
|
||||
int cws_fd_set_nonblocking(int sockfd) {
|
||||
cws_server_ret cws_fd_set_nonblocking(int sockfd) {
|
||||
const int status = fcntl(sockfd, F_SETFL, O_NONBLOCK);
|
||||
|
||||
if (status == -1) {
|
||||
CWS_LOG_ERROR("fcntl(): %s", strerror(errno));
|
||||
return -1;
|
||||
return CWS_SERVER_FD_NONBLOCKING_ERROR;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return CWS_SERVER_OK;
|
||||
}
|
||||
|
||||
int cws_server_accept_client(int sockfd, struct sockaddr_storage *their_sa, socklen_t *theirsa_size) {
|
||||
|
||||
Reference in New Issue
Block a user