From e339f2d269fcaffed7beed67c3995fe3b67393eb Mon Sep 17 00:00:00 2001 From: Guangxiong Lin Date: Sat, 17 Dec 2022 21:24:04 +0800 Subject: Support on_connect function in server lib --- src/connection.c | 35 ++++++----------------------------- 1 file changed, 6 insertions(+), 29 deletions(-) (limited to 'src/connection.c') diff --git a/src/connection.c b/src/connection.c index c164ec8..ce71b3a 100644 --- a/src/connection.c +++ b/src/connection.c @@ -1,13 +1,8 @@ #include -#include -#include -#include #include "connection.h" #include "constant.h" -#define READ_BUFFER_SIZE 1024 - struct connection { struct tsocket *sock; }; @@ -26,33 +21,15 @@ void connection_destroy(struct connection *conn) free(conn); } -static void echo(struct connection *conn) -{ - char buf[READ_BUFFER_SIZE]; - ssize_t n_read_bytes; - - struct tsocket *sock = (struct tsocket *)conn->sock; - - for (;;) { - n_read_bytes = read(sock->fd, buf, sizeof(buf)); - if (n_read_bytes > 0) { - printf("message from conn %d: %s\n", sock->fd, buf); - write(sock->fd, buf, sizeof(buf)); - } else if (n_read_bytes == 0) { - printf("conn %d disconnected\n", sock->fd); - return; - } else if (n_read_bytes == -1) { - if (errno == EAGAIN || errno == EWOULDBLOCK) - break; - } - } -} - -event_t *connection_create_event(struct tsocket *sock) +event_t *connection_event_create(struct tsocket *sock, connection_callback_func_t func) { connection_t *conn = connection_create(sock); return event_create(conn, conn->sock->fd, - (evloop_process_func_t) echo, + (evloop_process_func_t) func, (evloop_destroy_func_t) connection_destroy); } +int connection_fd(connection_t *conn) +{ + return tsocket_fd(conn->sock); +} -- cgit v1.2.3