diff --git a/src/net/netio_exception.cpp b/src/net/netio_exception.cpp new file mode 100644 index 0000000000000000000000000000000000000000..45a720eb8daa3053af66fa0b7455a9658bc4507e --- /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 0000000000000000000000000000000000000000..a73817f4b4ae6d478c2a5923e1252571166e9ee6 --- /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 0000000000000000000000000000000000000000..9a2d21adb4efd61250f7362eb953d25cf53aee6d --- /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 0000000000000000000000000000000000000000..4beca9af9e2a010f03406de36623eb5d5a905aa8 --- /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); +}; + +}