From 87f10dad540971c4aa22d892c676dce590ca189a Mon Sep 17 00:00:00 2001 From: Amlal Date: Sat, 9 Aug 2025 21:54:20 +0200 Subject: feat: move network.hpp to net/ and working on url.hpp Signed-off-by: Amlal --- dev/lib/except/error.hpp | 2 +- dev/lib/fix/network.hpp | 117 ----------------------------------------- dev/lib/fix/parser.hpp | 6 +-- dev/lib/memory/tracked_ptr.hpp | 2 +- dev/lib/net/network.hpp | 117 +++++++++++++++++++++++++++++++++++++++++ dev/lib/net/url.hpp | 8 +++ dev/lib/utility/cgi.hpp | 22 ++++---- dev/lib/utility/embfs.hpp | 8 +-- 8 files changed, 145 insertions(+), 137 deletions(-) delete mode 100644 dev/lib/fix/network.hpp create mode 100644 dev/lib/net/network.hpp create mode 100644 dev/lib/net/url.hpp (limited to 'dev/lib') diff --git a/dev/lib/except/error.hpp b/dev/lib/except/error.hpp index 6e75f16..486d2e6 100644 --- a/dev/lib/except/error.hpp +++ b/dev/lib/except/error.hpp @@ -14,7 +14,7 @@ namespace snu::except using runtime_error = std::runtime_error; using fix_error = runtime_error; using math_error = runtime_error; - using cgi_error = runtime_error; + using cgi_error = runtime_error; } // namespace snu::except #endif // _SNU_ERR_HPP \ No newline at end of file diff --git a/dev/lib/fix/network.hpp b/dev/lib/fix/network.hpp deleted file mode 100644 index edd4c26..0000000 --- a/dev/lib/fix/network.hpp +++ /dev/null @@ -1,117 +0,0 @@ -/* - * File: fix/network.hpp - * Purpose: Financial Information Exchange in C++ - * Author: Amlal El Mahrouss (founder@snu.systems) - * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp. - */ - -#ifndef _SNU_FIX_NETWORK_HPP -#define _SNU_FIX_NETWORK_HPP - -#include -#include -#include -#include - -#define SNU_MODEM : protected snu::fix::basic_delivery_modem - -namespace snu::fix -{ - class basic_delivery_modem; - - typedef int32_t delivery_socket_type; - - /// @brief a delivery modem is a container which establishes a comm. channel between the FIX session and application. - class basic_delivery_modem - { - public: - explicit basic_delivery_modem() = default; - virtual ~basic_delivery_modem() = default; - - basic_delivery_modem& operator=(const basic_delivery_modem&) = default; - basic_delivery_modem(const basic_delivery_modem&) = default; - - public: - static constexpr auto local_address = "127.0.0.1"; - static constexpr auto backlog_count = 18U; - - public: - delivery_socket_type fd_{}; - - template - bool receive(T& out, std::size_t len) noexcept - { - static_assert(std::is_pointer::value, "T is not a pointer!"); - - if (!out) - return false; - - if (!len) - return false; - - auto ret = ::recv(fd_, out, len, MSG_WAITALL); - - return ret > 0; - } - - template - bool transmit(T& out, std::size_t len) noexcept - { - static_assert(std::is_pointer::value, "T is not a pointer!"); - - if (!out) - return false; - - if (!len) - return false; - - auto ret = ::send(fd_, out, len, 0); - - return ret > 0; - } - - template - bool construct(const char* addr = basic_delivery_modem::local_address, const bool& is_server = false) noexcept - { - static_assert(AF != 0, "AF is zero"); - static_assert(Kind != 0, "Kind is zero"); - static_assert(IPProto != 0, "IPProto is zero"); - - fd_ = ::socket(AF, Kind, IPProto); - - if (fd_ < 1) - return false; - - struct sockaddr_in addr_ - { - }; - - addr_.sin_addr.s_addr = ::inet_addr(addr); - addr_.sin_port = Port; - - if (!is_server) - { - auto ret = ::connect(fd_, (struct sockaddr*)&addr_, sizeof(decltype(addr_))); - return ret == 0; - } - - ::listen(fd_, basic_delivery_modem::backlog_count); - - return true; - } - - bool destroy() noexcept - { - if (!fd_) - return false; - - ::shutdown(fd_, SHUT_RDWR); - - fd_ = 0U; - - return true; - } - }; -} // namespace snu::fix - -#endif // ifndef _SNU_FIX_NETWORK_HPP \ No newline at end of file diff --git a/dev/lib/fix/parser.hpp b/dev/lib/fix/parser.hpp index 5701783..f0f7141 100644 --- a/dev/lib/fix/parser.hpp +++ b/dev/lib/fix/parser.hpp @@ -27,7 +27,7 @@ namespace snu::fix struct basic_range; template - struct basic_range_data; + class basic_range_data; /// @brief Buffer+Length structure template @@ -65,7 +65,7 @@ namespace snu::fix bool is_valid() { - return bytes_ && length_ > 0; + return this->bytes_ && this->length_ > 0; } operator bool() @@ -98,7 +98,7 @@ namespace snu::fix static inline const char_type* begin = detail::begin_fix(); explicit basic_range_data() = default; - ~basic_range_data() = default; + ~basic_range_data() = default; basic_range_data& operator=(const basic_range_data&) = default; basic_range_data(const basic_range_data&) = default; diff --git a/dev/lib/memory/tracked_ptr.hpp b/dev/lib/memory/tracked_ptr.hpp index 2607b5b..c767b67 100644 --- a/dev/lib/memory/tracked_ptr.hpp +++ b/dev/lib/memory/tracked_ptr.hpp @@ -128,7 +128,7 @@ namespace snu::memory this->reset(); } - tracked_ptr(const tracked_ptr&) = delete; + tracked_ptr(const tracked_ptr&) = delete; tracked_ptr& operator=(const tracked_ptr&) = delete; public: diff --git a/dev/lib/net/network.hpp b/dev/lib/net/network.hpp new file mode 100644 index 0000000..22cc5b2 --- /dev/null +++ b/dev/lib/net/network.hpp @@ -0,0 +1,117 @@ +/* + * 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 +#include +#include +#include + +#define SNU_MODEM : protected snu::net::basic_delivery_modem + +namespace snu::net +{ + class basic_delivery_modem; + + typedef int64_t socket_type; + + /// @brief Delivery modem concept, a container to read and write on a network stream. + class basic_delivery_modem + { + public: + explicit basic_delivery_modem() = default; + virtual ~basic_delivery_modem() = default; + + basic_delivery_modem& operator=(const basic_delivery_modem&) = default; + basic_delivery_modem(const basic_delivery_modem&) = default; + + public: + static constexpr auto local_address = "127.0.0.1"; + static constexpr auto backlog_count = 18U; + + public: + socket_type fd_{}; + + template + bool receive(T& out, std::size_t len) noexcept + { + static_assert(std::is_pointer::value, "T is not a pointer!"); + + if (!out) + return false; + + if (!len) + return false; + + auto ret = ::recv(fd_, out, len, MSG_WAITALL); + + return ret > 0; + } + + template + bool transmit(T& out, std::size_t len) noexcept + { + static_assert(std::is_pointer::value, "T is not a pointer!"); + + if (!out) + return false; + + if (!len) + return false; + + auto ret = ::send(fd_, out, len, 0); + + return ret > 0; + } + + template + bool construct(const char* addr = basic_delivery_modem::local_address, const bool& is_server = false) noexcept + { + static_assert(AF != 0, "AF is zero"); + static_assert(Kind != 0, "Kind is zero"); + static_assert(IPProto != 0, "IPProto is zero"); + + fd_ = ::socket(AF, Kind, IPProto); + + if (fd_ < 1) + return false; + + struct sockaddr_in addr_ + { + }; + + addr_.sin_addr.s_addr = ::inet_addr(addr); + addr_.sin_port = Port; + + if (!is_server) + { + auto ret = ::connect(fd_, (struct sockaddr*)&addr_, sizeof(decltype(addr_))); + return ret == 0; + } + + ::listen(fd_, basic_delivery_modem::backlog_count); + + return true; + } + + bool destroy() noexcept + { + if (!fd_) + return false; + + ::shutdown(fd_, SHUT_RDWR); + + fd_ = 0U; + + return true; + } + }; +} // namespace snu::net + +#endif // ifndef _SNU_NET_NETWORK_HPP \ No newline at end of file diff --git a/dev/lib/net/url.hpp b/dev/lib/net/url.hpp new file mode 100644 index 0000000..6c4dc36 --- /dev/null +++ b/dev/lib/net/url.hpp @@ -0,0 +1,8 @@ +/* + * 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 diff --git a/dev/lib/utility/cgi.hpp b/dev/lib/utility/cgi.hpp index 8e2170e..57625b8 100644 --- a/dev/lib/utility/cgi.hpp +++ b/dev/lib/utility/cgi.hpp @@ -32,35 +32,35 @@ namespace snu public: explicit basic_writer() = default; - ~basic_writer() = default; + ~basic_writer() = default; basic_writer& operator=(const basic_writer&) = default; basic_writer(const basic_writer&) = default; public: - basic_writer& binary(const std::basic_stringstream& ss_html) + basic_writer& binary(const std::basic_stringstream& ss_in) { - return this->eval_("application/octet-stream", ss_html); + return this->eval_("application/octet-stream", ss_in); } - basic_writer& html(const std::basic_stringstream& ss_html) + basic_writer& html(const std::basic_stringstream& ss_in) { - return this->eval_("text/html", ss_html); + return this->eval_("text/html", ss_in); } - basic_writer& xml(const std::basic_stringstream& ss_html) + basic_writer& xml(const std::basic_stringstream& ss_in) { - return this->eval_("application/xml", ss_html); + return this->eval_("application/xml", ss_in); } - basic_writer& json(const std::basic_stringstream& ss_html) + basic_writer& json(const std::basic_stringstream& ss_in) { - return this->eval_("application/json", ss_html); + return this->eval_("application/json", ss_in); } - basic_writer& js(const std::basic_stringstream& ss_html) + basic_writer& js(const std::basic_stringstream& ss_in) { - return this->eval_("text/javascript", ss_html); + return this->eval_("text/javascript", ss_in); } }; } // namespace cgi diff --git a/dev/lib/utility/embfs.hpp b/dev/lib/utility/embfs.hpp index fea379c..3acc867 100644 --- a/dev/lib/utility/embfs.hpp +++ b/dev/lib/utility/embfs.hpp @@ -23,10 +23,10 @@ namespace snu::embfs inline constexpr const size_t _superblock_name_len = 16; inline constexpr const size_t _superblock_reserve_len = 462; - - inline constexpr const size_t _inode_name_len = 128; - inline constexpr const size_t _inode_arr_len = 12; - inline constexpr const size_t _inode_lookup_len = 8; + + inline constexpr const size_t _inode_name_len = 128; + inline constexpr const size_t _inode_arr_len = 12; + inline constexpr const size_t _inode_lookup_len = 8; #ifdef EMBFS_28BIT_LBA typedef std::uint32_t lba_t; -- cgit v1.2.3 From 1d9328259052be88b86b958fdb1b2396307cacdb Mon Sep 17 00:00:00 2001 From: Amlal Date: Mon, 11 Aug 2025 13:03:40 +0200 Subject: feat: SOCL modem improvements and newly added is_valid method to modem. Signed-off-by: Amlal --- dev/lib/fix/parser.hpp | 10 +++++----- dev/lib/net/network.hpp | 41 ++++++++++++++++++++++------------------- dev/lib/net/url.hpp | 10 ++++++++++ 3 files changed, 37 insertions(+), 24 deletions(-) (limited to 'dev/lib') diff --git a/dev/lib/fix/parser.hpp b/dev/lib/fix/parser.hpp index f0f7141..1095e8d 100644 --- a/dev/lib/fix/parser.hpp +++ b/dev/lib/fix/parser.hpp @@ -21,7 +21,7 @@ namespace snu::fix { template - struct basic_visitor; + class basic_visitor; template struct basic_range; @@ -63,12 +63,12 @@ namespace snu::fix char_type* bytes_; size_t length_; - bool is_valid() + bool is_valid() noexcept { return this->bytes_ && this->length_ > 0; } - operator bool() + explicit operator bool() { return this->is_valid(); } @@ -77,7 +77,7 @@ namespace snu::fix /// @brief Convert basic_range to usable string. /// @note This function assumes that the basic_range is valid and contains ASCII bytes. template - inline std::basic_string to_string(basic_range& basic_range) noexcept + std::basic_string to_string(basic_range& basic_range) noexcept { if (basic_range.length_ < 0) return std::basic_string{}; @@ -126,7 +126,7 @@ namespace snu::fix return magic_.starts_with(basic_range_data::begin); } - operator bool() + explicit operator bool() { return this->is_valid(); } diff --git a/dev/lib/net/network.hpp b/dev/lib/net/network.hpp index 22cc5b2..37b6819 100644 --- a/dev/lib/net/network.hpp +++ b/dev/lib/net/network.hpp @@ -13,35 +13,39 @@ #include #include -#define SNU_MODEM : protected snu::net::basic_delivery_modem +#define SNU_MODEM_INTERFACE : protected snu::net::basic_delivery_modem namespace snu::net { - class basic_delivery_modem; + template + class basic_modem; typedef int64_t socket_type; /// @brief Delivery modem concept, a container to read and write on a network stream. - class basic_delivery_modem + template + class basic_modem { public: - explicit basic_delivery_modem() = default; - virtual ~basic_delivery_modem() = default; + explicit basic_modem() = default; + virtual ~basic_modem() = default; - basic_delivery_modem& operator=(const basic_delivery_modem&) = default; - basic_delivery_modem(const basic_delivery_modem&) = default; + basic_modem& operator=(const basic_modem&) = default; + basic_modem(const basic_modem&) = default; - public: static constexpr auto local_address = "127.0.0.1"; static constexpr auto backlog_count = 18U; - public: socket_type fd_{}; - template - bool receive(T& out, std::size_t len) noexcept + bool is_valid() const noexcept + { + return this->fd_ != -1; + } + + bool receive(char_type& out, std::size_t len) noexcept { - static_assert(std::is_pointer::value, "T is not a pointer!"); + static_assert(std::is_pointer::value, "char_type is not a pointer!"); if (!out) return false; @@ -54,10 +58,9 @@ namespace snu::net return ret > 0; } - template - bool transmit(T& out, std::size_t len) noexcept + bool transmit(char_type& out, std::size_t len) noexcept { - static_assert(std::is_pointer::value, "T is not a pointer!"); + static_assert(std::is_pointer::value, "char_type is not a pointer!"); if (!out) return false; @@ -71,7 +74,7 @@ namespace snu::net } template - bool construct(const char* addr = basic_delivery_modem::local_address, const bool& is_server = false) noexcept + bool construct(const char* addr = basic_modem::local_address, const bool& is_server = false) noexcept { static_assert(AF != 0, "AF is zero"); static_assert(Kind != 0, "Kind is zero"); @@ -79,7 +82,7 @@ namespace snu::net fd_ = ::socket(AF, Kind, IPProto); - if (fd_ < 1) + if (fd_ == -1) return false; struct sockaddr_in addr_ @@ -91,11 +94,11 @@ namespace snu::net if (!is_server) { - auto ret = ::connect(fd_, (struct sockaddr*)&addr_, sizeof(decltype(addr_))); + const auto ret = ::connect(fd_, reinterpret_cast(&addr_), sizeof(decltype(addr_))); return ret == 0; } - ::listen(fd_, basic_delivery_modem::backlog_count); + ::listen(fd_, basic_modem::backlog_count); return true; } diff --git a/dev/lib/net/url.hpp b/dev/lib/net/url.hpp index 6c4dc36..0506cd3 100644 --- a/dev/lib/net/url.hpp +++ b/dev/lib/net/url.hpp @@ -6,3 +6,13 @@ */ #pragma once + +#include +#include + +namespace snu::net +{ + template + class basic_url; + +} \ No newline at end of file -- cgit v1.2.3 From 67228fbbe3a1147dee3043d0609c9b903d4d0391 Mon Sep 17 00:00:00 2001 From: Amlal Date: Mon, 11 Aug 2025 17:18:08 +0200 Subject: wip: url: module implementation in progress Signed-off-by: Amlal --- dev/lib/net/url.hpp | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'dev/lib') diff --git a/dev/lib/net/url.hpp b/dev/lib/net/url.hpp index 0506cd3..5dc0d03 100644 --- a/dev/lib/net/url.hpp +++ b/dev/lib/net/url.hpp @@ -9,10 +9,38 @@ #include #include +#include + +/// @author Amlal El Mahrouss (founder@snu.systems) namespace snu::net { template class basic_url; -} \ No newline at end of file + /// @brief Basic URL parser container. + template + class basic_url final + { + std::basic_stringstream 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& in) + { + ss_ += in; + return *this; + } + + basic_url& operator/=(const char_type& in) + { + ss_ += in; + return *this; + } + }; +} // namespace snu::net -- cgit v1.2.3 From 662288d772b9cb7669bafa03adcd84a362002a7b Mon Sep 17 00:00:00 2001 From: Amlal Date: Mon, 11 Aug 2025 18:17:53 +0200 Subject: feat: network module fixes and improvements. Signed-off-by: Amlal --- dev/lib/net/network.hpp | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) (limited to 'dev/lib') diff --git a/dev/lib/net/network.hpp b/dev/lib/net/network.hpp index 37b6819..a64497c 100644 --- a/dev/lib/net/network.hpp +++ b/dev/lib/net/network.hpp @@ -8,12 +8,14 @@ #ifndef _SNU_NET_NETWORK_HPP #define _SNU_NET_NETWORK_HPP +#include #include #include + #include #include -#define SNU_MODEM_INTERFACE : protected snu::net::basic_delivery_modem +#define SNU_MODEM_INTERFACE : protected snu::net::basic_modem namespace snu::net { @@ -33,8 +35,10 @@ namespace snu::net basic_modem& operator=(const basic_modem&) = default; basic_modem(const basic_modem&) = default; - static constexpr auto local_address = "127.0.0.1"; - static constexpr auto backlog_count = 18U; + 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_{}; @@ -43,9 +47,10 @@ namespace snu::net return this->fd_ != -1; } - bool receive(char_type& out, std::size_t len) noexcept + template + bool receive(ptr_type& out, std::size_t len) noexcept { - static_assert(std::is_pointer::value, "char_type is not a pointer!"); + static_assert(std::is_pointer::value, "ptr_type is not a pointer!"); if (!out) return false; @@ -58,9 +63,10 @@ namespace snu::net return ret > 0; } - bool transmit(char_type& out, std::size_t len) noexcept + template + bool transmit(ptr_type& out, std::size_t len) noexcept { - static_assert(std::is_pointer::value, "char_type is not a pointer!"); + static_assert(std::is_pointer::value, "char_type is not a pointer!"); if (!out) return false; @@ -73,14 +79,14 @@ namespace snu::net return ret > 0; } - template - bool construct(const char* addr = basic_modem::local_address, const bool& is_server = false) noexcept + template + bool construct(const char* addr = basic_modem::local_address_ip4, const bool& is_server = false) noexcept { - static_assert(AF != 0, "AF is zero"); - static_assert(Kind != 0, "Kind is zero"); - static_assert(IPProto != 0, "IPProto is zero"); + static_assert(af != 0, "AF is zero"); + static_assert(kind != 0, "Kind is zero"); + static_assert(ip_proto != 0, "IPProto is zero"); - fd_ = ::socket(AF, Kind, IPProto); + fd_ = ::socket(af, kind, ip_proto); if (fd_ == -1) return false; @@ -90,7 +96,7 @@ namespace snu::net }; addr_.sin_addr.s_addr = ::inet_addr(addr); - addr_.sin_port = Port; + addr_.sin_port = port; if (!is_server) { @@ -109,6 +115,7 @@ namespace snu::net return false; ::shutdown(fd_, SHUT_RDWR); + ::close(fd_); fd_ = 0U; @@ -117,4 +124,4 @@ namespace snu::net }; } // namespace snu::net -#endif // ifndef _SNU_NET_NETWORK_HPP \ No newline at end of file +#endif // ifndef _SNU_NET_NETWORK_HPP -- cgit v1.2.3 From 2450c6579543ae94b3dcb2af4893cfc6ecd0fcef Mon Sep 17 00:00:00 2001 From: Amlal Date: Mon, 11 Aug 2025 19:46:35 +0200 Subject: feat: add 'tools' directory for future tooling in python. Signed-off-by: Amlal --- dev/lib/net/network.hpp | 2 +- tools/.gitkeep | 0 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 tools/.gitkeep (limited to 'dev/lib') diff --git a/dev/lib/net/network.hpp b/dev/lib/net/network.hpp index a64497c..a58cb1f 100644 --- a/dev/lib/net/network.hpp +++ b/dev/lib/net/network.hpp @@ -37,7 +37,7 @@ namespace snu::net 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_{}; diff --git a/tools/.gitkeep b/tools/.gitkeep new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3 From 54e32a8a3535d99d104775687af0731b0be3b129 Mon Sep 17 00:00:00 2001 From: Amlal Date: Tue, 12 Aug 2025 11:53:13 +0200 Subject: wip: work in progress network tests using GTest. Reworked print module too. Signed-off-by: Amlal --- dev/lib/fix/parser.hpp | 2 +- dev/lib/io/print.hpp | 24 +++++++++++++++----- dev/lib/net/network.hpp | 22 +++++++++++++----- dev/lib/net/url.hpp | 8 +++++++ dev/tests/fix_basic/CMakeLists.txt | 2 +- dev/tests/fix_basic/fix_test.cc | 2 +- dev/tests/network_basic/CMakeLists.txt | 23 +++++++++++++++++++ dev/tests/network_basic/net_test.cc | 36 ++++++++++++++++++++++++++++++ dev/tests/tracked_ptr_basic/CMakeLists.txt | 2 +- dev/tests/tracked_ptr_leak/CMakeLists.txt | 2 +- 10 files changed, 107 insertions(+), 16 deletions(-) create mode 100644 dev/tests/network_basic/CMakeLists.txt create mode 100644 dev/tests/network_basic/net_test.cc (limited to 'dev/lib') diff --git a/dev/lib/fix/parser.hpp b/dev/lib/fix/parser.hpp index 1095e8d..0f2287e 100644 --- a/dev/lib/fix/parser.hpp +++ b/dev/lib/fix/parser.hpp @@ -13,7 +13,7 @@ #include #include #include - +#include #include #include #include diff --git a/dev/lib/io/print.hpp b/dev/lib/io/print.hpp index e326845..36192c4 100644 --- a/dev/lib/io/print.hpp +++ b/dev/lib/io/print.hpp @@ -8,20 +8,34 @@ #ifndef _SNU_PRINT_HPP #define _SNU_PRINT_HPP +#include #include +#include namespace snu::io -{ +{ + template + inline void printv(T fmt) noexcept + { + std::cout << fmt; + } + + template + inline void printv(std::initializer_list fmt) noexcept + { + std::cout << fmt; + } + template - inline void print(T... fmt) + inline void print(T... fmt) noexcept { - std::cout << std::move(fmt...); + (printv(fmt), ...); } template - inline void println(T... fmt) + inline void println(T... fmt) noexcept { - std::cout << std::move(fmt...); + print(static_cast(fmt)...); std::cout << std::endl; } } // namespace snu::io diff --git a/dev/lib/net/network.hpp b/dev/lib/net/network.hpp index a58cb1f..67f3740 100644 --- a/dev/lib/net/network.hpp +++ b/dev/lib/net/network.hpp @@ -11,7 +11,7 @@ #include #include #include - +#include #include #include @@ -38,7 +38,7 @@ namespace snu::net static constexpr auto local_address_ip6 = "127.0.0.1"; static constexpr auto local_address_ip4 = "::1"; - static constexpr auto backlog_count = 18U; + static constexpr auto backlog_count = 18U; socket_type fd_{}; @@ -58,7 +58,7 @@ namespace snu::net if (!len) return false; - auto ret = ::recv(fd_, out, len, MSG_WAITALL); + auto ret = ::recv(fd_, out, len, 0); return ret > 0; } @@ -79,12 +79,22 @@ namespace snu::net return ret > 0; } + template + bool transmit(std::basic_string out) noexcept + { + if (out.empty()) + return false; + + auto ret = ::send(fd_, out.data(), out.size(), 0); + + return ret > 0; + } + template bool construct(const char* addr = basic_modem::local_address_ip4, const bool& is_server = false) noexcept { - static_assert(af != 0, "AF is zero"); - static_assert(kind != 0, "Kind is zero"); - static_assert(ip_proto != 0, "IPProto is zero"); + static_assert(af != 0, "Address family is zero"); + static_assert(kind != 0, "Type is zero"); fd_ = ::socket(af, kind, ip_proto); diff --git a/dev/lib/net/url.hpp b/dev/lib/net/url.hpp index 5dc0d03..9a28f9b 100644 --- a/dev/lib/net/url.hpp +++ b/dev/lib/net/url.hpp @@ -18,10 +18,15 @@ namespace snu::net template class basic_url; + template + class basic_url_traits; + /// @brief Basic URL parser container. template class basic_url final { + friend basic_url_traits; + std::basic_stringstream ss_{}; public: @@ -33,6 +38,9 @@ namespace snu::net basic_url& operator/=(const std::basic_string& in) { + if (in.empty()) + return *this; + ss_ += in; return *this; } diff --git a/dev/tests/fix_basic/CMakeLists.txt b/dev/tests/fix_basic/CMakeLists.txt index 3a2542d..f792277 100644 --- a/dev/tests/fix_basic/CMakeLists.txt +++ b/dev/tests/fix_basic/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(TrackedPtrTests) +project(SOCLTests) include(FetchContent) FetchContent_Declare( diff --git a/dev/tests/fix_basic/fix_test.cc b/dev/tests/fix_basic/fix_test.cc index 1ce128f..bd04d07 100644 --- a/dev/tests/fix_basic/fix_test.cc +++ b/dev/tests/fix_basic/fix_test.cc @@ -13,6 +13,6 @@ TEST(FIXTest, BasicFIXUsage) snu::fix::basic_visitor basic_visitor; snu::fix::basic_range_data fix = basic_visitor.visit("8=FIX.4.2|9=65|35=A|49=SERVER|56=CLIENT|34=177|52=20090107-18:15:16|98=0|108=30|10=062|"); - EXPECT_EQ(fix.magic_, "FIX.4.2"); + EXPECT_EQ(fix.magic_, snu::fix::detail::begin_fix()); EXPECT_TRUE(fix.is_valid()); } \ No newline at end of file diff --git a/dev/tests/network_basic/CMakeLists.txt b/dev/tests/network_basic/CMakeLists.txt new file mode 100644 index 0000000..9e0fc6b --- /dev/null +++ b/dev/tests/network_basic/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.10) +project(SOCLTests) + +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 net_test.cc) +target_link_libraries(NetworkTestBasic gtest_main) + +set_property(TARGET NetworkTestBasic PROPERTY CXX_STANDARD 20) +target_include_directories(NetworkTestBasic PUBLIC ../../) + +include(GoogleTest) +gtest_discover_tests(NetworkTestBasic) diff --git a/dev/tests/network_basic/net_test.cc b/dev/tests/network_basic/net_test.cc new file mode 100644 index 0000000..72bcda0 --- /dev/null +++ b/dev/tests/network_basic/net_test.cc @@ -0,0 +1,36 @@ +/* + * File: tests/tracked_ptr_test.cc + * Purpose: Custom smart pointer unit tests in C++ + * Author: Amlal El Mahrouss (founder@snu.systems) + * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp. + */ + +#include +#include +#include +#include + +TEST(NetworkTest, BasicNetworkUsage) +{ + + snu::net::basic_modem modem; + modem.construct(snu::net::basic_modem::local_address_ip4, true); + + snu::net::basic_modem modem_cl; + modem.construct(snu::net::basic_modem::local_address_ip4, false); + + EXPECT_TRUE(modem_cl.is_valid()); + EXPECT_TRUE(modem.is_valid()); + + std::basic_string buf_dst = "HELLO, NET!"; + char* buf = new char[buf_dst.size()]; + + modem_cl.transmit(buf_dst); + modem.receive(buf, buf_dst.size()); + + snu::io::println<>(buf); + snu::io::println<>(buf_dst); + + delete[] buf; + buf = nullptr; +} \ No newline at end of file diff --git a/dev/tests/tracked_ptr_basic/CMakeLists.txt b/dev/tests/tracked_ptr_basic/CMakeLists.txt index 97f7204..9100e9a 100644 --- a/dev/tests/tracked_ptr_basic/CMakeLists.txt +++ b/dev/tests/tracked_ptr_basic/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(TrackedPtrTests) +project(SOCLTests) include(FetchContent) FetchContent_Declare( diff --git a/dev/tests/tracked_ptr_leak/CMakeLists.txt b/dev/tests/tracked_ptr_leak/CMakeLists.txt index 17df8f1..625a1dc 100644 --- a/dev/tests/tracked_ptr_leak/CMakeLists.txt +++ b/dev/tests/tracked_ptr_leak/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(TrackedPtrTests) +project(SOCLTests) include(FetchContent) FetchContent_Declare( -- cgit v1.2.3 From 37bc0133c137cdf7fe62e79ac8208ec46096316e Mon Sep 17 00:00:00 2001 From: Amlal Date: Thu, 14 Aug 2025 19:15:53 +0200 Subject: feat: mk.test tool, test a header file with static asserts. Signed-off-by: Amlal --- dev/lib/except/hpptest.hpp | 9 +++++++++ dev/lib/net/url.hpp | 10 ++++++++++ tools/hpptest.py | 10 ++++++++++ 3 files changed, 29 insertions(+) create mode 100644 dev/lib/except/hpptest.hpp create mode 100755 tools/hpptest.py (limited to 'dev/lib') diff --git a/dev/lib/except/hpptest.hpp b/dev/lib/except/hpptest.hpp new file mode 100644 index 0000000..5277780 --- /dev/null +++ b/dev/lib/except/hpptest.hpp @@ -0,0 +1,9 @@ +#pragma once + +/// @note Place your static asserts here, for hpptest.py! +#ifdef SOCL_HPPTEST +SOCL_HPPTEST_ASSERT(true); +SOCL_HPPTEST_ASSERT(1 + 1 == 2); +#endif + +/// @brief This header file is meant to be a tutorial on how to use mk.test and its macro framework. diff --git a/dev/lib/net/url.hpp b/dev/lib/net/url.hpp index 9a28f9b..496889c 100644 --- a/dev/lib/net/url.hpp +++ b/dev/lib/net/url.hpp @@ -50,5 +50,15 @@ namespace snu::net ss_ += in; return *this; } + + explicit operator bool() + { + return this->is_valid(); + } + + bool is_valid() + { + return ss_.size() > 0; + } }; } // namespace snu::net diff --git a/tools/hpptest.py b/tools/hpptest.py new file mode 100755 index 0000000..2cf8e9d --- /dev/null +++ b/tools/hpptest.py @@ -0,0 +1,10 @@ +#! /usr/bin/env python3 +# -*- coding: utf-8 -*- + +import sys, os + +if __name__ == '__main__': + if len(sys.argv) == 2: + os.system(f"clang++ -std=c++20 -DSOCL_HPPTEST '-DSOCL_HPPTEST_ASSERT(x)=static_assert(x, #x)' {sys.argv[1]} -o a.out && cat {sys.argv[1]}") + print("[TEST] HEADER COMPILATION PASSES") + -- cgit v1.2.3 From 70945244a35e0e6d2a2e0f84ae83f31b9898a0ca Mon Sep 17 00:00:00 2001 From: Amlal Date: Fri, 15 Aug 2025 12:09:28 +0200 Subject: feat: new hpptest and gtest module. Other module improvements. Signed-off-by: Amlal --- README.md | 12 +++++++++--- dev/examples/fix/fix.cc | 2 +- dev/lib/core/includes.hpp | 15 +++++++++++++++ dev/lib/except/hpptest.hpp | 9 --------- dev/lib/fix/parser.hpp | 2 +- dev/lib/io/print.hpp | 19 +++++++++---------- dev/lib/net/url.hpp | 16 ++++++++-------- dev/lib/tests/gtest.hpp | 8 ++++++++ dev/lib/tests/hpptest.hpp | 22 ++++++++++++++++++++++ dev/tests/network_basic/CMakeLists.txt | 4 ++-- dev/tests/network_basic/net_test.cc | 7 ++++--- 11 files changed, 79 insertions(+), 37 deletions(-) create mode 100644 dev/lib/core/includes.hpp delete mode 100644 dev/lib/except/hpptest.hpp create mode 100644 dev/lib/tests/gtest.hpp create mode 100644 dev/lib/tests/hpptest.hpp (limited to 'dev/lib') diff --git a/README.md b/README.md index bdee118..c7bf8c4 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,15 @@ A C++ library with additional modules for your C++ SDLC. -## Getting Started: +## Requirements: -Here is an example of how snu::opt works. +- Boost C++ libraries (https://www.boost.org/) +- Clang (https://clang.llvm.org/) +- Git (https://git-scm.com/) + +## Examples: + +### Logic Module (Option container) ```cpp #include @@ -23,4 +29,4 @@ int main(int argc, char** argv) } ``` -##### (c) 2025 SNU Systems, Corp. \ No newline at end of file +##### (c) 2025 SNU Systems, Corp. diff --git a/dev/examples/fix/fix.cc b/dev/examples/fix/fix.cc index 3656c0c..ce48186 100644 --- a/dev/examples/fix/fix.cc +++ b/dev/examples/fix/fix.cc @@ -4,7 +4,7 @@ licensed under the MIT license */ -#include +#include #include #include #include diff --git a/dev/lib/core/includes.hpp b/dev/lib/core/includes.hpp new file mode 100644 index 0000000..b516e5f --- /dev/null +++ b/dev/lib/core/includes.hpp @@ -0,0 +1,15 @@ +/* + * File: core/includes.hpp + * Purpose: Core includes for the SOCL library. + * Author: Amlal El Mahrouss (founder@snu.systems) + * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp. + */ + +#pragma once + +#include +#include +#include +#include +#include +#include diff --git a/dev/lib/except/hpptest.hpp b/dev/lib/except/hpptest.hpp deleted file mode 100644 index 5277780..0000000 --- a/dev/lib/except/hpptest.hpp +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -/// @note Place your static asserts here, for hpptest.py! -#ifdef SOCL_HPPTEST -SOCL_HPPTEST_ASSERT(true); -SOCL_HPPTEST_ASSERT(1 + 1 == 2); -#endif - -/// @brief This header file is meant to be a tutorial on how to use mk.test and its macro framework. diff --git a/dev/lib/fix/parser.hpp b/dev/lib/fix/parser.hpp index 0f2287e..8181359 100644 --- a/dev/lib/fix/parser.hpp +++ b/dev/lib/fix/parser.hpp @@ -159,7 +159,7 @@ namespace snu::fix if (in.empty()) return ret; - thread_local std::basic_string in_tmp{}; + static thread_local std::basic_string in_tmp{}; in_tmp.reserve(in.size()); diff --git a/dev/lib/io/print.hpp b/dev/lib/io/print.hpp index 36192c4..eb425c1 100644 --- a/dev/lib/io/print.hpp +++ b/dev/lib/io/print.hpp @@ -13,31 +13,30 @@ #include namespace snu::io -{ - template - inline void printv(T fmt) noexcept +{ + template + inline void print(T fmt, Args... other) noexcept { std::cout << fmt; + print(other...); } template - inline void printv(std::initializer_list fmt) noexcept + inline void print(T fmt) noexcept { std::cout << fmt; } - template - inline void print(T... fmt) noexcept + inline void print() noexcept { - (printv(fmt), ...); + std::cout << std::endl; } template inline void println(T... fmt) noexcept { - print(static_cast(fmt)...); - std::cout << std::endl; + print(fmt...); } } // namespace snu::io -#endif // ifndef _SNU_PRINT_HPP \ No newline at end of file +#endif // ifndef _SNU_PRINT_HPP diff --git a/dev/lib/net/url.hpp b/dev/lib/net/url.hpp index 496889c..f6dba95 100644 --- a/dev/lib/net/url.hpp +++ b/dev/lib/net/url.hpp @@ -51,14 +51,14 @@ namespace snu::net return *this; } - explicit operator bool() - { - return this->is_valid(); - } + explicit operator bool() + { + return this->is_valid(); + } - bool is_valid() - { - return ss_.size() > 0; - } + bool is_valid() + { + return ss_.size() > 0; + } }; } // namespace snu::net diff --git a/dev/lib/tests/gtest.hpp b/dev/lib/tests/gtest.hpp new file mode 100644 index 0000000..14474c0 --- /dev/null +++ b/dev/lib/tests/gtest.hpp @@ -0,0 +1,8 @@ +/* + * File: tests/gtest.hpp + * Purpose: Google Test wrapper for the SOCL library. + * Author: Amlal El Mahrouss (founder@snu.systems) + * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp. + */ + +#include diff --git a/dev/lib/tests/hpptest.hpp b/dev/lib/tests/hpptest.hpp new file mode 100644 index 0000000..4c99ce6 --- /dev/null +++ b/dev/lib/tests/hpptest.hpp @@ -0,0 +1,22 @@ +/* + * File: tests/hpptest.hpp + * Purpose: HPP Test wrapper for the SOCL library. + * Author: Amlal El Mahrouss (founder@snu.systems) + * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp. + */ + +#pragma once + + +#ifdef SOCL_HPPTEST +namespace snu::hpptest +{ + typedef bool condition_type; + + template + consteval inline void must_pass() + { + SOCL_HPPTEST_ASSERT(expr); + } +} // namespace snu::hpptest +#endif diff --git a/dev/tests/network_basic/CMakeLists.txt b/dev/tests/network_basic/CMakeLists.txt index 9e0fc6b..a5704fc 100644 --- a/dev/tests/network_basic/CMakeLists.txt +++ b/dev/tests/network_basic/CMakeLists.txt @@ -1,5 +1,5 @@ -cmake_minimum_required(VERSION 3.10) -project(SOCLTests) +cmake_minimum_required(VERSION 3.28) +project(SOCLTests LANGUAGES CXX) include(FetchContent) FetchContent_Declare( diff --git a/dev/tests/network_basic/net_test.cc b/dev/tests/network_basic/net_test.cc index 9423fe7..634048e 100644 --- a/dev/tests/network_basic/net_test.cc +++ b/dev/tests/network_basic/net_test.cc @@ -7,8 +7,8 @@ #include #include +#include #include -#include TEST(NetworkTest, BasicNetworkUsage) { @@ -27,8 +27,9 @@ TEST(NetworkTest, BasicNetworkUsage) modem_cl.transmit(buf_dst); modem.receive(buf, buf_dst.size()); - snu::io::println<>(buf); - snu::io::println<>(buf_dst); + snu::io::print(buf_dst); + snu::io::print(buf); + snu::io::print(); delete[] buf; buf = nullptr; -- cgit v1.2.3 From 95db0ac7dcb8625c5a1e92408c0c02962b205871 Mon Sep 17 00:00:00 2001 From: Amlal Date: Sat, 16 Aug 2025 19:16:11 +0200 Subject: feat: core: include inside dev/lib/core Signed-off-by: Amlal --- dev/lib/core/includes.hpp | 1 + tools/hpptest.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'dev/lib') diff --git a/dev/lib/core/includes.hpp b/dev/lib/core/includes.hpp index b516e5f..e482a7c 100644 --- a/dev/lib/core/includes.hpp +++ b/dev/lib/core/includes.hpp @@ -7,6 +7,7 @@ #pragma once +#include #include #include #include diff --git a/tools/hpptest.py b/tools/hpptest.py index 2cf8e9d..f9fcd1c 100755 --- a/tools/hpptest.py +++ b/tools/hpptest.py @@ -5,6 +5,6 @@ import sys, os if __name__ == '__main__': if len(sys.argv) == 2: - os.system(f"clang++ -std=c++20 -DSOCL_HPPTEST '-DSOCL_HPPTEST_ASSERT(x)=static_assert(x, #x)' {sys.argv[1]} -o a.out && cat {sys.argv[1]}") + os.system(f"clang++ -std=c++20 -DSOCL_HPPTEST '-DSOCL_HPPTEST_ASSERT(x)=static_assert(x, #x)' {sys.argv[1]}") print("[TEST] HEADER COMPILATION PASSES") -- cgit v1.2.3