adjust naming convention and initial config support
This commit is contained in:
@@ -16,11 +16,11 @@
|
||||
#include "utils/hashmap.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
int start_server(const char *hostname, const char *service) {
|
||||
int cws_server_start(const char *hostname, const char *service) {
|
||||
struct addrinfo hints;
|
||||
struct addrinfo *res;
|
||||
|
||||
setup_hints(&hints, sizeof hints, hostname);
|
||||
cws_server_setup_hints(&hints, sizeof hints, hostname);
|
||||
|
||||
int status = getaddrinfo(hostname, service, &hints, &res);
|
||||
if (status != 0) {
|
||||
@@ -43,13 +43,13 @@ int start_server(const char *hostname, const char *service) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
status = listen(sockfd, BACKLOG);
|
||||
status = listen(sockfd, CWS_SERVER_BACKLOG);
|
||||
if (status != 0) {
|
||||
fprintf(stderr, RED BOLD "[server] listen(): %s\n" RESET, gai_strerror(status));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
handle_clients(sockfd);
|
||||
cws_server_loop(sockfd);
|
||||
|
||||
freeaddrinfo(res);
|
||||
close(sockfd);
|
||||
@@ -57,7 +57,7 @@ int start_server(const char *hostname, const char *service) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void setup_hints(struct addrinfo *hints, size_t len, const char *hostname) {
|
||||
void cws_server_setup_hints(struct addrinfo *hints, size_t len, const char *hostname) {
|
||||
memset(hints, 0, len);
|
||||
/* IPv4 or IPv6 */
|
||||
hints->ai_family = AF_UNSPEC;
|
||||
@@ -69,53 +69,53 @@ void setup_hints(struct addrinfo *hints, size_t len, const char *hostname) {
|
||||
}
|
||||
}
|
||||
|
||||
void handle_clients(int sockfd) {
|
||||
void cws_server_loop(int sockfd) {
|
||||
struct sockaddr_storage their_sa;
|
||||
socklen_t theirsa_size = sizeof their_sa;
|
||||
|
||||
bucket_t clients[HASHMAP_MAX_CLIENTS];
|
||||
hm_init(clients);
|
||||
cws_bucket clients[CWS_HASHMAP_MAX_CLIENTS];
|
||||
cws_hm_init(clients);
|
||||
|
||||
int epfd = epoll_create1(0);
|
||||
setnonblocking(sockfd);
|
||||
epoll_ctl_add(epfd, sockfd, EPOLLIN | EPOLLET);
|
||||
cws_fd_set_nonblocking(sockfd);
|
||||
cws_epoll_add(epfd, sockfd, EPOLLIN | EPOLLET);
|
||||
|
||||
struct epoll_event *revents = malloc(EPOLL_MAXEVENTS * sizeof(struct epoll_event));
|
||||
struct epoll_event *revents = malloc(CWS_SERVER_EPOLL_MAXEVENTS * sizeof(struct epoll_event));
|
||||
int client_fd;
|
||||
|
||||
while (1) {
|
||||
int nfds = epoll_wait(epfd, revents, EPOLL_MAXEVENTS, EPOLL_TIMEOUT);
|
||||
int nfds = epoll_wait(epfd, revents, CWS_SERVER_EPOLL_MAXEVENTS, CWS_SERVER_EPOLL_TIMEOUT);
|
||||
|
||||
for (int i = 0; i < nfds; ++i) {
|
||||
if (revents[i].data.fd == sockfd) {
|
||||
/* New client */
|
||||
char ip[INET_ADDRSTRLEN];
|
||||
client_fd = handle_new_client(sockfd, &their_sa, &theirsa_size);
|
||||
get_client_ip(&their_sa, ip);
|
||||
client_fd = cws_server_accept_client(sockfd, &their_sa, &theirsa_size);
|
||||
cws_utils_get_client_ip(&their_sa, ip);
|
||||
fprintf(stdout, BLUE "[server] Client (%s) connected\n" RESET, ip);
|
||||
|
||||
setnonblocking(client_fd);
|
||||
epoll_ctl_add(epfd, client_fd, EPOLLIN);
|
||||
hm_push(clients, client_fd, &their_sa);
|
||||
cws_fd_set_nonblocking(client_fd);
|
||||
cws_epoll_add(epfd, client_fd, EPOLLIN);
|
||||
cws_hm_push(clients, client_fd, &their_sa);
|
||||
} else {
|
||||
char data[4096] = {0};
|
||||
/* Incoming data */
|
||||
client_fd = revents[i].data.fd;
|
||||
const ssize_t bytes_read = recv(client_fd, data, sizeof data, 0);
|
||||
char ip[INET_ADDRSTRLEN];
|
||||
bucket_t *client = hm_lookup(clients, client_fd);
|
||||
get_client_ip(&client->sas, ip);
|
||||
cws_bucket *client = cws_hm_lookup(clients, client_fd);
|
||||
cws_utils_get_client_ip(&client->sas, ip);
|
||||
|
||||
if (bytes_read == 0) {
|
||||
/* Client disconnected */
|
||||
fprintf(stdout, BLUE "[server] Client (%s) disconnected\n" RESET, ip);
|
||||
close_client(epfd, client_fd, clients);
|
||||
cws_server_close_client(epfd, client_fd, clients);
|
||||
continue;
|
||||
}
|
||||
if (bytes_read < 0) {
|
||||
if (errno != EAGAIN && errno != EWOULDBLOCK) {
|
||||
/* Error during read, handle it (close client) */
|
||||
epoll_ctl_del(epfd, client_fd);
|
||||
cws_epoll_del(epfd, client_fd);
|
||||
close(client_fd);
|
||||
}
|
||||
continue;
|
||||
@@ -124,18 +124,18 @@ void handle_clients(int sockfd) {
|
||||
data[bytes_read] = '\0';
|
||||
|
||||
/* Parse HTTP request */
|
||||
http_t *request = http_parse(data, client_fd);
|
||||
cws_http *request = cws_http_parse(data, client_fd);
|
||||
|
||||
if (request == NULL) {
|
||||
close_client(epfd, client_fd, clients);
|
||||
http_free(request);
|
||||
cws_server_close_client(epfd, client_fd, clients);
|
||||
cws_http_free(request);
|
||||
continue;
|
||||
}
|
||||
|
||||
http_send_response(request);
|
||||
cws_http_send_response(request);
|
||||
fprintf(stdout, BLUE "[server] Client (%s) disconnected\n" RESET, ip);
|
||||
close_client(epfd, client_fd, clients);
|
||||
http_free(request);
|
||||
cws_server_close_client(epfd, client_fd, clients);
|
||||
cws_http_free(request);
|
||||
|
||||
/* Clear str */
|
||||
memset(data, 0, sizeof data);
|
||||
@@ -147,11 +147,11 @@ void handle_clients(int sockfd) {
|
||||
/* TODO: fix endless loop using cli args */
|
||||
free(revents);
|
||||
close(epfd);
|
||||
close_fds(clients);
|
||||
hm_free(clients);
|
||||
cws_server_close_all_fds(clients);
|
||||
cws_hm_free(clients);
|
||||
}
|
||||
|
||||
void epoll_ctl_add(int epfd, int sockfd, uint32_t events) {
|
||||
void cws_epoll_add(int epfd, int sockfd, uint32_t events) {
|
||||
struct epoll_event event;
|
||||
event.events = events;
|
||||
event.data.fd = sockfd;
|
||||
@@ -163,7 +163,7 @@ void epoll_ctl_add(int epfd, int sockfd, uint32_t events) {
|
||||
}
|
||||
}
|
||||
|
||||
void epoll_ctl_del(int epfd, int sockfd) {
|
||||
void cws_epoll_del(int epfd, int sockfd) {
|
||||
const int status = epoll_ctl(epfd, EPOLL_CTL_DEL, sockfd, NULL);
|
||||
|
||||
if (status != 0) {
|
||||
@@ -172,7 +172,7 @@ void epoll_ctl_del(int epfd, int sockfd) {
|
||||
}
|
||||
}
|
||||
|
||||
void setnonblocking(int sockfd) {
|
||||
void cws_fd_set_nonblocking(int sockfd) {
|
||||
const int status = fcntl(sockfd, F_SETFL, O_NONBLOCK);
|
||||
|
||||
if (status == -1) {
|
||||
@@ -181,7 +181,7 @@ void setnonblocking(int sockfd) {
|
||||
}
|
||||
}
|
||||
|
||||
int handle_new_client(int sockfd, struct sockaddr_storage *their_sa, socklen_t *theirsa_size) {
|
||||
int cws_server_accept_client(int sockfd, struct sockaddr_storage *their_sa, socklen_t *theirsa_size) {
|
||||
const int client_fd = accept(sockfd, (struct sockaddr *)their_sa, theirsa_size);
|
||||
|
||||
if (client_fd == -1) {
|
||||
@@ -194,13 +194,13 @@ int handle_new_client(int sockfd, struct sockaddr_storage *their_sa, socklen_t *
|
||||
return client_fd;
|
||||
}
|
||||
|
||||
void close_fds(bucket_t *bucket) {
|
||||
for (size_t i = 0; i < HASHMAP_MAX_CLIENTS; ++i) {
|
||||
void cws_server_close_all_fds(cws_bucket *bucket) {
|
||||
for (size_t i = 0; i < CWS_HASHMAP_MAX_CLIENTS; ++i) {
|
||||
close(bucket[i].sockfd);
|
||||
if (bucket[i].next != NULL) {
|
||||
/* Close the fds */
|
||||
bucket_t *p = bucket[i].next;
|
||||
bucket_t *next = p->next;
|
||||
cws_bucket *p = bucket[i].next;
|
||||
cws_bucket *next = p->next;
|
||||
do {
|
||||
close(p->sockfd);
|
||||
p = next;
|
||||
@@ -210,8 +210,8 @@ void close_fds(bucket_t *bucket) {
|
||||
}
|
||||
}
|
||||
|
||||
void close_client(int epfd, int client_fd, bucket_t *clients) {
|
||||
epoll_ctl_del(epfd, client_fd);
|
||||
hm_remove(clients, client_fd);
|
||||
void cws_server_close_client(int epfd, int client_fd, cws_bucket *clients) {
|
||||
cws_epoll_del(epfd, client_fd);
|
||||
cws_hm_remove(clients, client_fd);
|
||||
close(client_fd);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user