move to nginx-like worker eventloop
This commit is contained in:
@@ -4,7 +4,6 @@
|
||||
#include <netdb.h>
|
||||
#include <signal.h>
|
||||
#include <sys/socket.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "myclib/hashmap/myhashmap.h"
|
||||
#include "utils/config.h"
|
||||
@@ -44,70 +43,13 @@ typedef enum cws_server_ret_t {
|
||||
CWS_SERVER_REQUEST_TOO_LARGE,
|
||||
CWS_SERVER_THREADPOOL_ERROR,
|
||||
CWS_SERVER_EPOLL_CREATE_ERROR,
|
||||
CWS_SERVER_WORKER_ERROR,
|
||||
} cws_server_ret;
|
||||
|
||||
typedef struct cws_client_info_t {
|
||||
time_t last_activity;
|
||||
int client_fd;
|
||||
} cws_client_info;
|
||||
|
||||
/**
|
||||
* @brief Runs the server
|
||||
*
|
||||
* @param[in] config The server's config
|
||||
*/
|
||||
cws_server_ret cws_server_start(cws_config *config);
|
||||
|
||||
/**
|
||||
* @brief Main server loop
|
||||
*
|
||||
* @param[in,out] sockfd Socket of the commincation endpoint
|
||||
*/
|
||||
cws_server_ret cws_server_loop(int server_fd, cws_config *config);
|
||||
|
||||
/**
|
||||
* @brief Adds a file descriptor to the interest list
|
||||
*
|
||||
* @param[in] epfd epoll file descriptor
|
||||
* @param[in] sockfd The file descriptor to watch
|
||||
* @param[in] events The events to follow
|
||||
*/
|
||||
cws_server_ret cws_epoll_add(int epfd, int sockfd, uint32_t events);
|
||||
|
||||
/**
|
||||
* @brief Removes a file descriptor from the interest list
|
||||
*
|
||||
* @param[in] epfd epoll file descriptor
|
||||
* @param[in] sockfd The file descriptor to remove
|
||||
*/
|
||||
cws_server_ret cws_epoll_del(int epfd, int sockfd);
|
||||
|
||||
/**
|
||||
* @brief Makes a file descriptor non-blocking
|
||||
*
|
||||
* @param[in] sockfd The file descriptor to make non-blocking
|
||||
*/
|
||||
int cws_server_handle_new_client(int server_fd, mcl_hashmap *clients);
|
||||
int cws_server_accept_client(int server_fd, struct sockaddr_storage *their_sa, socklen_t *theirsa_size);
|
||||
cws_server_ret cws_fd_set_nonblocking(int sockfd);
|
||||
|
||||
/**
|
||||
* @brief Handles the new client
|
||||
*
|
||||
* @param[in] sockfd Server's file descriptor
|
||||
* @param[out] their_sa Populates the struct with client's information
|
||||
* @param[in] theirsa_size Size of the struct
|
||||
*/
|
||||
int cws_server_accept_client(int server_fd, struct sockaddr_storage *their_sa, socklen_t *theirsa_size);
|
||||
|
||||
/**
|
||||
* @brief Disconnect a client
|
||||
*
|
||||
* @param[in] epfd Epoll file descriptor
|
||||
* @param[in] client_fd Client file descriptor
|
||||
* @param[in] clients Clients hash map
|
||||
*/
|
||||
void cws_server_close_client(int epfd, int client_fd, mcl_hashmap *clients);
|
||||
|
||||
int cws_server_handle_new_client(int server_fd, int epfd, mcl_hashmap *clients);
|
||||
void *cws_server_handle_client_data(void *arg);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
#ifndef CWS_THREADPOOL_H
|
||||
#define CWS_THREADPOOL_H
|
||||
|
||||
#include "myclib/queue/myqueue.h"
|
||||
#include "server.h"
|
||||
#include "utils/config.h"
|
||||
|
||||
#define CWS_MAX_QUEUE_TASKS 16
|
||||
#define CWS_MAX_THREADS 16
|
||||
|
||||
typedef struct cws_task_t {
|
||||
int client_fd;
|
||||
int epfd;
|
||||
mcl_hashmap *clients;
|
||||
cws_config *config;
|
||||
} cws_task;
|
||||
|
||||
typedef struct cws_thread_task_t {
|
||||
void (*function)(void *);
|
||||
void *arg;
|
||||
} cws_thread_task;
|
||||
|
||||
typedef struct cws_threadpool_t {
|
||||
mcl_queue *queue;
|
||||
pthread_mutex_t lock;
|
||||
pthread_cond_t notify;
|
||||
size_t threads_num;
|
||||
pthread_t *threads;
|
||||
int shutdown;
|
||||
} cws_threadpool;
|
||||
|
||||
cws_threadpool *cws_threadpool_init(size_t threads_num, size_t queue_size);
|
||||
cws_server_ret cws_threadpool_submit(cws_threadpool *pool, cws_thread_task *task);
|
||||
void *cws_threadpool_worker(void *arg);
|
||||
void cws_threadpool_destroy(cws_threadpool *pool);
|
||||
|
||||
#endif
|
||||
27
include/server/worker.h
Normal file
27
include/server/worker.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef CWS_WORKER_H
|
||||
#define CWS_WORKER_H
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#include "myclib/hashmap/myhashmap.h"
|
||||
#include "server/server.h"
|
||||
|
||||
typedef struct cws_worker_t {
|
||||
int epfd;
|
||||
int pipefd[2];
|
||||
size_t clients_num;
|
||||
pthread_t thread;
|
||||
mcl_hashmap *clients;
|
||||
cws_config *config;
|
||||
} cws_worker;
|
||||
|
||||
cws_worker **cws_worker_init(size_t workers_num, mcl_hashmap *clients, cws_config *config);
|
||||
void cws_worker_free(cws_worker **workers, size_t workers_num);
|
||||
void *cws_worker_loop(void *arg);
|
||||
|
||||
void cws_server_close_client(int epfd, int client_fd, mcl_hashmap *clients);
|
||||
cws_server_ret cws_epoll_add(int epfd, int sockfd, uint32_t events);
|
||||
cws_server_ret cws_epoll_del(int epfd, int sockfd);
|
||||
cws_server_ret cws_server_handle_client_data(int epfd, int client_fd, mcl_hashmap *clients, cws_config *config);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user