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
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();

View File

@ -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

View File

@ -21,7 +21,7 @@
#define HEADER_MAIN_LOOP_HPP
typedef unsigned long Uint32;
#include <atomic>
/** 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;

View File

@ -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 <iostream>
NetworkConsole::NetworkConsole()
namespace 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()
{
#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<NetworkConsole*>(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<ClientLobby>();
std::vector<NetworkPlayerProfile*> players =
STKHost::get()->getMyPlayerProfiles();
host->getMyPlayerProfiles();
// For now send a vote for each local player
for(unsigned int i=0; i<players.size(); i++)
{
@ -106,7 +88,7 @@ void* NetworkConsole::mainLoop(void* data)
getline(std::cin, str2);
auto clrp = LobbyProtocol::get<ClientLobby>();
std::vector<NetworkPlayerProfile*> 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<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
#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

View File

@ -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;

View File

@ -42,7 +42,6 @@
#include <thread>
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<STKPeer*> m_peers;