update myclib
This commit is contained in:
@@ -64,7 +64,7 @@ cws_http *cws_http_parse(mcl_string *request_str, int sockfd, cws_config *config
|
||||
|
||||
return NULL;
|
||||
}
|
||||
//CWS_LOG_DEBUG("method: %s", pch);
|
||||
// CWS_LOG_DEBUG("method: %s", pch);
|
||||
|
||||
int ret = cws_http_parse_method(request, pch);
|
||||
if (ret < 0) {
|
||||
@@ -85,7 +85,7 @@ cws_http *cws_http_parse(mcl_string *request_str, int sockfd, cws_config *config
|
||||
|
||||
return NULL;
|
||||
}
|
||||
//CWS_LOG_DEBUG("location: %s", pch);
|
||||
// CWS_LOG_DEBUG("location: %s", pch);
|
||||
mcl_string_append(request->location, pch);
|
||||
mcl_string_append(request->location_path, config->www);
|
||||
|
||||
@@ -95,7 +95,7 @@ cws_http *cws_http_parse(mcl_string *request_str, int sockfd, cws_config *config
|
||||
} else {
|
||||
mcl_string_append(request->location_path, mcl_string_cstr(request->location));
|
||||
}
|
||||
//CWS_LOG_DEBUG("location path: %s", mcl_string_cstr(request->location_path));
|
||||
// CWS_LOG_DEBUG("location path: %s", mcl_string_cstr(request->location_path));
|
||||
|
||||
/* Parse HTTP version */
|
||||
pch = strtok_r(NULL, " \r\n", &saveptr);
|
||||
@@ -105,7 +105,7 @@ cws_http *cws_http_parse(mcl_string *request_str, int sockfd, cws_config *config
|
||||
|
||||
return NULL;
|
||||
}
|
||||
//CWS_LOG_DEBUG("version: %s", pch);
|
||||
// CWS_LOG_DEBUG("version: %s", pch);
|
||||
mcl_string_append(request->http_version, pch);
|
||||
|
||||
/* Parse headers until a \r\n */
|
||||
|
||||
@@ -83,6 +83,7 @@ cws_server_ret cws_server_start(cws_config *config) {
|
||||
}
|
||||
|
||||
cws_server_ret cws_server_loop(int sockfd, cws_config *config) {
|
||||
/* Make the server loop multi-thread */
|
||||
mcl_hashmap *clients = mcl_hm_init(my_int_hash_fn, my_int_equal_fn, my_int_free_key_fn, my_str_free_fn, sizeof(int), sizeof(cws_client));
|
||||
if (!clients) {
|
||||
return CWS_SERVER_HASHMAP_INIT;
|
||||
@@ -116,9 +117,7 @@ cws_server_ret cws_server_loop(int sockfd, cws_config *config) {
|
||||
}
|
||||
|
||||
while (cws_server_run) {
|
||||
CWS_LOG_DEBUG("Waiting for epoll events...");
|
||||
int nfds = epoll_wait(epfd, revents, CWS_SERVER_EPOLL_MAXEVENTS, CWS_SERVER_EPOLL_TIMEOUT);
|
||||
CWS_LOG_DEBUG("epoll_wait returned %d events", nfds);
|
||||
|
||||
if (nfds == 0) {
|
||||
/* TODO: Check for inactive clients */
|
||||
@@ -132,10 +131,27 @@ cws_server_ret cws_server_loop(int sockfd, cws_config *config) {
|
||||
CWS_LOG_DEBUG("Handle new client: %d", ret);
|
||||
}
|
||||
} else {
|
||||
ret = cws_server_handle_client_data(revents[i].data.fd, epfd, clients, config);
|
||||
if (ret != CWS_SERVER_OK) {
|
||||
CWS_LOG_DEBUG("Handle client data: %d", ret);
|
||||
cws_pthread_data *thread_data = malloc(sizeof(cws_pthread_data));
|
||||
if (!thread_data) {
|
||||
continue;
|
||||
}
|
||||
pthread_t client_thread;
|
||||
|
||||
int client_fd = revents[i].data.fd;
|
||||
thread_data->client_fd = client_fd;
|
||||
thread_data->clients = clients;
|
||||
thread_data->config = config;
|
||||
thread_data->epfd = epfd;
|
||||
|
||||
ret = pthread_create(&client_thread, NULL, cws_server_handle_client_data, thread_data);
|
||||
CWS_LOG_DEBUG("Started client thread for fd: %d", client_fd);
|
||||
if (ret != 0) {
|
||||
free(thread_data);
|
||||
cws_server_close_client(epfd, client_fd, clients);
|
||||
continue;
|
||||
}
|
||||
|
||||
pthread_detach(client_thread);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -173,7 +189,14 @@ cws_server_ret cws_server_handle_new_client(int sockfd, int epfd, mcl_hashmap *c
|
||||
return CWS_SERVER_OK;
|
||||
}
|
||||
|
||||
cws_server_ret cws_server_handle_client_data(int client_fd, int epfd, mcl_hashmap *clients, cws_config *config) {
|
||||
void *cws_server_handle_client_data(void *arg) {
|
||||
cws_pthread_data *thread_data = (cws_pthread_data *)arg;
|
||||
|
||||
int client_fd = thread_data->client_fd;
|
||||
int epfd = thread_data->epfd;
|
||||
mcl_hashmap *clients = thread_data->clients;
|
||||
cws_config *config = thread_data->config;
|
||||
|
||||
char tmp_data[1024];
|
||||
memset(tmp_data, 0, sizeof(tmp_data));
|
||||
char ip[INET_ADDRSTRLEN] = {0};
|
||||
@@ -187,8 +210,9 @@ cws_server_ret cws_server_handle_client_data(int client_fd, int epfd, mcl_hashma
|
||||
if (total_bytes > CWS_SERVER_MAX_REQUEST_SIZE) {
|
||||
mcl_string_free(data);
|
||||
cws_server_close_client(epfd, client_fd, clients);
|
||||
free(thread_data);
|
||||
|
||||
return CWS_SERVER_REQUEST_TOO_LARGE;
|
||||
return NULL;
|
||||
}
|
||||
mcl_string_append(data, tmp_data);
|
||||
memset(tmp_data, 0, sizeof(tmp_data));
|
||||
@@ -199,8 +223,9 @@ cws_server_ret cws_server_handle_client_data(int client_fd, int epfd, mcl_hashma
|
||||
CWS_LOG_ERROR("recv(): %s", strerror(errno));
|
||||
mcl_string_free(data);
|
||||
cws_server_close_client(epfd, client_fd, clients);
|
||||
free(thread_data);
|
||||
|
||||
return CWS_SERVER_CLIENT_DISCONNECTED_ERROR;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Retrieve client ip */
|
||||
@@ -210,8 +235,9 @@ cws_server_ret cws_server_handle_client_data(int client_fd, int epfd, mcl_hashma
|
||||
mcl_string_free(data);
|
||||
cws_epoll_del(epfd, client_fd);
|
||||
close(client_fd);
|
||||
free(thread_data);
|
||||
|
||||
return CWS_SERVER_CLIENT_NOT_FOUND;
|
||||
return NULL;
|
||||
}
|
||||
cws_client *client_info = (cws_client *)client->value;
|
||||
cws_utils_get_client_ip(&client_info->addr, ip);
|
||||
@@ -222,8 +248,9 @@ cws_server_ret cws_server_handle_client_data(int client_fd, int epfd, mcl_hashma
|
||||
CWS_LOG_INFO("Client (%s) disconnected", ip);
|
||||
mcl_string_free(data);
|
||||
cws_server_close_client(epfd, client_fd, clients);
|
||||
free(thread_data);
|
||||
|
||||
return CWS_SERVER_CLIENT_DISCONNECTED;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Parse HTTP request */
|
||||
@@ -233,19 +260,23 @@ cws_server_ret cws_server_handle_client_data(int client_fd, int epfd, mcl_hashma
|
||||
if (request == NULL) {
|
||||
CWS_LOG_INFO("Client (%s) disconnected (request NULL)", ip);
|
||||
cws_server_close_client(epfd, client_fd, clients);
|
||||
free(thread_data);
|
||||
|
||||
return CWS_SERVER_HTTP_PARSE_ERROR;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int keepalive = cws_http_send_resource(request);
|
||||
cws_http_free(request);
|
||||
|
||||
/* Only close connection if not keep-alive */
|
||||
/* TODO: fix */
|
||||
if (keepalive <= 0) {
|
||||
cws_server_close_client(epfd, client_fd, clients);
|
||||
}
|
||||
|
||||
return CWS_SERVER_OK;
|
||||
free(thread_data);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cws_server_ret cws_epoll_add(int epfd, int sockfd, uint32_t events) {
|
||||
@@ -298,6 +329,8 @@ int cws_server_accept_client(int sockfd, struct sockaddr_storage *their_sa, sock
|
||||
}
|
||||
|
||||
void cws_server_close_client(int epfd, int client_fd, mcl_hashmap *hashmap) {
|
||||
cws_epoll_del(epfd, client_fd);
|
||||
mcl_hm_remove(hashmap, &client_fd);
|
||||
if (fcntl(client_fd, F_GETFD) != -1) {
|
||||
cws_epoll_del(epfd, client_fd);
|
||||
mcl_hm_remove(hashmap, &client_fd);
|
||||
}
|
||||
}
|
||||
|
||||
49
src/server/threadpool.c
Normal file
49
src/server/threadpool.c
Normal file
@@ -0,0 +1,49 @@
|
||||
#include "server/threadpool.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
cws_threadpool *cws_threadpool_init() {
|
||||
cws_threadpool *pool = malloc(sizeof(cws_threadpool));
|
||||
if (pool == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int ret;
|
||||
|
||||
ret = pthread_mutex_init(&pool->lock, NULL);
|
||||
if (ret != 0) {
|
||||
free(pool);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret = pthread_cond_init(&pool->cond, NULL);
|
||||
if (ret != 0) {
|
||||
free(pool);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pool->front = 0;
|
||||
pool->rear = 0;
|
||||
pool->shutdown = 0;
|
||||
|
||||
return pool;
|
||||
}
|
||||
|
||||
cws_server_ret cws_threadpool_add_task(cws_threadpool *pool, cws_task *task) {
|
||||
pthread_mutex_lock(&pool->lock);
|
||||
|
||||
/* ? */
|
||||
|
||||
pthread_mutex_unlock(&pool->lock);
|
||||
|
||||
return CWS_SERVER_OK;
|
||||
}
|
||||
|
||||
cws_server_ret cws_threadpool_worker(cws_threadpool *pool) { return CWS_SERVER_OK; }
|
||||
|
||||
cws_server_ret cws_threadpool_destroy(cws_threadpool *pool) {
|
||||
pthread_mutex_destroy(&pool->lock);
|
||||
pthread_cond_destroy(&pool->cond);
|
||||
|
||||
return CWS_SERVER_OK;
|
||||
}
|
||||
Reference in New Issue
Block a user