aboutsummaryrefslogtreecommitdiff
path: root/acceptor.c
diff options
context:
space:
mode:
authorGuangxiong Lin <[email protected]>2022-12-16 13:53:35 +0800
committerGuangxiong Lin <[email protected]>2022-12-16 13:53:35 +0800
commit7369505397cdfddf0883e2c24e1652df8bd488fe (patch)
treee961bd1bba0276e2c5f523bf12663b34983c9e51 /acceptor.c
parent49839c88a98d3798f7b18c58f54f26f36cacff38 (diff)
downloadtinyserver-7369505397cdfddf0883e2c24e1652df8bd488fe.tar.gz
tinyserver-7369505397cdfddf0883e2c24e1652df8bd488fe.tar.bz2
tinyserver-7369505397cdfddf0883e2c24e1652df8bd488fe.zip
Refactor file structure
Diffstat (limited to 'acceptor.c')
-rw-r--r--acceptor.c58
1 files changed, 0 insertions, 58 deletions
diff --git a/acceptor.c b/acceptor.c
deleted file mode 100644
index 1f28fcc..0000000
--- a/acceptor.c
+++ /dev/null
@@ -1,58 +0,0 @@
-#include <stdbool.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <sys/epoll.h>
-
-#include "acceptor.h"
-#include "util.h"
-#include "connection.h"
-#include "evloop.h"
-
-struct conn_acceptor {
- struct tsocket *sock;
- evloop_t *el;
-};
-typedef struct conn_acceptor conn_acceptor_t;
-
-static void conn_acceptor_accept(conn_acceptor_t *ca)
-{
- struct tsocket *conn_sock = tsocket_accept(ca->sock);
- if (conn_sock == NULL) {
- perror("socket accept");
- return;
- }
-
- if (setblocking(conn_sock->fd, false) == -1) {
- perror("setblocking");
- tsocket_destroy(conn_sock);
- return;
- }
-
- event_t *conn_ev = connection_create_event(conn_sock);
-
- if (evloop_add(ca->el, conn_ev, EPOLLIN | EPOLLET) == -1) {
- perror("eventloop add fd: conn_sock");
- return;
- }
-
- printf("New client fd %d, ip: %s, port: %d\n",
- conn_sock->fd, conn_sock->addr, conn_sock->port);
-}
-
-static void conn_acceptor_destroy(conn_acceptor_t *ca)
-{
- tsocket_destroy(ca->sock);
- free(ca);
-}
-
-event_t *conn_acceptor_create_event(struct tsocket *sock, evloop_t *el)
-{
- conn_acceptor_t *ca = malloc(sizeof(*ca));
- ca->sock = sock;
- ca->el = el;
-
- struct event *ev = event_create(ca, sock->fd,
- (evloop_process_func_t) conn_acceptor_accept,
- (evloop_destroy_func_t) conn_acceptor_destroy);
- return ev;
-}