From e9f747051957882c5570fdbb330ae407c6dab4ca Mon Sep 17 00:00:00 2001
From: Doralitze <doralitze@chaotikum.org>
Date: Sat, 2 Jan 2021 22:45:17 +0100
Subject: [PATCH] add: TCP socket constructor

---
 src/net/netio_exception.cpp   | 20 ++++++++++++++++
 src/net/netio_exception.hpp   | 24 +++++++++++++++++++
 src/net/tcp_server_socket.cpp | 43 +++++++++++++++++++++++++++++++++++
 src/net/tcp_server_socket.hpp | 26 +++++++++++++++++++++
 4 files changed, 113 insertions(+)
 create mode 100644 src/net/netio_exception.cpp
 create mode 100644 src/net/netio_exception.hpp
 create mode 100644 src/net/tcp_server_socket.cpp
 create mode 100644 src/net/tcp_server_socket.hpp

diff --git a/src/net/netio_exception.cpp b/src/net/netio_exception.cpp
new file mode 100644
index 0000000..45a720e
--- /dev/null
+++ b/src/net/netio_exception.cpp
@@ -0,0 +1,20 @@
+/*
+ * netio_exception.cpp
+ *
+ *  Created on: 02.01.2021
+ *      Author: doralitze
+ */
+
+#include "netio_exception.hpp"
+
+namespace rmrf::net {
+	netio_exception::netio_exception(const std::string cause_) : cause(cause_) {
+
+	}
+
+	const char* netio_exception::what() const throw(){
+		return this->cause.c_str();
+	}
+}
+
+
diff --git a/src/net/netio_exception.hpp b/src/net/netio_exception.hpp
new file mode 100644
index 0000000..a73817f
--- /dev/null
+++ b/src/net/netio_exception.hpp
@@ -0,0 +1,24 @@
+/*
+ * netio_exception.hpp
+ *
+ *  Created on: 02.01.2021
+ *      Author: doralitze
+ */
+
+#pragma once
+
+
+#include <exception>
+#include <string>
+
+namespace rmrf::net {
+
+class netio_exception: public std::exception {
+private:
+	std::string cause;
+public:
+	netio_exception(const std::string cause_);
+	virtual const char* what() const throw();
+};
+
+}
diff --git a/src/net/tcp_server_socket.cpp b/src/net/tcp_server_socket.cpp
new file mode 100644
index 0000000..9a2d21a
--- /dev/null
+++ b/src/net/tcp_server_socket.cpp
@@ -0,0 +1,43 @@
+/*
+ * tcp_server_socket.cpp
+ *
+ *  Created on: 02.01.2021
+ *      Author: doralitze
+ */
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <netinet/in.h>
+
+#include "tcp_server_socket.hpp"
+
+namespace rmrf::net {
+
+tcp_server_socket::tcp_server_socket(uint16_t port) : ss{nullptr} {
+	auto raw_socket_fd = socket(AF_INET, SOCK_STREAM, 0);
+	if(raw_socket_fd < 0) {
+		// TODO implement propper error handling
+		throw netio_exception("Failed to create socket fd.");
+	}
+
+	struct sockaddr_in addr;
+	addr.sin_family = AF_INET;
+	addr.sin_port = htons(port);
+	addr.sin_addr.s_addr = INADDR_ANY; // TODO FIXME
+	if (bind(raw_socket_fd, (struct sockaddr *) &addr, sizeof(addr)) != 0) {
+		throw netio_exception("Failed to bind to all addresses (FIXME)");
+	}
+
+	fcntl(raw_socket_fd, F_SETFL, fcntl(raw_socket_fd, F_GETFL, 0) | O_NONBLOCK);
+	if (listen(raw_socket_fd, 5) == -1) {
+		throw netio_exception("Failed to enable listenting mode for raw socket");
+	}
+
+	this->ss = std::make_shared<async_server_socket>(auto_fd(raw_socket_fd));
+}
+
+}
diff --git a/src/net/tcp_server_socket.hpp b/src/net/tcp_server_socket.hpp
new file mode 100644
index 0000000..4beca9a
--- /dev/null
+++ b/src/net/tcp_server_socket.hpp
@@ -0,0 +1,26 @@
+/*
+ * tcp_server_socket.hpp
+ *
+ *  Created on: 02.01.2021
+ *      Author: doralitze
+ */
+
+#pragma once
+
+#include <cstdio>
+#include <memory>
+
+#include "async_server.hpp"
+#include "netio_exception.hpp"
+
+namespace rmrf::net {
+
+
+class tcp_server_socket {
+private:
+	std::shared_ptr<async_server_socket> ss;
+public:
+	tcp_server_socket(uint16_t port);
+};
+
+}
-- 
GitLab