From 49839c88a98d3798f7b18c58f54f26f36cacff38 Mon Sep 17 00:00:00 2001 From: Guangxiong Lin Date: Fri, 9 Dec 2022 16:46:49 +0800 Subject: Implement a simple thread pool and refactor Refactor --- connection.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'connection.c') diff --git a/connection.c b/connection.c index d12ab86..c164ec8 100644 --- a/connection.c +++ b/connection.c @@ -8,7 +8,11 @@ #define READ_BUFFER_SIZE 1024 -struct connection *connectionNew(struct tsocket *sock) +struct connection { + struct tsocket *sock; +}; + +struct connection *connection_create(struct tsocket *sock) { struct connection *conn = malloc(sizeof(*conn)); conn->sock = sock; @@ -16,13 +20,13 @@ struct connection *connectionNew(struct tsocket *sock) return conn; } -void connectionDel(struct connection *conn) +void connection_destroy(struct connection *conn) { - tsocketDelete(conn->sock); + tsocket_destroy(conn->sock); free(conn); } -int echo(struct connection *conn) +static void echo(struct connection *conn) { char buf[READ_BUFFER_SIZE]; ssize_t n_read_bytes; @@ -36,18 +40,19 @@ int echo(struct connection *conn) write(sock->fd, buf, sizeof(buf)); } else if (n_read_bytes == 0) { printf("conn %d disconnected\n", sock->fd); - return ERROR; + return; } else if (n_read_bytes == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK) break; } } - - return OK; } -struct event *connectionNewEvent(struct connection *conn) +event_t *connection_create_event(struct tsocket *sock) { - return eventNew(conn, conn->sock->fd, echo, connectionDel); + connection_t *conn = connection_create(sock); + return event_create(conn, conn->sock->fd, + (evloop_process_func_t) echo, + (evloop_destroy_func_t) connection_destroy); } -- cgit v1.2.3