This commit is contained in:
Timur A. Fatkhullin
2024-10-29 01:21:24 +03:00
parent 1047b57013
commit 4e3e3ec60e
7 changed files with 109 additions and 87 deletions

View File

@@ -25,7 +25,13 @@ public:
template <interfaces::adc_netsession_proto_c SessProtoT, std::derived_from<AdcEndpointParser> EptT>
#ifdef USE_OPENSSL_WITH_ASIO
void start(const EptT& endpoint,
asio::ssl::context tls_context = asio::ssl::context(asio::ssl::context::tlsv13_server),
asio::ssl::verify_mode tls_verify_mode = asio::ssl::context_base::verify_peer)
#else
void start(const EptT& endpoint)
#endif
{
if (!endpoint.isValid()) {
return;
@@ -38,10 +44,11 @@ public:
asio::ip::tcp::endpoint ept(asio::ip::make_address(endpoint.host()), endpoint.port());
if (endpoint.isTCP()) {
using srv_t = AdcNetServiceASIO<asio::ip::tcp, SessProtoT>;
AdcDeviceNetServer::start<Session<srv_t>>("TCP", this, _ioContext, ept);
AdcGenericNetServer::start<Session<srv_t>>("TCP", this, _ioContext, ept);
} else {
using srv_t = AdcNetServiceASIOTLS<asio::ip::tcp, SessProtoT>;
AdcDeviceNetServer::start<Session<srv_t>>("TLS", this, _ioContext, ept);
AdcGenericNetServer::start<Session<srv_t>>("TLS", this, _ioContext, ept, tls_context, tls_verify_mode);
}
#else
if (endpoint.isTCP()) {
@@ -53,7 +60,7 @@ public:
if (endpoint.isLocalStream()) {
asio::local::stream_protocol::endpoint ept(endpoint.template path<std::string>());
using srv_t = AdcNetServiceASIO<asio::local::stream_protocol, SessProtoT>;
AdcDeviceNetServer::start<Session<srv_t>>("LOCAL STREAM", this, _ioContext, ept);
AdcGenericNetServer::start<Session<srv_t>>("LOCAL STREAM", this, _ioContext, ept);
// } else if (endpoint.isLocalDatagram()) {
// asio::local::datagram_protocol::endpoint ept(endpoint.template path<std::string>());
// using srv_t = AdcNetServiceASIO<asio::local::datagram_protocol, SessProtoT>;
@@ -61,7 +68,7 @@ public:
} else if (endpoint.isLocalSeqpacket()) {
asio::local::seq_packet_protocol::endpoint ept(endpoint.template path<std::string>());
using srv_t = AdcNetServiceASIO<asio::local::seq_packet_protocol, SessProtoT>;
AdcDeviceNetServer::start<Session<srv_t>>("LOCAL SEQPACK", this, _ioContext, ept);
AdcGenericNetServer::start<Session<srv_t>>("LOCAL SEQPACK", this, _ioContext, ept);
}
} else {
throw std::system_error(std::make_error_code(std::errc::protocol_not_supported));

View File

@@ -183,7 +183,9 @@ public:
auto timer = netservice_t::getDeadlineTimer(_acceptor, timeout);
auto srv = std::make_unique<netservice_t>(_ioContext);
// auto srv = std::make_unique<netservice_t>(_ioContext);
auto srv = netservice_t::isTLS ? std::make_unique<netservice_t>(_ioContext, srv->_tlsContext)
: std::make_unique<netservice_t>(_ioContext);
return asio::async_compose<TokenT, void(std::error_code, netservice_t)>(
[timer = std::move(timer), srv = std::move(srv), state = sock_accept, this](
@@ -254,6 +256,11 @@ public:
return accept(timeout);
}
void close(std::error_code& ec)
{
_acceptor.close(ec);
}
private:
asio::io_context& _ioContext;
srv_acceptor_t _acceptor;
@@ -273,14 +280,14 @@ public:
}
AdcBaseNetServiceASIO(socket_t socket)
: SESSION_PROTOT(),
_ioContext(static_cast<asio::io_context&>(socket.get_executor().context())),
_socket(std::move(socket)),
_receiveStrand(_ioContext),
_receiveQueue()
{
}
// AdcBaseNetServiceASIO(socket_t socket)
// : SESSION_PROTOT(),
// _ioContext(static_cast<asio::io_context&>(socket.get_executor().context())),
// _socket(std::move(socket)),
// _receiveStrand(_ioContext),
// _receiveQueue()
// {
// }
#ifdef USE_OPENSSL_WITH_ASIO
AdcBaseNetServiceASIO(asio::io_context& ctx,
@@ -300,6 +307,7 @@ public:
AdcBaseNetServiceASIO(AdcBaseNetServiceASIO&& other)
requires(!isTLS)
: _ioContext(other._ioContext),
_receiveStrand(std::move(other._receiveStrand)),
_socket(std::move(other._socket)),
@@ -312,6 +320,23 @@ public:
_streamBuffer.commit(bytes);
}
#ifdef USE_OPENSSL_WITH_ASIO
AdcBaseNetServiceASIO(AdcBaseNetServiceASIO&& other)
requires isTLS
: _ioContext(other._ioContext),
_receiveStrand(std::move(other._receiveStrand)),
_socket(std::move(other._socket)),
_sessSocket(std::move(other._sessSocket)),
_streamBuffer(),
_receiveQueue(std::move(other._receiveQueue)),
_tlsContext(std::move(other._tlsContext)),
_tlsPeerVerifyMode(std::move(other._tlsPeerVerifyMode))
{
auto bytes = asio::buffer_copy(_streamBuffer.prepare(other._streamBuffer.size()), other._streamBuffer.data());
_streamBuffer.commit(bytes);
}
#endif
AdcBaseNetServiceASIO(const AdcBaseNetServiceASIO&) = delete; // no copy constructor!
@@ -321,6 +346,7 @@ public:
AdcBaseNetServiceASIO& operator=(const AdcBaseNetServiceASIO&) = delete;
AdcBaseNetServiceASIO& operator=(AdcBaseNetServiceASIO&& other)
requires(!isTLS)
{
_ioContext = other._ioContext;
_receiveStrand = std::move(other._receiveStrand);
@@ -335,7 +361,29 @@ public:
return *this;
};
#ifdef USE_OPENSSL_WITH_ASIO
AdcBaseNetServiceASIO& operator=(AdcBaseNetServiceASIO&& other)
requires isTLS
{
_ioContext = other._ioContext;
_receiveStrand = std::move(other._receiveStrand);
_receiveQueue = std::move(other._receiveQueue);
_socket = std::move(other._socket);
_sessSocket = std::move(other._sessSocket);
_tlsContext = std::move(other._tlsContext);
_tlsPeerVerifyMode = std::move(other._tlsPeerVerifyMode);
_streamBuffer.consume(_streamBuffer.size());
auto bytes = asio::buffer_copy(_streamBuffer.prepare(other._streamBuffer.size()), other._streamBuffer.data());
_streamBuffer.commit(bytes);
return *this;
};
#endif
constexpr netservice_ident_t ident() const
{
return _ident;
@@ -424,7 +472,7 @@ public:
return _socket.async_send(buff_seq, std::move(self));
} else if constexpr (std::derived_from<socket_t, asio::basic_seq_packet_socket<
typename socket_t::protocol_type>>) {
return _socket.async_send(buff_seq, std::move(self));
return _socket.async_send(buff_seq, 0, std::move(self));
} else {
static_assert(false, "UNKNOWN ASIO-LIBRARY SOCKET TYPE!!!");
}
@@ -772,8 +820,12 @@ protected:
std::chrono::steady_clock::time_point::max() - std::chrono::steady_clock::now() -
std::chrono::seconds(1));
timer->expires_after(timeout < max_d ? timeout : max_d); // to avoid overflow!
// timer->expires_after(timeout);
if (timeout < max_d) {
timer->expires_after(timeout);
} else {
timer->expires_after(max_d); // to avoid overflow!
}
timer->async_wait([&obj](const std::error_code& ec) mutable {
if (!ec) {