From f58069b27b637e6ce2ae2ccf35ed10171e3f36a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9=20Lopez?= Date: Thu, 24 Aug 2023 18:24:11 +0200 Subject: [PATCH] Stop messages to tracker creating endless loop --- client/Makefile | 2 +- client/src/main.cpp | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/client/Makefile b/client/Makefile index fb9436f..b5efb57 100644 --- a/client/Makefile +++ b/client/Makefile @@ -4,7 +4,7 @@ SRC = main.cpp \ upnp_init.cpp \ upnp_actions.cpp \ Bot.cpp - + SRC_UPNP = upnp.cpp \ upnp_init.cpp \ upnp_actions.cpp diff --git a/client/src/main.cpp b/client/src/main.cpp index 23b4b6e..df7f45c 100644 --- a/client/src/main.cpp +++ b/client/src/main.cpp @@ -10,7 +10,7 @@ void bot(asio::ip::tcp::socket &socket) b.connectToTracker(); } -void tracker(asio::ip::tcp::socket &socket) +[[noreturn]] void tracker(asio::ip::tcp::socket &socket) { asio::io_service io_service; asio::ip::tcp::acceptor acceptor_server(io_service, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), 3612)); @@ -26,10 +26,12 @@ void tracker(asio::ip::tcp::socket &socket) } }); while (true) { - asio::read_until(socket, asio::dynamic_buffer(received_buffer), "\r\n"); + std::size_t n = asio::read_until(socket, asio::dynamic_buffer(received_buffer), "\r\n"); std::cout << "Sending message to all bots: " << received_buffer << std::endl; + std::string line = received_buffer.substr(0, n); + received_buffer.erase(0, n); for (auto &client : clients) { - asio::write(client, asio::buffer(received_buffer)); + asio::write(client, asio::buffer(line)); } } }