...
This commit is contained in:
parent
b09cb001d6
commit
3640a0d719
@ -1,6 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
#include <asio/awaitable.hpp>
|
#include <asio/awaitable.hpp>
|
||||||
|
#include <asio/deferred.hpp>
|
||||||
|
#include <asio/detached.hpp>
|
||||||
#include <asio/ip/tcp.hpp>
|
#include <asio/ip/tcp.hpp>
|
||||||
#include <asio/local/seq_packet_protocol.hpp>
|
#include <asio/local/seq_packet_protocol.hpp>
|
||||||
#include <asio/local/stream_protocol.hpp>
|
#include <asio/local/stream_protocol.hpp>
|
||||||
@ -48,6 +52,67 @@ public:
|
|||||||
|
|
||||||
~MccMountServer() {}
|
~MccMountServer() {}
|
||||||
|
|
||||||
|
|
||||||
|
template <typename... CtorArgTs>
|
||||||
|
asio::awaitable<void> listen(MccServerEndpoint endpoint, CtorArgTs&&... ctor_args)
|
||||||
|
{
|
||||||
|
if (!endpoint.isValid()) {
|
||||||
|
_serverLogger->error("Cannot start listening! Invalid endpoint string representation ('{}')!",
|
||||||
|
endpoint.endpoint());
|
||||||
|
co_return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// add root path to endpoint one
|
||||||
|
std::filesystem::path pt("/");
|
||||||
|
pt += endpoint.path();
|
||||||
|
|
||||||
|
auto args = std::make_tuple(std::forward<CtorArgTs>(ctor_args)...);
|
||||||
|
|
||||||
|
if (endpoint.isLocalSerial()) {
|
||||||
|
asio::serial_port s_port(_asioContext);
|
||||||
|
|
||||||
|
std::error_code ec;
|
||||||
|
|
||||||
|
if constexpr (sizeof...(CtorArgTs)) { // options
|
||||||
|
setSerialOpts(s_port, std::forward<CtorArgTs>(ctor_args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
s_port.open(pt.string(), ec);
|
||||||
|
if (ec) {
|
||||||
|
_serverLogger->error("Cannot open serial device '{}' (Error = '{}')!", pt.string(), ec.message());
|
||||||
|
co_return;
|
||||||
|
}
|
||||||
|
|
||||||
|
listen(std::move(s_port));
|
||||||
|
} else if (endpoint.isLocal()) {
|
||||||
|
// create abstract namespace socket endpoint if its path starts from '@' symbol
|
||||||
|
endpoint.makeAbstract('@');
|
||||||
|
|
||||||
|
|
||||||
|
if (endpoint.isLocalStream()) {
|
||||||
|
listen(asio::local::stream_protocol::endpoint(pt.string()));
|
||||||
|
} else if (endpoint.isLocalSeqpacket()) {
|
||||||
|
listen(asio::local::seq_packet_protocol::endpoint(pt.string()));
|
||||||
|
} else {
|
||||||
|
co_return; // ???!!!!
|
||||||
|
}
|
||||||
|
} else if (endpoint.isTCP()) {
|
||||||
|
// resolve hostname
|
||||||
|
try {
|
||||||
|
asio::ip::tcp::resolver res(_asioContext);
|
||||||
|
|
||||||
|
auto r_result = co_await res.async_resolve(endpoint.host(), endpoint.portView(), asio::deferred);
|
||||||
|
|
||||||
|
listen(std::move(*r_result.begin()));
|
||||||
|
|
||||||
|
} catch (const std::system_error& err) {
|
||||||
|
_serverLogger->error("An error occured while resolving '{}' hostname (Error = '{}')", endpoint.host(),
|
||||||
|
err.code().message());
|
||||||
|
co_return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void listen(traits::mcc_endpoint_c auto endpoint)
|
void listen(traits::mcc_endpoint_c auto endpoint)
|
||||||
{
|
{
|
||||||
using epn_t = std::decay_t<decltype(endpoint)>;
|
using epn_t = std::decay_t<decltype(endpoint)>;
|
||||||
@ -206,6 +271,37 @@ private:
|
|||||||
void doAccept()
|
void doAccept()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// helpers
|
||||||
|
template <typename OptT, typename... OptTs>
|
||||||
|
void setSerialOpts(asio::serial_port& s_port, OptT&& opt, OptTs&&... opts)
|
||||||
|
{
|
||||||
|
std::error_code ec;
|
||||||
|
|
||||||
|
s_port.set_option(opt, ec);
|
||||||
|
if (ec) {
|
||||||
|
std::string_view opt_name;
|
||||||
|
|
||||||
|
if constexpr (std::same_as<OptT, asio::serial_port::baud_rate>) {
|
||||||
|
opt_name = "baud rate";
|
||||||
|
} else if constexpr (std::same_as<OptT, asio::serial_port::parity>) {
|
||||||
|
opt_name = "parity";
|
||||||
|
} else if constexpr (std::same_as<OptT, asio::serial_port::flow_control>) {
|
||||||
|
opt_name = "flow control";
|
||||||
|
} else if constexpr (std::same_as<OptT, asio::serial_port::stop_bits>) {
|
||||||
|
opt_name = "stop bits";
|
||||||
|
} else if constexpr (std::same_as<OptT, asio::serial_port::character_size>) {
|
||||||
|
opt_name = "char size";
|
||||||
|
}
|
||||||
|
|
||||||
|
_serverLogger->error("Cannot set serial port '{}' option! Just skip!", opt_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
if constexpr (sizeof...(OptTs)) {
|
||||||
|
setSerialOpts(s_port, std::forward<OptTs>(opts)...);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace mcc
|
} // namespace mcc
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user