summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfadhil riyanto <me@fadev.org>2024-09-27 20:06:24 +0700
committerfadhil riyanto <me@fadev.org>2024-09-27 20:06:24 +0700
commit544420827e20952df190f84856a2ff82b3b2b16c (patch)
treef2cc3afd6e4d5ed34d32df7d94539043de1b2038
parent055c6f3eed6f430c493c3d4c0a34d9cbd571ea2f (diff)
add epoll
Signed-off-by: fadhil riyanto <me@fadev.org>
-rw-r--r--config.h6
-rw-r--r--main.c36
2 files changed, 41 insertions, 1 deletions
diff --git a/config.h b/config.h
new file mode 100644
index 0000000..c5f8568
--- /dev/null
+++ b/config.h
@@ -0,0 +1,6 @@
+#ifndef _CONFIG_H_
+#define _CONFIG_H_
+
+#define MAX_ACCEPT_WORKER 5
+
+#endif \ No newline at end of file
diff --git a/main.c b/main.c
index 53d916a..2a6738f 100644
--- a/main.c
+++ b/main.c
@@ -9,10 +9,15 @@
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
+#include <sys/poll.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
+#include <sys/epoll.h>
+#include <sys/wait.h>
+
+#include "config.h"
#define dbgchr(x) log_info("%c", x)
@@ -36,8 +41,15 @@ struct runtime_opts {
};
+struct epoll_fd_queue {
+ struct epoll_event *eventmode;
+ int i;
+};
+
struct server_ctx {
int tcpfd;
+ int epoll_fd;
+ struct epoll_fd_queue *epoll_fd_queue;
volatile int *need_exit_ptr;
};
@@ -134,12 +146,34 @@ static int server_reg_sigaction(void)
}
+
+static void setup_epoll(struct server_ctx *srv_ctx)
+{
+ srv_ctx->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
+}
+
static int enter_eventloop(struct server_ctx *srv_ctx)
{
- while(!*srv_ctx->need_exit_ptr) {
+ setup_epoll(srv_ctx);
+ struct epoll_fd_queue *epoll_fd_queue = (struct epoll_fd_queue *)malloc(
+ sizeof(struct epoll_event) * MAX_ACCEPT_WORKER + sizeof(int));
+
+ srv_ctx->epoll_fd_queue = epoll_fd_queue;
+
+ for(int i = 0; i < MAX_ACCEPT_WORKER; i++) {
+ if (fork() == 0) {
+
+ _exit(0);
+ }
}
+ while(wait(NULL) > 0);
+
+ close(srv_ctx->epoll_fd);
+ free(epoll_fd_queue);
+
+
return 0;
}