This commit is contained in:
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
/* MOUNT CONTROL COMPONENTS LIBRARY */
/* A VERY SIMPLE NETWORK SERVER IMPLEMENTATION */
#include <filesystem>
#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_SND_TIMEOUT = std::chrono::milliseconds(2000);
MccNetworkServer(asio::io_context& ctx, LoggerT logger = MccNullLogger{})
: _asioContext(ctx), _stopSignal(ctx), _restartSignal(ctx)
// handle received message user function
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;
st << std::this_thread::get_id();
@@ -490,6 +500,8 @@ public:
private:
asio::io_context& _asioContext;
handle_message_func_t _handleMessageFunc;
asio::signal_set _stopSignal, _restartSignal;
std::set<asio::serial_port*> _serialPorts;
@@ -625,7 +637,7 @@ private:
bool do_read = true;
// main client request -- server respond cycle
// main "client request -- server respond" cycle
for (;;) {
// receive message
@@ -698,7 +710,8 @@ private:
logDebug(std::format("A command [{}] was received from client (remote endpoint <{}>, thread 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!!!
sbuff.consume(msg.size());