summaryrefslogtreecommitdiffhomepage
path: root/dev/lib
diff options
context:
space:
mode:
Diffstat (limited to 'dev/lib')
-rw-r--r--dev/lib/core/includes.hpp5
-rw-r--r--dev/lib/net/modem.hpp (renamed from dev/lib/net/network.hpp)40
2 files changed, 27 insertions, 18 deletions
diff --git a/dev/lib/core/includes.hpp b/dev/lib/core/includes.hpp
index e482a7c..6bddba9 100644
--- a/dev/lib/core/includes.hpp
+++ b/dev/lib/core/includes.hpp
@@ -11,6 +11,7 @@
#include <boost/core/nvp.hpp>
#include <boost/core/demangle.hpp>
#include <boost/core/null_deleter.hpp>
-#include <memory>
-#include <utility>
+
#include <filesystem>
+#include <utility>
+#include <memory>
diff --git a/dev/lib/net/network.hpp b/dev/lib/net/modem.hpp
index e078c93..ba3be0c 100644
--- a/dev/lib/net/network.hpp
+++ b/dev/lib/net/modem.hpp
@@ -1,5 +1,5 @@
/*
- * File: net/network.hpp
+ * File: net/modem.hpp
* Purpose: Modem concept in modern C++
* Author: Amlal El Mahrouss (founder@snu.systems)
* Copyright 2025, Amlal El Mahrouss and SNU Systems Corp.
@@ -24,10 +24,13 @@ namespace snu::net
typedef int64_t socket_type;
- /// @brief Delivery modem concept, a container to read and write on a network stream.
+ /// @brief Modem container concept, a container to read and write on a network stream.
template <typename char_type>
class basic_modem
{
+ socket_type fd_{};
+ bool server_fd_{};
+
public:
explicit basic_modem() = default;
virtual ~basic_modem() = default;
@@ -35,11 +38,9 @@ namespace snu::net
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 const auto backlog_count = 18U;
-
- socket_type fd_{};
+ static constexpr auto local_address_ip4 = "127.0.0.1";
+ static constexpr auto local_address_ip6 = "::1";
+ static constexpr const auto backlog_count = 5U;
bool is_valid() const noexcept
{
@@ -57,7 +58,12 @@ namespace snu::net
if (!len)
return false;
- auto ret = ::recv(fd_, out, len, 0);
+ socket_type cl_{fd_};
+
+ if (this->server_fd_)
+ cl_ = ::accept(fd_, nullptr, nullptr);
+
+ auto ret = ::recv(cl_, out, len, 0);
return ret > 0L;
}
@@ -89,30 +95,32 @@ namespace snu::net
return ret > 0L;
}
- template <int32_t af, int32_t kind, int32_t ip_proto, int32_t port>
+ 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
{
static_assert(af != 0, "Address family is zero");
- static_assert(kind != 0, "Type is zero");
+ static_assert(kind != 0, "Kind is zero");
- fd_ = ::socket(af, kind, ip_proto);
+ fd_ = ::socket(af, kind, 0);
+ server_fd_ = is_server;
if (fd_ == -1)
return false;
- struct sockaddr_in addr_
- {
- };
+ struct sockaddr_in addr_;
+ std::memset(&addr_, 0, sizeof(struct sockaddr_in));
addr_.sin_addr.s_addr = ::inet_addr(addr);
- addr_.sin_port = port;
+ addr_.sin_port = htons(port);
+ addr_.sin_family = af;
if (!is_server)
{
- const auto ret = ::connect(fd_, reinterpret_cast<struct sockaddr*>(&addr_), sizeof(decltype(addr_)));
+ const auto ret = ::connect(fd_, reinterpret_cast<struct sockaddr*>(&addr_), sizeof(addr_));
return ret == 0L;
}
+ ::bind(fd_, (struct sockaddr*)&addr_, sizeof(addr_));
::listen(fd_, basic_modem::backlog_count);
return true;