From 967be9e750221ab2ab783f95df79bb26d290a45e Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:07:58 +0200 Subject: add: added projects --- bittorrent/epoll_server/connection.c | 58 ++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 bittorrent/epoll_server/connection.c (limited to 'bittorrent/epoll_server/connection.c') diff --git a/bittorrent/epoll_server/connection.c b/bittorrent/epoll_server/connection.c new file mode 100644 index 0000000..62560c5 --- /dev/null +++ b/bittorrent/epoll_server/connection.c @@ -0,0 +1,58 @@ +#include "connection.h" + +#include +#include +#include + +#include "utils/xalloc.h" + +struct connection_t *add_client(struct connection_t *head, int client_socket) +{ + struct connection_t *new_connection = xmalloc(sizeof(struct connection_t)); + + new_connection->client_socket = client_socket; + new_connection->buffer = NULL; + new_connection->nb_read = 0; + new_connection->next = head; + + return new_connection; +} + +struct connection_t *remove_client(struct connection_t *head, int client_socket) +{ + if (head && head->client_socket == client_socket) + { + struct connection_t *client_connection = head->next; + if (close(head->client_socket) == -1) + err(EXIT_FAILURE, "Failed to close socket"); + free(head->buffer); + free(head); + return client_connection; + } + + struct connection_t *tmp = head; + while (tmp->next) + { + if (tmp->next->client_socket == client_socket) + { + struct connection_t *client_connection = tmp->next; + tmp->next = client_connection->next; + if (close(client_connection->client_socket) == -1) + err(EXIT_FAILURE, "Failed to close socket"); + free(client_connection->buffer); + free(client_connection); + break; + } + tmp = tmp->next; + } + + return head; +} + +struct connection_t *find_client(struct connection_t *head, int client_socket) +{ + while (head != NULL && head->client_socket != client_socket) + head = head->next; + + return head; +} -- cgit v1.2.3