X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/37324ea9b8c06d96b9383be993da02a01f103253..32d9767958eece65c3d6feb02419a3f003dc2f0a:/projects/stargazer/plugins/other/radius/server.cpp diff --git a/projects/stargazer/plugins/other/radius/server.cpp b/projects/stargazer/plugins/other/radius/server.cpp index 4d16b6cb..850847fa 100644 --- a/projects/stargazer/plugins/other/radius/server.cpp +++ b/projects/stargazer/plugins/other/radius/server.cpp @@ -1,18 +1,32 @@ #include "server.h" #include "radproto/packet_codes.h" +#include "stg/common.h" +#include #include -#include using STG::Server; using boost::system::error_code; -Server::Server(boost::asio::io_service& io_service, const std::string& secret, uint16_t port, const std::string& filePath) +Server::Server(boost::asio::io_service& io_service, const std::string& secret, uint16_t port, const std::string& filePath, std::stop_token token, PluginLogger& logger) : m_radius(io_service, secret, port), - m_dictionaries(filePath) + m_dictionaries(filePath), + m_token(std::move(token)), + m_logger(logger) +{ + start(); +} + +void Server::start() { startReceive(); } +void Server::stop() +{ + error_code ec; + m_radius.close(ec); +} + void Server::startReceive() { m_radius.asyncReceive([this](const auto& error, const auto& packet, const boost::asio::ip::udp::endpoint& source){ handleReceive(error, packet, source); }); @@ -42,27 +56,33 @@ RadProto::Packet Server::makeResponse(const RadProto::Packet& request) void Server::handleSend(const error_code& ec) { - if (ec) - std::cout << "Error asyncSend: " << ec.message() << "\n"; + if (m_token.stop_requested()) + return; + if (ec) + { + m_logger("Error asyncSend: %s", ec.message().c_str()); + printfd(__FILE__, "Error asyncSend: '%s'\n", ec.message().c_str()); + } startReceive(); } void Server::handleReceive(const error_code& error, const std::optional& packet, const boost::asio::ip::udp::endpoint& source) { + if (m_token.stop_requested()) + return; + if (error) { - std::cout << "Error asyncReceive: " << error.message() << "\n"; - return; + m_logger("Error asyncReceive: %s", error.message().c_str()); + printfd(__FILE__, "Error asyncReceive: '%s'\n", error.message().c_str()); } if (packet == std::nullopt) { - std::cout << "Error asyncReceive: the request packet is missing\n"; + m_logger("Error asyncReceive: the request packet is missing\n"); + printfd(__FILE__, "Error asyncReceive: the request packet is missing\n"); return; } - else - { - m_radius.asyncSend(makeResponse(*packet), source, [this](const auto& ec){ handleSend(ec); }); - } + m_radius.asyncSend(makeResponse(*packet), source, [this](const auto& ec){ handleSend(ec); }); }