summaryrefslogtreecommitdiffhomepage
path: root/dev/lib/net
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-11-19 03:16:11 +0100
committerAmlal El Mahrouss <amlal@nekernel.org>2025-11-19 03:16:11 +0100
commit399f3697b5468af6f827af4f8a856aa842d7c2b6 (patch)
treeba96824ae096ef9b1607718f1373d529c9a1b7fe /dev/lib/net
parent54940e80e0a259f748d483291e0e7aef7dd98353 (diff)
feat: making usage of the OCL way easier.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'dev/lib/net')
-rw-r--r--dev/lib/net/modem.hpp29
-rw-r--r--dev/lib/net/url.hpp22
2 files changed, 36 insertions, 15 deletions
diff --git a/dev/lib/net/modem.hpp b/dev/lib/net/modem.hpp
index 7c07191..5bcf3fd 100644
--- a/dev/lib/net/modem.hpp
+++ b/dev/lib/net/modem.hpp
@@ -16,16 +16,18 @@
#include <string>
#include <cstddef>
-#define OCL_MODEM_INTERFACE : public ocl::net::basic_modem
+#define OCL_MODEM_INTERFACE : public ocl::net::modem
namespace ocl::net
{
- class basic_modem;
-
- typedef int64_t socket_type;
+ class modem;
+ using socket_type = int64_t;
+
+ /// =============================================================================
/// @brief Modem container concept, a container to read and write on a network stream.
- class basic_modem
+ /// =============================================================================
+ class modem final
{
private:
socket_type fd_{};
@@ -35,20 +37,25 @@ namespace ocl::net
public:
const bool& bad{bad_};
- explicit basic_modem() = default;
+ explicit modem() = default;
- virtual ~basic_modem()
+ ~modem()
{
this->destroy();
}
- basic_modem& operator=(const basic_modem&) = delete;
- basic_modem(const basic_modem&) = delete;
+ modem& operator=(const modem&) = delete;
+ modem(const modem&) = delete;
static constexpr auto local_address_ip4 = "127.0.0.1";
static constexpr auto local_address_ip6 = "::1";
static constexpr const auto backlog_count = 5U;
+ /// =============================================================================
+ /// @brief Check if the modem is valid.
+ /// @return true if valid, false otherwise.
+ /// =============================================================================
+
bool is_valid() const noexcept
{
return this->fd_ != -1 && !this->bad_;
@@ -107,7 +114,7 @@ namespace ocl::net
}
template <int32_t af, int32_t kind, int32_t port>
- bool construct(const char* addr = basic_modem::local_address_ip4, const bool& is_server = false) noexcept
+ bool construct(const char* addr = modem::local_address_ip4, const bool& is_server = false) noexcept
{
static_assert(af != 0, "Address family is zero");
static_assert(kind != 0, "Kind is zero");
@@ -139,7 +146,7 @@ namespace ocl::net
bad_ = ret == -1;
- ::listen(fd_, basic_modem::backlog_count);
+ ::listen(fd_, modem::backlog_count);
return bad_ == false;
}
diff --git a/dev/lib/net/url.hpp b/dev/lib/net/url.hpp
index b77e790..e4eca2a 100644
--- a/dev/lib/net/url.hpp
+++ b/dev/lib/net/url.hpp
@@ -33,6 +33,7 @@ namespace ocl::net
{
url_protocol m_protocol_{url_protocol::invalid};
std::basic_stringstream<char_type> m_ss_{};
+ std::basic_string<char_type> m_port_{""};
public:
explicit basic_url(const std::basic_string<char_type>& protocol)
@@ -65,6 +66,12 @@ namespace ocl::net
if (in.empty())
return *this;
+ if (in.starts_with(":"))
+ {
+ m_port_ = in.substr(1);
+ return *this;
+ }
+
m_ss_ += in;
return *this;
}
@@ -81,14 +88,21 @@ namespace ocl::net
}
public:
- bool protocol_exists()
+ auto protocol() const noexcept
{
- return this->m_protocol_ != url_protocol::bad || this->m_protocol_ != url_protocol::invalid;
+ return this->m_protocol_;
}
- bool is_valid()
+ auto port() const noexcept
{
- return m_ss_.size() > 0 && this->protocol_exists();
+ return this->m_port_;
+ }
+
+ auto is_valid() const noexcept
+ {
+ return m_ss_.size() > 0 && this->m_protocol_ != url_protocol::bad || this->m_protocol_ != url_protocol::invalid;
}
};
+
+ using url = basic_url<char>;
} // namespace ocl::net