Simpify network console

This commit is contained in:
Benau 2018-02-23 14:57:59 +08:00
parent 1023e6580e
commit 7d14954012
7 changed files with 46 additions and 95 deletions

View File

@ -987,6 +987,9 @@ int handleCmdLine()
} }
// Networking command lines // Networking command lines
if(CommandLine::has("--start-console"))
STKHost::m_enable_console = true;
NetworkConfig::get()-> NetworkConfig::get()->
setMaxPlayers(UserConfigParams::m_server_max_players); setMaxPlayers(UserConfigParams::m_server_max_players);
if (CommandLine::has("--port", &n)) if (CommandLine::has("--port", &n))
@ -1040,7 +1043,7 @@ int handleCmdLine()
Log::warn("main", "No saved online player session to create a wan server"); 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()->setServerName(core::stringw(s.c_str()));
NetworkConfig::get()->setIsServer(true); NetworkConfig::get()->setIsServer(true);
@ -1059,9 +1062,6 @@ int handleCmdLine()
if(CommandLine::has("--max-players", &n)) if(CommandLine::has("--max-players", &n))
UserConfigParams::m_server_max_players=n; UserConfigParams::m_server_max_players=n;
if(CommandLine::has("--start-console"))
STKHost::m_enable_console = true;
if(CommandLine::has("--login", &s) ) if(CommandLine::has("--login", &s) )
{ {
login = s.c_str(); login = s.c_str();

View File

@ -277,7 +277,7 @@ void MainLoop::run()
float dt = 1.0f / stk_config->m_physics_fps; float dt = 1.0f / stk_config->m_physics_fps;
left_over_time -= num_steps * dt ; left_over_time -= num_steps * dt ;
if (!ProfileWorld::isNoGraphics() && STKHost::existHost() && if (STKHost::existHost() &&
STKHost::get()->requestedShutdown()) STKHost::get()->requestedShutdown())
{ {
STKHost::get()->shutdown(); STKHost::get()->shutdown();
@ -285,13 +285,18 @@ void MainLoop::run()
{ {
race_manager->exitRace(); race_manager->exitRace();
} }
GUIEngine::Screen* new_stack[] = if (!ProfileWorld::isNoGraphics())
{ {
MainMenuScreen::getInstance(), OnlineScreen::getInstance(), NULL GUIEngine::Screen* new_stack[] =
}; {
StateManager::get()->resetAndSetStack(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(); 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 // Add a Time step entry to the rewind list, which can store all

View File

@ -21,7 +21,7 @@
#define HEADER_MAIN_LOOP_HPP #define HEADER_MAIN_LOOP_HPP
typedef unsigned long Uint32; typedef unsigned long Uint32;
#include <atomic>
/** Management class for the whole gameflow, this is where the /** Management class for the whole gameflow, this is where the
main-loop is */ main-loop is */
@ -29,7 +29,7 @@ class MainLoop
{ {
private: private:
/** True if the main loop should exit. */ /** True if the main loop should exit. */
bool m_abort; std::atomic_bool m_abort;
/** True if the frame rate should be throttled. */ /** True if the frame rate should be throttled. */
bool m_throttle_fps; bool m_throttle_fps;

View File

@ -16,9 +16,6 @@
// along with this program; if not, write to the Free Software // along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // 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_config.hpp"
#include "network/network_player_profile.hpp" #include "network/network_player_profile.hpp"
#include "network/stk_host.hpp" #include "network/stk_host.hpp"
@ -28,52 +25,37 @@
#include "utils/log.hpp" #include "utils/log.hpp"
#include "utils/time.hpp" #include "utils/time.hpp"
#include "utils/vs.hpp" #include "utils/vs.hpp"
#include "main_loop.hpp"
#include <iostream> #include <iostream>
namespace NetworkConsole
NetworkConsole::NetworkConsole()
{ {
m_localhost = NULL; // ----------------------------------------------------------------------------
m_thread_keyboard = NULL; void kickAllPlayers(STKHost* host)
} {
const std::vector<STKPeer*> &peers = host->getPeers();
for (unsigned int i = 0; i < peers.size(); i++)
{
peers[i]->disconnect();
}
} // kickAllPlayers
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
NetworkConsole::~NetworkConsole() void mainLoop(STKHost* host)
{
#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)
{ {
VS::setThreadName("NetworkConsole"); VS::setThreadName("NetworkConsole");
NetworkConsole *me = static_cast<NetworkConsole*>(data);
std::string str = ""; std::string str = "";
bool stop = false; while (!host->requestedShutdown())
while (!stop)
{ {
getline(std::cin, str); getline(std::cin, str);
if (str == "quit") if (str == "quit")
{ {
stop = true; host->requestShutdown();
} }
else if (str == "kickall" && NetworkConfig::get()->isServer()) else if (str == "kickall" && NetworkConfig::get()->isServer())
{ {
me->kickAllPlayers(); kickAllPlayers(host);
} }
else if (str == "start" && NetworkConfig::get()->isServer()) else if (str == "start" && NetworkConfig::get()->isServer())
{ {
@ -91,7 +73,7 @@ void* NetworkConsole::mainLoop(void* data)
getline(std::cin, str2); getline(std::cin, str2);
auto clrp = LobbyProtocol::get<ClientLobby>(); auto clrp = LobbyProtocol::get<ClientLobby>();
std::vector<NetworkPlayerProfile*> players = std::vector<NetworkPlayerProfile*> players =
STKHost::get()->getMyPlayerProfiles(); host->getMyPlayerProfiles();
// For now send a vote for each local player // For now send a vote for each local player
for(unsigned int i=0; i<players.size(); i++) for(unsigned int i=0; i<players.size(); i++)
{ {
@ -106,7 +88,7 @@ void* NetworkConsole::mainLoop(void* data)
getline(std::cin, str2); getline(std::cin, str2);
auto clrp = LobbyProtocol::get<ClientLobby>(); auto clrp = LobbyProtocol::get<ClientLobby>();
std::vector<NetworkPlayerProfile*> players = std::vector<NetworkPlayerProfile*> players =
STKHost::get()->getMyPlayerProfiles(); host->getMyPlayerProfiles();
if (str2 == "track") if (str2 == "track")
{ {
std::cin >> str2; std::cin >> str2;
@ -157,16 +139,6 @@ void* NetworkConsole::mainLoop(void* data)
} }
} // while !stop } // while !stop
main_loop->abort(); main_loop->abort();
return NULL;
} // mainLoop } // mainLoop
// ---------------------------------------------------------------------------- }
void NetworkConsole::kickAllPlayers()
{
const std::vector<STKPeer*> &peers = STKHost::get()->getPeers();
for (unsigned int i = 0; i < peers.size(); i++)
{
peers[i]->disconnect();
}
} // kickAllPlayers

View File

@ -19,38 +19,11 @@
#ifndef HEADER_NETWORK_CONSOLE_HPP #ifndef HEADER_NETWORK_CONSOLE_HPP
#define HEADER_NETWORK_CONSOLE_HPP #define HEADER_NETWORK_CONSOLE_HPP
#include "utils/types.hpp"
#include "pthread.h"
class NetworkString;
class STKHost; class STKHost;
class NetworkConsole namespace NetworkConsole
{ {
protected: void mainLoop(STKHost* host);
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; }
}; // class NetworkConsole }; // class NetworkConsole
#endif // SERVER_CONSOLE_HPP #endif // SERVER_CONSOLE_HPP

View File

@ -353,12 +353,11 @@ void STKHost::init()
ProtocolManager::createInstance(); ProtocolManager::createInstance();
// Optional: start the network console // 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 = std::thread(std::bind(&NetworkConsole::mainLoop,
m_network_console->run(); this));
} }
} // STKHost } // STKHost
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -367,6 +366,9 @@ void STKHost::init()
*/ */
STKHost::~STKHost() STKHost::~STKHost()
{ {
requestShutdown();
if (m_network_console.joinable())
m_network_console.join();
// delete the game setup // delete the game setup
if (m_game_setup) if (m_game_setup)
delete m_game_setup; delete m_game_setup;

View File

@ -42,7 +42,6 @@
#include <thread> #include <thread>
class GameSetup; class GameSetup;
class NetworkConsole;
class STKHost class STKHost
{ {
@ -67,8 +66,8 @@ private:
/** ENet host interfacing sockets. */ /** ENet host interfacing sockets. */
Network* m_network; Network* m_network;
/** Network console */ /** Network console thread */
NetworkConsole *m_network_console; std::thread m_network_console;
/** The list of peers connected to this instance. */ /** The list of peers connected to this instance. */
std::vector<STKPeer*> m_peers; std::vector<STKPeer*> m_peers;