summaryrefslogtreecommitdiff
path: root/bittorrent/epoll_server/connection.c
diff options
context:
space:
mode:
Diffstat (limited to 'bittorrent/epoll_server/connection.c')
-rw-r--r--bittorrent/epoll_server/connection.c58
1 files changed, 58 insertions, 0 deletions
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 <err.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#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;
+}