summaryrefslogtreecommitdiffhomepage
path: root/dev/lib/net
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-08-16 21:35:13 +0200
committerGitHub <noreply@github.com>2025-08-16 21:35:13 +0200
commit443588a42fe9cf48b5f63184b94afe483cb0e761 (patch)
treeb65844000839103efb40c635337339dc8f193f89 /dev/lib/net
parent8b7b48fe4acf0482580930eaebaa2f316727f864 (diff)
parent95db0ac7dcb8625c5a1e92408c0c02962b205871 (diff)
Merge pull request #3 from snutech-gh/dev
SOCL – v1.0.4
Diffstat (limited to 'dev/lib/net')
-rw-r--r--dev/lib/net/network.hpp137
-rw-r--r--dev/lib/net/url.hpp64
2 files changed, 201 insertions, 0 deletions
diff --git a/dev/lib/net/network.hpp b/dev/lib/net/network.hpp
new file mode 100644
index 0000000..67f3740
--- /dev/null
+++ b/dev/lib/net/network.hpp
@@ -0,0 +1,137 @@
+/*
+ * File: net/network.hpp
+ * Purpose: Modem concept in modern C++
+ * Author: Amlal El Mahrouss (founder@snu.systems)
+ * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp.
+ */
+
+#ifndef _SNU_NET_NETWORK_HPP
+#define _SNU_NET_NETWORK_HPP
+
+#include <unistd.h>
+#include <arpa/inet.h>
+#include <sys/socket.h>
+#include <string>
+#include <utility>
+#include <cstddef>
+
+#define SNU_MODEM_INTERFACE : protected snu::net::basic_modem
+
+namespace snu::net
+{
+ template <typename char_type>
+ class basic_modem;
+
+ typedef int64_t socket_type;
+
+ /// @brief Delivery modem concept, a container to read and write on a network stream.
+ template <typename char_type>
+ class basic_modem
+ {
+ public:
+ explicit basic_modem() = default;
+ virtual ~basic_modem() = default;
+
+ basic_modem& operator=(const basic_modem&) = default;
+ basic_modem(const basic_modem&) = default;
+
+ static constexpr auto local_address_ip6 = "127.0.0.1";
+ static constexpr auto local_address_ip4 = "::1";
+
+ static constexpr auto backlog_count = 18U;
+
+ socket_type fd_{};
+
+ bool is_valid() const noexcept
+ {
+ return this->fd_ != -1;
+ }
+
+ template <typename ptr_type>
+ bool receive(ptr_type& out, std::size_t len) noexcept
+ {
+ static_assert(std::is_pointer<ptr_type>::value, "ptr_type is not a pointer!");
+
+ if (!out)
+ return false;
+
+ if (!len)
+ return false;
+
+ auto ret = ::recv(fd_, out, len, 0);
+
+ return ret > 0;
+ }
+
+ template <typename ptr_type>
+ bool transmit(ptr_type& out, std::size_t len) noexcept
+ {
+ static_assert(std::is_pointer<ptr_type>::value, "char_type is not a pointer!");
+
+ if (!out)
+ return false;
+
+ if (!len)
+ return false;
+
+ auto ret = ::send(fd_, out, len, 0);
+
+ return ret > 0;
+ }
+
+ template <typename ptr_type>
+ bool transmit(std::basic_string<ptr_type> out) noexcept
+ {
+ if (out.empty())
+ return false;
+
+ auto ret = ::send(fd_, out.data(), out.size(), 0);
+
+ return ret > 0;
+ }
+
+ template <int32_t af, int32_t kind, int32_t ip_proto, int32_t port>
+ bool construct(const char* addr = basic_modem::local_address_ip4, const bool& is_server = false) noexcept
+ {
+ static_assert(af != 0, "Address family is zero");
+ static_assert(kind != 0, "Type is zero");
+
+ fd_ = ::socket(af, kind, ip_proto);
+
+ if (fd_ == -1)
+ return false;
+
+ struct sockaddr_in addr_
+ {
+ };
+
+ addr_.sin_addr.s_addr = ::inet_addr(addr);
+ addr_.sin_port = port;
+
+ if (!is_server)
+ {
+ const auto ret = ::connect(fd_, reinterpret_cast<struct sockaddr*>(&addr_), sizeof(decltype(addr_)));
+ return ret == 0;
+ }
+
+ ::listen(fd_, basic_modem::backlog_count);
+
+ return true;
+ }
+
+ bool destroy() noexcept
+ {
+ if (!fd_)
+ return false;
+
+ ::shutdown(fd_, SHUT_RDWR);
+ ::close(fd_);
+
+ fd_ = 0U;
+
+ return true;
+ }
+ };
+} // namespace snu::net
+
+#endif // ifndef _SNU_NET_NETWORK_HPP
diff --git a/dev/lib/net/url.hpp b/dev/lib/net/url.hpp
new file mode 100644
index 0000000..f6dba95
--- /dev/null
+++ b/dev/lib/net/url.hpp
@@ -0,0 +1,64 @@
+/*
+ * File: net/url.hpp
+ * Purpose: URL container in modern C++
+ * Author: Amlal El Mahrouss (founder@snu.systems)
+ * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp.
+ */
+
+#pragma once
+
+#include <string>
+#include <iostream>
+#include <sstream>
+
+/// @author Amlal El Mahrouss (founder@snu.systems)
+
+namespace snu::net
+{
+ template <typename char_type>
+ class basic_url;
+
+ template <typename char_type>
+ class basic_url_traits;
+
+ /// @brief Basic URL parser container.
+ template <typename char_type>
+ class basic_url final
+ {
+ friend basic_url_traits<char_type>;
+
+ std::basic_stringstream<char_type> ss_{};
+
+ public:
+ explicit basic_url() = default;
+ ~basic_url() = default;
+
+ basic_url& operator=(const basic_url&) = default;
+ basic_url(const basic_url&) = default;
+
+ basic_url& operator/=(const std::basic_string<char_type>& in)
+ {
+ if (in.empty())
+ return *this;
+
+ ss_ += in;
+ return *this;
+ }
+
+ basic_url& operator/=(const char_type& in)
+ {
+ ss_ += in;
+ return *this;
+ }
+
+ explicit operator bool()
+ {
+ return this->is_valid();
+ }
+
+ bool is_valid()
+ {
+ return ss_.size() > 0;
+ }
+ };
+} // namespace snu::net