summaryrefslogtreecommitdiff
path: root/bittorrent/epoll_server/epoll_server.h
diff options
context:
space:
mode:
Diffstat (limited to 'bittorrent/epoll_server/epoll_server.h')
-rw-r--r--bittorrent/epoll_server/epoll_server.h58
1 files changed, 58 insertions, 0 deletions
diff --git a/bittorrent/epoll_server/epoll_server.h b/bittorrent/epoll_server/epoll_server.h
new file mode 100644
index 0000000..ba5de4f
--- /dev/null
+++ b/bittorrent/epoll_server/epoll_server.h
@@ -0,0 +1,58 @@
+#ifndef EPOLL_SERVER_H
+#define EPOLL_SERVER_H
+
+#include <netdb.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+
+#include "connection.h"
+
+/**
+ * \brief The length of the event array, must be greater than zero
+ */
+#define MAX_EVENTS 64
+
+#define DEFAULT_BUFFER_SIZE 2048
+
+/**
+ * \brief Iterate over the struct addrinfo elements to create and bind a socket
+ *
+ * \param addrinfo: struct addrinfo elements
+ *
+ * \return The created socket or exit with 1 if there is an error
+ *
+ * Try to create and connect a socket with every addrinfo element until it
+ * succeeds
+ *
+ */
+int create_and_bind(struct addrinfo *addrinfo);
+
+/**
+ * \brief Initialize the Addrinfo struct and call create_and bind() and
+ * listen(2)
+ *
+ * \param ip: IP address of the server
+ * \param port: Port of the server
+ *
+ * \return The created socket
+ *
+ * Initialize the struct addrinfo needed by create_and_bind() before calling
+ * it. When create_and_bind() returns a valid socket, set the socket to
+ * listening and return it.
+ */
+int prepare_socket(const char *ip, const char *port);
+
+/**
+ * \brief Accept a new client and add it to the connection_t struct
+ *
+ * \param epoll_instance: the epoll instance
+ * \param server_socket: listening socket
+ * \param connection: the connection linked list with all the current
+ * connections
+ *
+ * \return The connection struct with the new client added
+ */
+struct connection_t *accept_client(int epoll_instance, int server_socket,
+ struct connection_t *connection);
+
+#endif /* !EPOLL_SERVER_H */