diff options
| -rw-r--r-- | example/simple_unique_socket/CMakeLists.txt | 15 | ||||
| -rw-r--r-- | example/simple_unique_socket/example.cc | 22 | ||||
| -rw-r--r-- | include/ocl/detail/posix/.keep | 0 | ||||
| -rw-r--r-- | include/ocl/detail/posix/net_config.hpp | 22 | ||||
| -rw-r--r-- | include/ocl/posix/unique_socket.hpp | 214 | ||||
| -rw-r--r-- | include/ocl/smart_socket.hpp | 21 | ||||
| -rw-r--r-- | test/.keep | 0 | ||||
| -rw-r--r-- | test/network_basic/CMakeLists.txt | 23 | ||||
| -rw-r--r-- | test/network_basic/network_basic_test.cc | 49 |
9 files changed, 0 insertions, 366 deletions
diff --git a/example/simple_unique_socket/CMakeLists.txt b/example/simple_unique_socket/CMakeLists.txt deleted file mode 100644 index 06d5119..0000000 --- a/example/simple_unique_socket/CMakeLists.txt +++ /dev/null @@ -1,15 +0,0 @@ - -cmake_minimum_required(VERSION 3.15...3.31) - -project( - NetworkExample - VERSION 1.0 - LANGUAGES CXX) - -find_package(Boost REQUIRED COMPONENTS container) - -add_executable(NetworkExample example.cc) - -set_property(TARGET NetworkExample PROPERTY CXX_STANDARD 20) -target_include_directories(NetworkExample PUBLIC ../../include/) -target_link_libraries(NetworkExample PRIVATE Boost::container) diff --git a/example/simple_unique_socket/example.cc b/example/simple_unique_socket/example.cc deleted file mode 100644 index 86178e1..0000000 --- a/example/simple_unique_socket/example.cc +++ /dev/null @@ -1,22 +0,0 @@ -#include <ocl/posix/unique_socket.hpp> -#include <ocl/print.hpp> - -/// @brief Basic Send test -int main() -{ - ocl::unique_socket sock = ocl::unique_socket::make_socket<8005>(ocl::unique_socket::any_address, true); - - char buf_dst[512] = {"HELLO, WORLD\0"}; - - ocl::unique_socket sock2 = ocl::unique_socket::make_socket<8005>(ocl::unique_socket::any_address, false); - - char buf_dst2[512] = {0}; - - auto new_sock = sock.accept(); - new_sock.write_from_buffer(buf_dst, strlen(buf_dst)); - sock2.read_client_buffer(buf_dst2, strlen(buf_dst)); - std::cout << "result:" << buf_dst2 << "\n"; - - - return EXIT_SUCCESS; -} diff --git a/include/ocl/detail/posix/.keep b/include/ocl/detail/posix/.keep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/include/ocl/detail/posix/.keep diff --git a/include/ocl/detail/posix/net_config.hpp b/include/ocl/detail/posix/net_config.hpp deleted file mode 100644 index 671d4ba..0000000 --- a/include/ocl/detail/posix/net_config.hpp +++ /dev/null @@ -1,22 +0,0 @@ -/* - * File: detail/net_config.hpp - * Purpose: Network Config file of the OCL.CORE library. - * Author: Amlal El Mahrouss (amlal@nekernel.org) - * Copyright 2025, Amlal El Mahrouss, licensed under the Boost Software License. - */ - -#ifndef __OCL_NET_CONFIG -#define __OCL_NET_CONFIG - -#include <stdexcept> -#include <arpa/inet.h> -#include <sys/socket.h> -#include <string> -#include <cstddef> -#include <cstring> - -#ifdef OCL_WINDOWS -#error !!! "Windows is not supported yet for <unique_socket>" !!! -#endif // OCL_WINDOWS - -#endif // __OCL_NET_CONFIG
\ No newline at end of file diff --git a/include/ocl/posix/unique_socket.hpp b/include/ocl/posix/unique_socket.hpp deleted file mode 100644 index f094e54..0000000 --- a/include/ocl/posix/unique_socket.hpp +++ /dev/null @@ -1,214 +0,0 @@ -/* - * File: unique_socket.hpp - * Purpose: RAII socket concept in modern C++ - * Author: Amlal El Mahrouss (amlal@nekernel.org) - * Copyright 2025, Amlal El Mahrouss, licensed under the Boost Software License. - */ - -#ifndef __OCL_CORE_SOCKET -#define __OCL_CORE_SOCKET - -#include <ocl/detail/config.hpp> -#include <ocl/detail/posix/net_config.hpp> - -namespace ocl -{ - class unique_socket; - - inline constexpr auto socket_null = 0; - - class unique_socket final - { - public: - using socket_type = int; - using error_type = bool; - using condition_type = bool; - using socket_state = int; - - private: - socket_type socket_{}; - condition_type is_server_{false}; - error_type bad_{false}; - - public: - unique_socket() = default; - - ~unique_socket() - { - this->destroy(); - } - - unique_socket& operator=(const unique_socket&) = delete; - unique_socket(const unique_socket&) = delete; - - unique_socket& operator=(unique_socket&& other) noexcept - { - if (this != &other) - { - this->destroy(); - - socket_ = other.socket_; - is_server_ = other.is_server_; - bad_ = other.bad_; - - other.socket_ = 0; - other.bad_ = true; - } - - return *this; - } - - unique_socket(unique_socket&& other) noexcept - : socket_(other.socket_), is_server_(other.is_server_), bad_(other.bad_) - { - other.socket_ = 0; - other.bad_ = true; - } - - static constexpr auto any_address = "0.0.0.0"; - static constexpr auto backlog_count = 20U; - - const error_type& bad() - { - return bad_; - } - - unique_socket::socket_state state() noexcept - { - socket_state error = 0; - socklen_t len = sizeof(error); - getsockopt(socket_, SOL_SOCKET, SO_ERROR, &error, &len); - - return error; - } - - unique_socket accept() noexcept - { - socket_type cl_{-1}; - - if (this->is_server_) - cl_ = ::accept(socket_, nullptr, nullptr); - - unique_socket ret_sock; - ret_sock.socket_ = cl_; - - return std::move(ret_sock); - } - - unique_socket read_server_buffer(char* in, std::size_t len) - { - if (!in || !len) - return {}; - - if (!is_server_) - return {}; - - auto ret_sock = accept(); - - if (ret_sock.socket_ == -1) - throw std::invalid_argument("no connection to accept."); - - auto ret = ::recv(ret_sock.socket_, in, len, 0); - ret_sock.bad_ = ret < 0L; - - return ret_sock; - } - - void read_client_buffer(char* in, std::size_t len) - { - if (!in || !len) - return; - - if (is_server_) - return; - - auto ret = ::recv(this->socket_, in, len, 0); - this->bad_ = ret < 0L; - } - - void write_from_buffer(const char* out, std::size_t len) - { - if (!out) - return; - - if (!len) - return; - - auto ret = ::send(this->socket_, out, len, 0); - this->bad_ = ret < 0L; - } - - template <uint16_t port> - static unique_socket make_socket(const std::string& address, const bool is_server) - { - if (unique_socket sock; sock.construct<AF_INET, SOCK_STREAM, port>(address.c_str(), is_server)) - return sock; - - throw std::invalid_argument("invalid socket argument"); - } - - private: - template <uint16_t af, uint16_t kind, uint16_t port> - bool construct(const char* addr = unique_socket::any_address, const bool& is_server = false) noexcept - { - static_assert(af != 0, "Address family is zero"); - static_assert(kind != 0, "Kind is zero"); - - socket_ = ::socket(af, kind, 0); - is_server_ = is_server; - - if (socket_ == -1) - return false; - - struct sockaddr_in addr_; - std::memset(&addr_, 0, sizeof(struct sockaddr_in)); - - if (addr == unique_socket::any_address) - addr_.sin_addr.s_addr = INADDR_ANY; - else - addr_.sin_addr.s_addr = ::inet_addr(addr); - - addr_.sin_port = htons(port); - addr_.sin_family = af; - - if (!is_server) - { - const auto ret = ::connect(socket_, reinterpret_cast<struct sockaddr*>(&addr_), sizeof(addr_)); - bad_ = ret == -1; - return bad_ == false; - } - - int ret = ::bind(socket_, (struct sockaddr*)&addr_, sizeof(addr_)); - - bad_ = ret == -1; - - if (bad_) - return false; - - ret = ::listen(socket_, unique_socket::backlog_count); - - bad_ = ret == -1; - - return bad_ == false; - } - - bool destroy() noexcept - { - if (!socket_) - return false; - - ::close(socket_); - - socket_ = 0L; - - return true; - } - }; - - template <typename TS> - concept IsValidSocket = requires(TS& sock) { - { !sock.bad() }; - }; -} // namespace ocl - -#endif
\ No newline at end of file diff --git a/include/ocl/smart_socket.hpp b/include/ocl/smart_socket.hpp deleted file mode 100644 index 331f86d..0000000 --- a/include/ocl/smart_socket.hpp +++ /dev/null @@ -1,21 +0,0 @@ -/* - * File: smart_socket.hpp - * Purpose: Smart Socket helpers. - * Author: Amlal El Mahrouss (amlal@nekernel.org) - * Copyright 2025, Amlal El Mahrouss, Licensed under the Boost Software License. Licensed under the BSL 1.0 license - */ - -#ifndef __OCL_SMART_SOCKET -#define __OCL_SMART_SOCKET - -#include <ocl/detail/config.hpp> - -#ifndef __OCL_FREESTANDING -#include <boost/asio.hpp> - -#ifdef OCL_POSIX -#include <ocl/posix/unique_socket.hpp> -#endif -#endif // !__OCL_FREESTANDING - -#endif // __OCL_SMART_SOCKET
\ No newline at end of file diff --git a/test/.keep b/test/.keep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/.keep diff --git a/test/network_basic/CMakeLists.txt b/test/network_basic/CMakeLists.txt deleted file mode 100644 index 267b584..0000000 --- a/test/network_basic/CMakeLists.txt +++ /dev/null @@ -1,23 +0,0 @@ -cmake_minimum_required(VERSION 3.27) -project(NetworkTestBasic LANGUAGES CXX) - -include(FetchContent) -FetchContent_Declare( - googletest - URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip -) - -# For Windows: Prevent overriding the parent project's compiler/linker settings -set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) -FetchContent_MakeAvailable(googletest) - -enable_testing() - -add_executable(NetworkTestBasic network_basic_test.cc) -target_link_libraries(NetworkTestBasic gtest_main) - -set_property(TARGET NetworkTestBasic PROPERTY CXX_STANDARD 20) -target_include_directories(NetworkTestBasic PUBLIC ../../include/) - -include(GoogleTest) -gtest_discover_tests(NetworkTestBasic) diff --git a/test/network_basic/network_basic_test.cc b/test/network_basic/network_basic_test.cc deleted file mode 100644 index 68442ba..0000000 --- a/test/network_basic/network_basic_test.cc +++ /dev/null @@ -1,49 +0,0 @@ -/* - * File: tests/network_basic_test.cc - * Purpose: Network unit tests in C++ - * Author: Amlal El Mahrouss (amlal@nekernel.org) - * Copyright 2025, Amlal El Mahrouss, licensed under the Boost Software License. - */ - -#include <ocl/posix/unique_socket.hpp> -#include <ocl/print.hpp> -#include <gtest/gtest.h> - -/// @brief Basic Send test -TEST(NetworkTest, BasicNetworkIO) -{ - ocl::unique_socket sock = ocl::unique_socket::make_socket<8000>(ocl::unique_socket::any_address, true); - - std::vector<char> buf_dst(512); - - buf_dst.push_back('H'); - buf_dst.push_back('e'); - buf_dst.push_back('l'); - buf_dst.push_back('l'); - buf_dst.push_back('o'); - buf_dst.push_back('\0'); - - auto buf = buf_dst.data(); - auto sz = buf_dst.size(); - - ocl::unique_socket sock2 = ocl::unique_socket::make_socket<8000>(ocl::unique_socket::any_address, false); - - auto accepeted = sock.accept(); - accepeted.write_from_buffer(buf, sz); - - std::vector<char> buf_dst2(512); - - buf = buf_dst2.data(); - sz = buf_dst2.size(); - - sock2.read_client_buffer(buf, sz); - - EXPECT_TRUE(!sock.bad()); - EXPECT_TRUE(!sock2.bad()); -} - -TEST(NetworkTest, BasicNetworkMakeSocket) -{ - auto socket = ocl::unique_socket::make_socket<8000>(ocl::unique_socket::any_address, true); - EXPECT_TRUE(!socket.bad()); -} |
