...
This commit is contained in:
@@ -72,8 +72,11 @@ static constexpr bool is_local_seqpack_proto = std::derived_from<T, asio::local:
|
||||
} // namespace traits
|
||||
|
||||
|
||||
|
||||
/* A GENERIC NETWORK SERVER IMPLEMENTATION */
|
||||
|
||||
template <mcc_logger_c LoggerT = MccNullLogger>
|
||||
class MccNetworkServer : public LoggerT
|
||||
class MccGenericNetworkServer : public LoggerT
|
||||
{
|
||||
public:
|
||||
using LoggerT::logDebug;
|
||||
@@ -89,8 +92,8 @@ public:
|
||||
typedef std::function<std::vector<char>(std::string_view)> handle_message_func_t;
|
||||
|
||||
|
||||
MccNetworkServer(asio::io_context& ctx, const handle_message_func_t& func)
|
||||
requires std::same_as<LoggerT, MccNullLogger>
|
||||
MccGenericNetworkServer(asio::io_context& ctx, const handle_message_func_t& func)
|
||||
requires std::is_default_constructible_v<LoggerT>
|
||||
: _asioContext(ctx), _handleMessageFunc(func), _stopSignal(ctx), _restartSignal(ctx)
|
||||
{
|
||||
std::stringstream st;
|
||||
@@ -100,8 +103,8 @@ public:
|
||||
}
|
||||
|
||||
template <typename... LoggerCtorArgsTs>
|
||||
MccNetworkServer(asio::io_context& ctx, const handle_message_func_t& func, LoggerCtorArgsTs&&... log_args)
|
||||
requires(not std::same_as<LoggerT, MccNullLogger>)
|
||||
MccGenericNetworkServer(asio::io_context& ctx, const handle_message_func_t& func, LoggerCtorArgsTs&&... log_args)
|
||||
requires(not std::is_default_constructible_v<LoggerT>)
|
||||
: LoggerT(std::forward<LoggerCtorArgsTs>(log_args)...),
|
||||
_asioContext(ctx),
|
||||
_handleMessageFunc(func),
|
||||
@@ -123,7 +126,7 @@ public:
|
||||
// logInfo(std::format("Create mount server instance (thread ID = {})", st.str()));
|
||||
// }
|
||||
|
||||
~MccNetworkServer()
|
||||
virtual ~MccGenericNetworkServer()
|
||||
{
|
||||
std::stringstream st;
|
||||
st << std::this_thread::get_id();
|
||||
@@ -522,7 +525,7 @@ public:
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
protected:
|
||||
asio::io_context& _asioContext;
|
||||
|
||||
handle_message_func_t _handleMessageFunc;
|
||||
@@ -811,4 +814,54 @@ private:
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template <mcc_logger_c LoggerT = MccNullLogger>
|
||||
class MccGenericMountNetworkServer : public MccGenericNetworkServer<LoggerT>
|
||||
{
|
||||
using base_t = MccGenericNetworkServer<LoggerT>;
|
||||
|
||||
public:
|
||||
template <mcc_generic_mount_c MountT, typename... LoggerCtorArgsTs>
|
||||
MccGenericMountNetworkServer(asio::io_context& ctx, MountT& mount, LoggerCtorArgsTs&&... log_args)
|
||||
: base_t(ctx, {}, std::forward<LoggerCtorArgsTs>(log_args)...)
|
||||
{
|
||||
base_t::_handleMessageFunc = [mount = std::move(mount), this](std::string_view command) {
|
||||
MccNetMessage input_msg;
|
||||
MccNetMessage<std::vector<char>> output_msg;
|
||||
|
||||
if (auto ec = parseMessage(command, input_msg)) {
|
||||
output_msg.construct(MCC_COMMPROTO_KEYWORD_SERVER_ERROR_STR, ec);
|
||||
} else {
|
||||
bool imsg_ok = true;
|
||||
if (input_msg.withKey(MCC_COMMPROTO_KEYWORD_SERVER_ACK_STR)) { // strange!
|
||||
output_msg.construct(MCC_COMMPROTO_KEYWORD_SERVER_ACK_STR, MCC_COMMPROTO_KEYWORD_SERVER_ACK_STR);
|
||||
} else if (input_msg.withKey(MCC_COMMPROTO_KEYWORD_SERVER_ERROR_STR)) { // ??!!!
|
||||
output_msg.construct(MCC_COMMPROTO_KEYWORD_SERVER_ACK_STR, MCC_COMMPROTO_KEYWORD_SERVER_ERROR_STR);
|
||||
} else if (input_msg.withKey(MCC_COMMPROTO_KEYWORD_RESTART_SERVER_STR)) {
|
||||
this->restart();
|
||||
output_msg.construct(MCC_COMMPROTO_KEYWORD_SERVER_ACK_STR,
|
||||
MCC_COMMPROTO_KEYWORD_RESTART_SERVER_STR);
|
||||
}
|
||||
|
||||
if (imsg_ok) { // send ACK
|
||||
// output_msg.construct(MCC_COMMPROTO_KEYWORD_SERVER_ACK_STR, input_msg);
|
||||
}
|
||||
}
|
||||
|
||||
return output_msg;
|
||||
};
|
||||
}
|
||||
|
||||
virtual ~MccGenericMountNetworkServer() {}
|
||||
|
||||
protected:
|
||||
template <typename MSG_T>
|
||||
std::error_code parseMessage(std::string_view msg_bytes, MSG_T& msg)
|
||||
{
|
||||
auto ec = msg.fromCharRange(msg_bytes);
|
||||
|
||||
return ec;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace mcc::network
|
||||
|
||||
Reference in New Issue
Block a user