This commit is contained in:
Timur A. Fatkhullin 2025-09-25 12:11:48 +03:00
parent b8383c1375
commit 4a9ecf8639
2 changed files with 33 additions and 14 deletions

View File

@ -1,5 +1,11 @@
#pragma once #pragma once
/* MOUNT CONTROL COMPONENTS LIBRARY */
/* A VERY SIMPLE NETWORK SERVER IMPLEMENTATION */
#include <filesystem> #include <filesystem>
#include <set> #include <set>
@ -79,8 +85,12 @@ public:
static constexpr std::chrono::duration DEFAULT_RCV_TIMEOUT = std::chrono::hours(12); static constexpr std::chrono::duration DEFAULT_RCV_TIMEOUT = std::chrono::hours(12);
static constexpr std::chrono::duration DEFAULT_SND_TIMEOUT = std::chrono::milliseconds(2000); static constexpr std::chrono::duration DEFAULT_SND_TIMEOUT = std::chrono::milliseconds(2000);
MccNetworkServer(asio::io_context& ctx, LoggerT logger = MccNullLogger{}) // handle received message user function
: _asioContext(ctx), _stopSignal(ctx), _restartSignal(ctx) typedef std::function<std::vector<char>(std::string_view)> handle_message_func_t;
MccNetworkServer(asio::io_context& ctx, const handle_message_func_t& func, LoggerT logger = MccNullLogger{})
: _asioContext(ctx), _handleMessageFunc(func), _stopSignal(ctx), _restartSignal(ctx)
{ {
std::stringstream st; std::stringstream st;
st << std::this_thread::get_id(); st << std::this_thread::get_id();
@ -490,6 +500,8 @@ public:
private: private:
asio::io_context& _asioContext; asio::io_context& _asioContext;
handle_message_func_t _handleMessageFunc;
asio::signal_set _stopSignal, _restartSignal; asio::signal_set _stopSignal, _restartSignal;
std::set<asio::serial_port*> _serialPorts; std::set<asio::serial_port*> _serialPorts;
@ -625,7 +637,7 @@ private:
bool do_read = true; bool do_read = true;
// main client request -- server respond cycle // main "client request -- server respond" cycle
for (;;) { for (;;) {
// receive message // receive message
@ -698,7 +710,8 @@ private:
logDebug(std::format("A command [{}] was received from client (remote endpoint <{}>, thread ID = {})", logDebug(std::format("A command [{}] was received from client (remote endpoint <{}>, thread ID = {})",
comm, r_epn, thr_id)); comm, r_epn, thr_id));
auto resp = handleClientCommand(comm); // auto resp = handleClientCommand(comm);
auto resp = _handleMessageFunc(comm);
// remove received message from the input stream buffer. NOTE: 'msg' is now invalidated!!! // remove received message from the input stream buffer. NOTE: 'msg' is now invalidated!!!
sbuff.consume(msg.size()); sbuff.consume(msg.size());

View File

@ -1,5 +1,10 @@
#pragma once #pragma once
/* MOUNT CONTROL COMPONENTS LIBRARY */
/* BASIC NETWORK PROTOCOL DEFINITIONS */
#include <algorithm> #include <algorithm>
#include <string_view> #include <string_view>
#include "mcc_angle.h" #include "mcc_angle.h"
@ -52,16 +57,17 @@ static constexpr std::string_view MCC_COMMPROTO_COORD_KIND_XY = "XY";
// : MccCoordPairKind::COORDS_KIND_GENERIC; // : MccCoordPairKind::COORDS_KIND_GENERIC;
// } // }
static constexpr MccCoordPairKind mcc_str2pairkind(traits::mcc_char_range auto const& spair) template <mcc::traits::mcc_char_range R>
static constexpr MccCoordPairKind mcc_str2pairkind(R&& spair)
{ {
const auto hash = utils::FNV1aHash(spair); const auto hash = mcc::utils::FNV1aHash(std::forward<R>(spair));
return hash == utils::FNV1aHash(MCC_COMMPROTO_COORD_KIND_RADEC_ICRS) ? MccCoordPairKind::COORDS_KIND_RADEC_ICRS return hash == mcc::utils::FNV1aHash(MCC_COMMPROTO_COORD_KIND_RADEC_ICRS) ? MccCoordPairKind::COORDS_KIND_RADEC_ICRS
: hash == utils::FNV1aHash(MCC_COMMPROTO_COORD_KIND_RADEC) ? MccCoordPairKind::COORDS_KIND_RADEC_APP : hash == mcc::utils::FNV1aHash(MCC_COMMPROTO_COORD_KIND_RADEC) ? MccCoordPairKind::COORDS_KIND_RADEC_APP
: hash == utils::FNV1aHash(MCC_COMMPROTO_COORD_KIND_HADEC) ? MccCoordPairKind::COORDS_KIND_HADEC_APP : hash == mcc::utils::FNV1aHash(MCC_COMMPROTO_COORD_KIND_HADEC) ? MccCoordPairKind::COORDS_KIND_HADEC_APP
: hash == utils::FNV1aHash(MCC_COMMPROTO_COORD_KIND_AZZD) ? MccCoordPairKind::COORDS_KIND_AZZD : hash == mcc::utils::FNV1aHash(MCC_COMMPROTO_COORD_KIND_AZZD) ? MccCoordPairKind::COORDS_KIND_AZZD
: hash == utils::FNV1aHash(MCC_COMMPROTO_COORD_KIND_AZALT) ? MccCoordPairKind::COORDS_KIND_AZALT : hash == mcc::utils::FNV1aHash(MCC_COMMPROTO_COORD_KIND_AZALT) ? MccCoordPairKind::COORDS_KIND_AZALT
: hash == utils::FNV1aHash(MCC_COMMPROTO_COORD_KIND_XY) ? MccCoordPairKind::COORDS_KIND_XY : hash == mcc::utils::FNV1aHash(MCC_COMMPROTO_COORD_KIND_XY) ? MccCoordPairKind::COORDS_KIND_XY
: MccCoordPairKind::COORDS_KIND_GENERIC; : MccCoordPairKind::COORDS_KIND_GENERIC;
} }
@ -269,7 +275,7 @@ bool mcc_netmsg_construct(traits::mcc_output_char_range auto& msg,
traits::mcc_input_char_range auto const& keyword, traits::mcc_input_char_range auto const& keyword,
PTs... params) PTs... params)
{ {
const size_t hash = utils::FNV1aHash(keyword); const size_t hash = mcc::utils::FNV1aHash(keyword);
if (!std::ranges::contains(MCC_COMMPROTO_VALID_KEYS_HASH, hash)) { if (!std::ranges::contains(MCC_COMMPROTO_VALID_KEYS_HASH, hash)) {
return false; return false;
} }