summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-11-30 03:04:35 -0500
committerAmlal El Mahrouss <amlal@nekernel.org>2025-11-30 03:04:35 -0500
commit9dce70e33cf855bea6f610bac52f3a5239ce877c (patch)
treefb6a6661288f50937440a1f5c6f5fefae4b21999
parent374d395d5ce986b668b07c59e2a78666631fb46c (diff)
chore: update tests for unique_socket.
feat: updated public API for unique_socket. Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
-rw-r--r--include/ocl/net/unique_socket.hpp68
-rw-r--r--tests/network_basic/network_basic_test.cc20
2 files changed, 34 insertions, 54 deletions
diff --git a/include/ocl/net/unique_socket.hpp b/include/ocl/net/unique_socket.hpp
index 906853f..2f8f744 100644
--- a/include/ocl/net/unique_socket.hpp
+++ b/include/ocl/net/unique_socket.hpp
@@ -56,68 +56,63 @@ namespace ocl::net
static constexpr auto local_address_ip6 = "::1";
static constexpr const auto backlog_count = 5U;
- /// =============================================================================
- /// @brief Check if the unique_socket is valid.
- /// @return true if valid, false otherwise.
- /// =============================================================================
-
- bool is_valid() const noexcept
- {
- return this->fd_ != -1 && !this->bad_;
- }
-
- template <typename ptr_type>
- bool receive(ptr_type& out, std::size_t len) noexcept
+ unique_socket read(const char* 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;
+ if (!out || !len)
+ return {};
socket_type cl_{fd_};
if (this->is_server_)
cl_ = ::accept(fd_, nullptr, nullptr);
- auto ret = ::recv(cl_, out, len, 0);
+ auto ret = ::recv(cl_, static_cast<void*>(const_cast<char*>(out)), len, 0);
+
+ unique_socket sock;
+
+ sock.fd_ = cl_;
+ sock.bad_ = ret > 0L;
- return ret > 0L;
+ return std::move(sock);
}
- template <typename ptr_type>
- bool transmit(ptr_type& out, std::size_t len) noexcept
+ unique_socket& write(const char* out, std::size_t len) noexcept
{
- static_assert(std::is_pointer<ptr_type>::value, "ptr_type is not a pointer!");
-
if (!out)
- return false;
+ return *this;
if (!len)
- return false;
+ return *this;
auto ret = ::send(fd_, out, len, 0);
bad_ = !(ret >= 0L);
- return ret >= 0L;
+ return *this;
}
- template <typename char_type>
- bool transmit(const std::basic_string<char_type>& out) noexcept
+ unique_socket& write(const std::string& out) noexcept
{
if (out.empty())
- return false;
+ return *this;
auto ret = ::send(fd_, out.data(), out.size(), 0);
bad_ = !(ret >= 0L);
- return ret >= 0L;
+ return *this;
}
+ template <uint16_t port>
+ static unique_socket make_socket(const std::string& address, const bool is_server)
+ {
+ unique_socket sock;
+ sock.construct<AF_INET, SOCK_STREAM, port>(address.c_str(), is_server);
+
+ return sock;
+ }
+
+ private:
template <uint16_t af, uint16_t kind, uint16_t port>
bool construct(const char* addr = unique_socket::local_address_ip4, const bool& is_server = false) noexcept
{
@@ -165,13 +160,4 @@ namespace ocl::net
return true;
}
};
-
- template<typename char_type, uint16_t port>
- inline unique_socket make_socket(const std::basic_string<char_type>& address, const bool is_server)
- {
- unique_socket sock;
- sock.construct<AF_INET, SOCK_STREAM, port>(address.c_str(), is_server);
-
- return sock;
- }
} // namespace ocl::net
diff --git a/tests/network_basic/network_basic_test.cc b/tests/network_basic/network_basic_test.cc
index d5e55cf..955466f 100644
--- a/tests/network_basic/network_basic_test.cc
+++ b/tests/network_basic/network_basic_test.cc
@@ -12,22 +12,17 @@
/// @brief Basic Send Test
TEST(NetworkTest, BasicNetworkTransmit)
{
- ocl::net::unique_socket sock;
- sock.construct<AF_INET, SOCK_STREAM, 8000>(ocl::net::unique_socket::local_address_ip4, true);
-
- EXPECT_TRUE(sock.is_valid());
-
+ ocl::net::unique_socket sock = ocl::net::unique_socket::make_socket<8000>(ocl::net::unique_socket::local_address_ip4, false);
std::basic_string<char> buf_dst = "HELLO, WORLD!";
- EXPECT_TRUE(sock.transmit(buf_dst));
+ EXPECT_TRUE(sock.read(buf_dst.c_str(), buf_dst.size()).bad == false);
}
/// @brief Basic Receive test
TEST(NetworkTest, BasicNetworkReceive)
{
- ocl::net::unique_socket sock;
- sock.construct<AF_INET, SOCK_STREAM, 8000>(ocl::net::unique_socket::local_address_ip4, true);
+ ocl::net::unique_socket sock = ocl::net::unique_socket::make_socket<8000>(ocl::net::unique_socket::local_address_ip4, true);
- EXPECT_TRUE(sock.is_valid());
+ EXPECT_TRUE(!sock.bad);
std::basic_string<char> buf_dst;
buf_dst.reserve(512);
@@ -35,12 +30,11 @@ TEST(NetworkTest, BasicNetworkReceive)
auto buf = buf_dst.data();
auto sz = buf_dst.size();
- EXPECT_FALSE(sock.receive(buf, sz));
+ EXPECT_FALSE(!sock.read(buf, sz).bad);
}
TEST(NetworkTest, BasicNetworkConstruct)
{
- auto socket = ocl::net::make_socket<char, 8000>(ocl::net::unique_socket::local_address_ip4, true);
-
- EXPECT_TRUE(socket.is_valid());
+ auto socket = ocl::net::unique_socket::make_socket<8000>(ocl::net::unique_socket::local_address_ip4, true);
+ EXPECT_TRUE(!socket.bad);
} \ No newline at end of file