From eabad44d4bcc45a255fb7bdda409e6f7a1a47e64 Mon Sep 17 00:00:00 2001 From: EricksOliveira Date: Fri, 18 Oct 2024 14:40:25 -0300 Subject: [PATCH] Revert "Update PlayerbotCommandServer.cpp" This reverts commit 8f2455b7663c935fb33d3552bb1371390d1106f8. --- src/PlayerbotCommandServer.cpp | 46 +++++----------------------------- 1 file changed, 6 insertions(+), 40 deletions(-) diff --git a/src/PlayerbotCommandServer.cpp b/src/PlayerbotCommandServer.cpp index c08f1d05..482c2348 100644 --- a/src/PlayerbotCommandServer.cpp +++ b/src/PlayerbotCommandServer.cpp @@ -9,10 +9,8 @@ #include #include #include -#include #include #include -#include #include "IoContext.h" #include "Playerbots.h" @@ -20,39 +18,21 @@ using boost::asio::ip::tcp; typedef boost::shared_ptr socket_ptr; -std::mutex session_mutex; // Global mutex to protect sessions -boost::asio::thread_pool pool(4); // 4-thread thread pool - bool ReadLine(socket_ptr sock, std::string* buffer, std::string* line) { - // Check if the socket is valid before using it - if (!sock || !sock->is_open()) - { - LOG_ERROR("playerbots", "Invalid or closed socket."); - return false; - } - - // Does the actual reading until the buffer has a '\n' + // Do the real reading from fd until buffer has '\n'. std::string::iterator pos; while ((pos = find(buffer->begin(), buffer->end(), '\n')) == buffer->end()) { char buf[1025]; boost::system::error_code error; - - // Read socket data size_t n = sock->read_some(boost::asio::buffer(buf), error); - if (n == 0 || error == boost::asio::error::eof) - { - LOG_INFO("playerbots", "Connection closed by peer."); + if (n == -1 || error == boost::asio::error::eof) return false; - } else if (error) - { - LOG_ERROR("playerbots", "Socket read error: {}", error.message()); - return false; // Returns false in case of error. - } + throw boost::system::system_error(error); // Some other error. - buf[n] = 0; // Ensures the buffer ends with null + buf[n] = 0; *buffer += buf; } @@ -65,8 +45,6 @@ void session(socket_ptr sock) { try { - std::lock_guard guard(session_mutex); // Protect session with mutex - std::string buffer, request; while (ReadLine(sock, &buffer, &request)) { @@ -77,19 +55,7 @@ void session(socket_ptr sock) } catch (std::exception& e) { - LOG_ERROR("playerbots", "Session error: {}", e.what()); - } - - // Make sure to close the socket at the end of the session - if (sock && sock->is_open()) - { - boost::system::error_code ec; - sock->shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec); - sock->close(ec); - if (ec) - { - LOG_ERROR("playerbots", "Error closing socket: {}", ec.message()); - } + LOG_ERROR("playerbots", "{}", e.what()); } } @@ -100,7 +66,7 @@ void server(Acore::Asio::IoContext& io_service, short port) { socket_ptr sock(new tcp::socket(io_service)); a.accept(*sock); - boost::asio::post(pool, boost::bind(session, sock)); // Use thread pool instead of creating new threads + boost::thread t(boost::bind(session, sock)); } }