From 7d14954012a912d7c075d37fe5a81e24fefdc083 Mon Sep 17 00:00:00 2001 From: Benau Date: Fri, 23 Feb 2018 14:57:59 +0800 Subject: [PATCH] Simpify network console --- src/main.cpp | 8 ++--- src/main_loop.cpp | 17 +++++---- src/main_loop.hpp | 4 +-- src/network/network_console.cpp | 64 ++++++++++----------------------- src/network/network_console.hpp | 31 ++-------------- src/network/stk_host.cpp | 12 ++++--- src/network/stk_host.hpp | 5 ++- 7 files changed, 46 insertions(+), 95 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 8fd337154..bd495f47b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -987,6 +987,9 @@ int handleCmdLine() } // Networking command lines + if(CommandLine::has("--start-console")) + STKHost::m_enable_console = true; + NetworkConfig::get()-> setMaxPlayers(UserConfigParams::m_server_max_players); if (CommandLine::has("--port", &n)) @@ -1040,7 +1043,7 @@ int handleCmdLine() Log::warn("main", "No saved online player session to create a wan server"); } } - if (CommandLine::has("--lan-server", &s)) + else if (CommandLine::has("--lan-server", &s)) { NetworkConfig::get()->setServerName(core::stringw(s.c_str())); NetworkConfig::get()->setIsServer(true); @@ -1059,9 +1062,6 @@ int handleCmdLine() if(CommandLine::has("--max-players", &n)) UserConfigParams::m_server_max_players=n; - if(CommandLine::has("--start-console")) - STKHost::m_enable_console = true; - if(CommandLine::has("--login", &s) ) { login = s.c_str(); diff --git a/src/main_loop.cpp b/src/main_loop.cpp index 352325e3d..4b6206743 100644 --- a/src/main_loop.cpp +++ b/src/main_loop.cpp @@ -277,7 +277,7 @@ void MainLoop::run() float dt = 1.0f / stk_config->m_physics_fps; left_over_time -= num_steps * dt ; - if (!ProfileWorld::isNoGraphics() && STKHost::existHost() && + if (STKHost::existHost() && STKHost::get()->requestedShutdown()) { STKHost::get()->shutdown(); @@ -285,13 +285,18 @@ void MainLoop::run() { race_manager->exitRace(); } - GUIEngine::Screen* new_stack[] = + if (!ProfileWorld::isNoGraphics()) { - MainMenuScreen::getInstance(), OnlineScreen::getInstance(), NULL - }; - StateManager::get()->resetAndSetStack(new_stack); + GUIEngine::Screen* new_stack[] = + { + MainMenuScreen::getInstance(), + OnlineScreen::getInstance(), NULL + }; + StateManager::get()->resetAndSetStack(new_stack); + MessageQueue::add(MessageQueue::MT_ERROR, + _("Connection to server is lost.")); + } NetworkConfig::get()->unsetNetworking(); - MessageQueue::add(MessageQueue::MT_ERROR, _("Connection to server is lost.")); } // Add a Time step entry to the rewind list, which can store all diff --git a/src/main_loop.hpp b/src/main_loop.hpp index 0aae68201..766a47c7b 100644 --- a/src/main_loop.hpp +++ b/src/main_loop.hpp @@ -21,7 +21,7 @@ #define HEADER_MAIN_LOOP_HPP typedef unsigned long Uint32; - +#include /** Management class for the whole gameflow, this is where the main-loop is */ @@ -29,7 +29,7 @@ class MainLoop { private: /** True if the main loop should exit. */ - bool m_abort; + std::atomic_bool m_abort; /** True if the frame rate should be throttled. */ bool m_throttle_fps; diff --git a/src/network/network_console.cpp b/src/network/network_console.cpp index 66bf830b1..455d78c81 100644 --- a/src/network/network_console.cpp +++ b/src/network/network_console.cpp @@ -16,9 +16,6 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -#include "network/network_console.hpp" - -#include "main_loop.hpp" #include "network/network_config.hpp" #include "network/network_player_profile.hpp" #include "network/stk_host.hpp" @@ -28,52 +25,37 @@ #include "utils/log.hpp" #include "utils/time.hpp" #include "utils/vs.hpp" +#include "main_loop.hpp" #include - -NetworkConsole::NetworkConsole() +namespace NetworkConsole { - m_localhost = NULL; - m_thread_keyboard = NULL; -} +// ---------------------------------------------------------------------------- +void kickAllPlayers(STKHost* host) +{ + const std::vector &peers = host->getPeers(); + for (unsigned int i = 0; i < peers.size(); i++) + { + peers[i]->disconnect(); + } +} // kickAllPlayers // ---------------------------------------------------------------------------- -NetworkConsole::~NetworkConsole() -{ -#ifndef ANDROID - if (m_thread_keyboard) - pthread_cancel(*m_thread_keyboard);//, SIGKILL); -#endif -} - -// ---------------------------------------------------------------------------- -void NetworkConsole::run() -{ - // listen keyboard console input - m_thread_keyboard = new pthread_t; - pthread_create(m_thread_keyboard, NULL, mainLoop, this); - - Log::info("NetworkConsole", "Ready."); -} // run - -// ---------------------------------------------------------------------------- -void* NetworkConsole::mainLoop(void* data) +void mainLoop(STKHost* host) { VS::setThreadName("NetworkConsole"); - NetworkConsole *me = static_cast(data); std::string str = ""; - bool stop = false; - while (!stop) + while (!host->requestedShutdown()) { getline(std::cin, str); if (str == "quit") { - stop = true; + host->requestShutdown(); } else if (str == "kickall" && NetworkConfig::get()->isServer()) { - me->kickAllPlayers(); + kickAllPlayers(host); } else if (str == "start" && NetworkConfig::get()->isServer()) { @@ -91,7 +73,7 @@ void* NetworkConsole::mainLoop(void* data) getline(std::cin, str2); auto clrp = LobbyProtocol::get(); std::vector players = - STKHost::get()->getMyPlayerProfiles(); + host->getMyPlayerProfiles(); // For now send a vote for each local player for(unsigned int i=0; i(); std::vector players = - STKHost::get()->getMyPlayerProfiles(); + host->getMyPlayerProfiles(); if (str2 == "track") { std::cin >> str2; @@ -157,16 +139,6 @@ void* NetworkConsole::mainLoop(void* data) } } // while !stop main_loop->abort(); - - return NULL; } // mainLoop -// ---------------------------------------------------------------------------- -void NetworkConsole::kickAllPlayers() -{ - const std::vector &peers = STKHost::get()->getPeers(); - for (unsigned int i = 0; i < peers.size(); i++) - { - peers[i]->disconnect(); - } -} // kickAllPlayers +} diff --git a/src/network/network_console.hpp b/src/network/network_console.hpp index 4a429d1fd..299c04a07 100644 --- a/src/network/network_console.hpp +++ b/src/network/network_console.hpp @@ -19,38 +19,11 @@ #ifndef HEADER_NETWORK_CONSOLE_HPP #define HEADER_NETWORK_CONSOLE_HPP -#include "utils/types.hpp" - -#include "pthread.h" - -class NetworkString; class STKHost; -class NetworkConsole +namespace NetworkConsole { -protected: - - STKHost *m_localhost; - - pthread_t* m_thread_keyboard; - - uint8_t m_max_players; - - static void* mainLoop(void* data); - -public: - NetworkConsole(); - virtual ~NetworkConsole(); - - virtual void run(); - void kickAllPlayers(); - // ------------------------------------------------------------------------ - void setMaxPlayers(uint8_t count) { m_max_players = count; } - // ------------------------------------------------------------------------ - uint8_t getMaxPlayers() { return m_max_players; } - // ------------------------------------------------------------------------ - virtual bool isServer() { return true; } - + void mainLoop(STKHost* host); }; // class NetworkConsole #endif // SERVER_CONSOLE_HPP diff --git a/src/network/stk_host.cpp b/src/network/stk_host.cpp index acc6d4206..d3cd29101 100644 --- a/src/network/stk_host.cpp +++ b/src/network/stk_host.cpp @@ -353,12 +353,11 @@ void STKHost::init() ProtocolManager::createInstance(); // Optional: start the network console - m_network_console = NULL; - if(m_enable_console) + if (m_enable_console) { - m_network_console = new NetworkConsole(); - m_network_console->run(); - } + m_network_console = std::thread(std::bind(&NetworkConsole::mainLoop, + this)); + } } // STKHost // ---------------------------------------------------------------------------- @@ -367,6 +366,9 @@ void STKHost::init() */ STKHost::~STKHost() { + requestShutdown(); + if (m_network_console.joinable()) + m_network_console.join(); // delete the game setup if (m_game_setup) delete m_game_setup; diff --git a/src/network/stk_host.hpp b/src/network/stk_host.hpp index 16db2686e..fbaa118bc 100644 --- a/src/network/stk_host.hpp +++ b/src/network/stk_host.hpp @@ -42,7 +42,6 @@ #include class GameSetup; -class NetworkConsole; class STKHost { @@ -67,8 +66,8 @@ private: /** ENet host interfacing sockets. */ Network* m_network; - /** Network console */ - NetworkConsole *m_network_console; + /** Network console thread */ + std::thread m_network_console; /** The list of peers connected to this instance. */ std::vector m_peers;