This commit is contained in:
Timur A. Fatkhullin 2024-06-16 00:11:20 +03:00
parent 9a2baa702d
commit 3e8a113c3a
4 changed files with 60 additions and 31 deletions

View File

@ -43,7 +43,6 @@ if (ASIO_LIBRARY)
net/adc_netservice_asio.h
)
add_compile_options(ASIO::ASIO)
add_compile_definitions(PUBLIC USE_ASIO_LIBRARY)
option(OPENSSL_LIBRARY "Use openssl library for related ASIO-based implementation" ON)

View File

@ -542,7 +542,7 @@ template <typename T>
concept adc_netmessage_c = requires {
typename T::byte_storage_t;
typename T::byte_view_t;
std::derived_from<T, AdcNetMessageInterface<typename T::byte_storage_t, typename T::byte_view_t>>;
requires std::derived_from<T, AdcNetMessageInterface<typename T::byte_storage_t, typename T::byte_view_t>>;
};
// template <typename T, typename IT>
// concept adc_netmessage_c = requires(const T t) { // const methods

View File

@ -9,6 +9,7 @@ ABSTRACT DEVICE COMPONENTS LIBRARY
#include <filesystem>
#include <set>
#include <unordered_map>
#if __has_include(<unistd.h>) // POSIX
#define FORK_EXISTS 1
#include <sys/stat.h>

View File

@ -8,49 +8,82 @@
*/
#ifdef USE_ASIO_LIBRARY
#include <future>
#include "adc_netservice.h"
#ifdef USE_ASIO_LIBRARY
// #include <asio/awaitable.hpp>
#include <asio/basic_datagram_socket.hpp>
#include <asio/basic_seq_packet_socket.hpp>
#include <asio/basic_stream_socket.hpp>
#include <asio/compose.hpp>
// #include <asio/experimental/awaitable_operators.hpp>
#include <asio/ip/tcp.hpp>
#include <asio/ip/udp.hpp>
#include <asio/local/seq_packet_protocol.hpp>
#include <asio/local/stream_protocol.hpp>
#include <asio/read_until.hpp>
#include <asio/ssl.hpp>
#include <asio/ssl/stream.hpp>
#include <asio/steady_timer.hpp>
#include <asio/streambuf.hpp>
#include <asio/use_future.hpp>
#include <asio/write.hpp>
#ifdef USE_OPENSSL_WITH_ASIO
#include <asio/ssl.hpp>
#include <asio/ssl/stream.hpp>
#endif
#include <concepts>
#include "adc_netmsg.h"
namespace adc::traits
{
// still only TCP, UDP and UNIX
template <typename T>
concept adc_asio_inet_proto_c = requires {
requires std::derived_from<T, asio::ip::tcp> || std::derived_from<T, asio::ip::udp> ||
std::derived_from<T, asio::local::seq_packet_protocol> ||
std::derived_from<T, asio::local::stream_protocol>;
};
} // namespace adc::traits
namespace adc::impl
{
template <typename InetProtoT>
template <typename InetProtoT, typename HighLevelSocketT = typename InetProtoT::socket>
class AdcNetServiceASIO : public InetProtoT
{
public:
using socket_t = typename InetProtoT::socket;
using endpoint_t = typename InetProtoT::endpoint;
using high_level_socket_t = HighLevelSocketT;
#ifdef USE_OPENSSL_WITH_ASIO
static_assert(std::is_same_v<HighLevelSocketT, socket_t>
? true
: std::is_same_v<HighLevelSocketT, asio::ssl::stream<InetProtoT>>,
"ONLY BASIC SOCKETS AND SSL::STREAM ARE SUPPORTED!!!");
static constexpr bool IsBasicSocket = std::is_same_v<high_level_socket_t, socket_t>;
#else
static_assert(std::is_same_v<high_level_socket_t, socket_t>,
"HighLevelSocketT AND InetProtoT::socket TYPES MUST BE THE SAME!!!");
#endif
using streambuff_iter_t = asio::buffers_iterator<asio::streambuf::const_buffers_type>;
template <typename... CtorArgTs>
AdcNetServiceASIO(socket_t& sock, CtorArgTs&&... ctor_args)
: InetProtoT(std::forward<CtorArgTs>(ctor_args)...), _socket(sock)
{
}
AdcNetServiceASIO(high_level_socket_t& sock) : _socket(sock) {}
virtual ~AdcNetServiceASIO() = default;
@ -70,11 +103,10 @@ public:
switch (state) {
case starting:
state = cancel_timer;
if constexpr (std::derived_from<socket_t,
asio::ssl::stream<typename socket_t::lowest_layer_type>>) {
return _socket.lowest_layer().async_connect(endpoint, std::move(self));
} else {
if constexpr (AdcNetServiceASIO::IsBasicSocket) {
return _socket.async_connect(endpoint, std::move(self));
} else {
return _socket.lowest_layer().async_connect(endpoint, std::move(self));
}
break;
case cancel_timer:
@ -235,7 +267,12 @@ public:
{
std::error_code ec;
if constexpr (std::derived_from<socket_t, asio::ssl::stream<typename socket_t::lowest_layer_type>>) {
if constexpr (AdcNetServiceASIO::IsBasicSocket) {
_socket.shutdown(stype, ec);
if (!ec) {
_socket.close(ec);
}
} else {
_socket.shutdown(ec); // shutdown OpenSSL stream
if (!ec) {
_socket.lowest_layer().shutdown(stype, ec);
@ -243,18 +280,13 @@ public:
_socket.lowest_layer().close(ec);
}
}
} else {
_socket.shutdown(stype, ec);
if (!ec) {
_socket.close(ec);
}
}
return ec;
}
protected:
socket_t& _socket;
high_level_socket_t& _socket;
asio::streambuf _streamBuffer;
@ -278,14 +310,11 @@ protected:
};
typedef AdcNetService<impl::AdcNetServiceASIO<asio::ip::tcp>> AdcNetServiceAsioTcp;
typedef AdcNetService<impl::AdcNetServiceASIO<asio::local::seq_packet_protocol>> AdcNetServiceAsioLocalSeqPack;
typedef AdcNetService<impl::AdcNetServiceASIO<asio::local::stream_protocol>> AdcNetServiceAsioLocalStream;
} // namespace adc::impl
namespace adc
{
typedef AdcNetService<impl::AdcNetServiceASIO<asio::ip::tcp>> AdcNetServiceAsioTcp;
} // namespace adc
#endif