This commit is contained in:
Timur A. Fatkhullin 2024-11-09 23:51:40 +03:00
parent afd1a917b4
commit afa8d09ade
5 changed files with 22 additions and 14 deletions

View File

@ -246,8 +246,9 @@ public:
_netService.asyncReceive( _netService.asyncReceive(
[self, this](netservice_t::async_callback_err_t ec, message_t msg) { [self, this](netservice_t::async_callback_err_t ec, message_t msg) {
if (ec) { if (ec) {
_serverPtr->errorMessage(std::string("asyncReceive operation completed with") + std::string str("asyncReceive operation completed with error: ");
netservice_t::formatError(ec)); netservice_t::formatError(ec, str);
_serverPtr->errorMessage(str);
stop(); stop();
} else { } else {
auto msg_sptr = std::make_shared<message_t>(std::move(msg)); auto msg_sptr = std::make_shared<message_t>(std::move(msg));
@ -260,8 +261,9 @@ public:
*msg_sptr, *msg_sptr,
[self, msg_sptr, this](netservice_t::async_callback_err_t ec) { [self, msg_sptr, this](netservice_t::async_callback_err_t ec) {
if (ec) { if (ec) {
_serverPtr->errorMessage(std::string("asyncSend operation completed with") + std::string str("asyncSend operation completed with error: ");
netservice_t::formatError(ec)); netservice_t::formatError(ec, str);
_serverPtr->errorMessage(str);
stop(); stop();
} else { } else {
start(); start();
@ -391,9 +393,6 @@ public:
auto id = std::forward<IdSerialT>(id_ser_func)(dev_ptr->ident()); auto id = std::forward<IdSerialT>(id_ser_func)(dev_ptr->ident());
_devices.try_emplace(dev_ptr, dev_ptr, id, std::forward<AttrIdDeserialT>(attr_id_deser_func), _devices.try_emplace(dev_ptr, dev_ptr, id, std::forward<AttrIdDeserialT>(attr_id_deser_func),
std::forward<CmdIdDeserialT>(cmd_id_deser_func)); std::forward<CmdIdDeserialT>(cmd_id_deser_func));
// _devices.try_emplace(dev_ptr, dev_ptr, std::forward<IdSerialT>(id_ser_func),
// std::forward<AttrIdDeserialT>(attr_id_deser_func),
// std::forward<CmdIdDeserialT>(cmd_id_deser_func));
return *this; return *this;
} }

View File

@ -159,6 +159,9 @@ concept adc_netservice_c = requires(SRVT srv, const SRVT srv_const) {
{ srv.receive(std::declval<const typename SRVT::timeout_t&>()) } -> std::same_as<typename SRVT::recv_msg_t>; { srv.receive(std::declval<const typename SRVT::timeout_t&>()) } -> std::same_as<typename SRVT::recv_msg_t>;
srv.close(); srv.close();
// static method
SRVT::formatError(std::declval<typename SRVT::async_callback_err_t>(), std::declval<std::string&>());
}; };

View File

@ -353,13 +353,16 @@ protected:
_isListening<SessionT>[this][id] = true; _isListening<SessionT>[this][id] = true;
doAccept<SessionT>(acceptor, std::move(id), std::move(sess_ctx)); doAccept<SessionT>(acceptor, std::move(id), std::move(sess_ctx));
} else { } else {
errorMessage(SessionT::netservice_t::formatError(ec)); std::string str{"Cannot start accepting connection: "};
SessionT::netservice_t::formatError(ec, str);
errorMessage(str);
_isListening<SessionT>[this][id] = false; _isListening<SessionT>[this][id] = false;
} }
}); });
} }
virtual void errorMessage(const std::string&) = 0; virtual void errorMessage(const std::string&) {};
}; };

View File

@ -30,6 +30,11 @@ protected:
std::string _serverID; std::string _serverID;
virtual void errorMessage(const std::string& err_msg)
{
logError(err_msg);
}
public: public:
using typename ServerT::server_ident_t; using typename ServerT::server_ident_t;

View File

@ -758,12 +758,10 @@ public:
} }
template <typename OutputItT, typename FormatT = std::string_view> static void formatError(std::error_code err, std::string& result_str)
static void formatError(std::error_code err,
OutputItT out_iter,
FormatT fmt = "{} (Err category: {}) (Err msg: {})")
{ {
std::format_to(out_iter, fmt, err.value(), err.category().name(), err.message()); std::format_to(std::back_inserter(result_str), "{} (Err category: {}) (Err msg: {})", err.value(),
err.category().name(), err.message());
} }
protected: protected: