From 5fc830825f4beae0be35e3a9998fb0030889e0c2 Mon Sep 17 00:00:00 2001 From: hiker Date: Thu, 12 Nov 2015 09:53:12 +1100 Subject: [PATCH] Refactoring: Created a separate NetworkConfiguration class which stores all information to create the STKHost. --- sources.cmake | 2 +- src/items/item_manager.cpp | 4 +- src/karts/kart.cpp | 8 +- src/main.cpp | 13 +- src/main_loop.cpp | 5 +- src/modes/standard_race.cpp | 4 +- src/network/network_config.cpp | 57 ++++++ src/network/network_config.hpp | 166 ++++++++++++++++++ src/network/network_console.cpp | 20 ++- src/network/network_world.cpp | 4 +- src/network/protocols/connect_to_peer.cpp | 4 +- src/network/protocols/connect_to_peer.hpp | 4 +- src/network/protocols/connect_to_server.cpp | 69 +++++--- src/network/protocols/connect_to_server.hpp | 4 +- .../protocols/controller_events_protocol.cpp | 8 +- src/network/protocols/get_peer_address.cpp | 5 +- src/network/protocols/get_public_address.cpp | 4 +- .../protocols/kart_update_protocol.cpp | 6 +- src/network/protocols/request_connection.cpp | 26 ++- .../protocols/server_lobby_room_protocol.cpp | 14 +- src/network/protocols/start_game_protocol.cpp | 7 +- src/network/protocols/stop_server.cpp | 4 +- .../protocols/synchronization_protocol.cpp | 7 +- src/network/race_config.cpp | 3 +- src/network/stk_host.cpp | 64 ++++--- src/network/stk_host.hpp | 102 +---------- src/network/stk_peer.hpp | 1 - src/network/transport_address.hpp | 6 +- src/online/server.hpp | 5 +- src/online/servers_manager.cpp | 10 +- src/race/race_manager.cpp | 4 +- src/states_screens/create_server_screen.cpp | 20 ++- .../dialogs/server_info_dialog.cpp | 2 +- src/states_screens/online_screen.cpp | 14 +- 34 files changed, 447 insertions(+), 229 deletions(-) create mode 100644 src/network/network_config.cpp create mode 100644 src/network/network_config.hpp diff --git a/sources.cmake b/sources.cmake index 2317715ec..e48e476be 100644 --- a/sources.cmake +++ b/sources.cmake @@ -1,5 +1,5 @@ # Modify this file to change the last-modified date when you add/remove a file. -# This will then trigger a new cmake run automatically. +# This will then trigger a new cmake run automatically. file(GLOB_RECURSE STK_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.hpp") file(GLOB_RECURSE STK_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.cpp") file(GLOB_RECURSE STK_SHADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "data/shaders/*") diff --git a/src/items/item_manager.cpp b/src/items/item_manager.cpp index 5361556a9..845d6f0c1 100644 --- a/src/items/item_manager.cpp +++ b/src/items/item_manager.cpp @@ -29,8 +29,8 @@ #include "io/file_manager.hpp" #include "karts/abstract_kart.hpp" #include "modes/linear_world.hpp" +#include "network/network_config.hpp" #include "network/network_world.hpp" -#include "network/stk_host.hpp" #include "tracks/quad_graph.hpp" #include "tracks/track.hpp" #include "utils/string_utils.hpp" @@ -320,7 +320,7 @@ void ItemManager::checkItemHit(AbstractKart* kart) // if we're not playing online, pick the item. if (!NetworkWorld::getInstance()->isRunning()) collectedItem(*i, kart); - else if (STKHost::isServer()) + else if (NetworkConfig::get()->isServer()) { collectedItem(*i, kart); NetworkWorld::getInstance()->collectedItem(*i, kart); diff --git a/src/karts/kart.cpp b/src/karts/kart.cpp index e1ad9a0db..94d3bd2c0 100644 --- a/src/karts/kart.cpp +++ b/src/karts/kart.cpp @@ -54,8 +54,8 @@ #include "karts/max_speed.hpp" #include "karts/skidding.hpp" #include "modes/linear_world.hpp" +#include "network/network_config.hpp" #include "network/network_world.hpp" -#include "network/stk_host.hpp" #include "physics/btKart.hpp" #include "physics/btKartRaycast.hpp" #include "physics/physics.hpp" @@ -1314,8 +1314,10 @@ void Kart::update(float dt) PROFILER_POP_CPU_MARKER(); // Check if any item was hit. - // check it if we're not in a network world, or if we're on the server (when network mode is on) - if (!NetworkWorld::getInstance()->isRunning() || STKHost::isServer()) + // check it if we're not in a network world, or if we're on the server + // (when network mode is on) + if (!NetworkWorld::getInstance()->isRunning() || + NetworkConfig::get()->isServer()) ItemManager::get()->checkItemHit(this); static video::SColor pink(255, 255, 133, 253); diff --git a/src/main.cpp b/src/main.cpp index bc09f7ca1..2e2d0afc3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -174,6 +174,7 @@ #include "karts/kart_properties_manager.hpp" #include "modes/demo_world.hpp" #include "modes/profile_world.hpp" +#include "network/network_config.hpp" #include "network/stk_host.hpp" #include "online/profile_manager.hpp" #include "online/request_manager.hpp" @@ -775,9 +776,11 @@ int handleCmdLine() // Networking command lines if(CommandLine::has("--server", &s) ) { - STKHost::setMaxPlayers(UserConfigParams::m_server_max_players); - STKHost::create(core::stringw(s.c_str())); - Log::info("main", "Creating a server."); + NetworkConfig::get()-> + setMaxPlayers(UserConfigParams::m_server_max_players); + NetworkConfig::get()->setServerName(core::stringw(s.c_str())); + STKHost::create(); + Log::info("main", "Creating a server '%s'.", s.c_str()); } if(CommandLine::has("--max-players", &n)) @@ -1504,7 +1507,7 @@ int main(int argc, char *argv[] ) StateManager::get()->resetActivePlayers(); if(input_manager) delete input_manager; // if early crash avoid delete NULL - if(STKHost::isNetworking()) + if(NetworkConfig::get()->isNetworking()) STKHost::get()->abort(); cleanSuperTuxKart(); @@ -1614,7 +1617,7 @@ static void cleanSuperTuxKart() // FIXME: do we need to wait for threads there, can they be // moved further up? Online::ServersManager::deallocate(); - if(STKHost::isNetworking()) + if(NetworkConfig::get()->isNetworking()) STKHost::destroy(); cleanUserConfig(); diff --git a/src/main_loop.cpp b/src/main_loop.cpp index e97a1ea47..34ba99232 100644 --- a/src/main_loop.cpp +++ b/src/main_loop.cpp @@ -30,6 +30,7 @@ #include "input/wiimote_manager.hpp" #include "modes/profile_world.hpp" #include "modes/world.hpp" +#include "network/network_config.hpp" #include "network/protocol_manager.hpp" #include "network/network_world.hpp" #include "network/stk_host.hpp" @@ -177,7 +178,7 @@ void MainLoop::run() PROFILER_POP_CPU_MARKER(); PROFILER_PUSH_CPU_MARKER("Protocol manager update", 0x7F, 0x00, 0x7F); - if (STKHost::isNetworking()) + if (STKHost::existHost()) ProtocolManager::getInstance()->update(); PROFILER_POP_CPU_MARKER(); @@ -188,7 +189,7 @@ void MainLoop::run() else if (!m_abort && ProfileWorld::isNoGraphics()) { PROFILER_PUSH_CPU_MARKER("Protocol manager update", 0x7F, 0x00, 0x7F); - if(STKHost::isNetworking()) + if(NetworkConfig::get()->isNetworking()) ProtocolManager::getInstance()->update(); PROFILER_POP_CPU_MARKER(); diff --git a/src/modes/standard_race.cpp b/src/modes/standard_race.cpp index 7282e5a7a..1a8640edc 100644 --- a/src/modes/standard_race.cpp +++ b/src/modes/standard_race.cpp @@ -21,7 +21,7 @@ #include "items/powerup_manager.hpp" #include "karts/abstract_kart.hpp" #include "karts/controller/controller.hpp" -#include "network/stk_host.hpp" +#include "network/network_config.hpp" //----------------------------------------------------------------------------- StandardRace::StandardRace() : LinearWorld() @@ -124,6 +124,6 @@ void StandardRace::endRaceEarly() } // Finish the active players endSetKartPositions(); setPhase(RESULT_DISPLAY_PHASE); - if (!isNetworkWorld() || STKHost::isServer()) + if (!isNetworkWorld() || NetworkConfig::get()->isServer()) terminateRace(); } // endRaceEarly diff --git a/src/network/network_config.cpp b/src/network/network_config.cpp new file mode 100644 index 000000000..d84369e41 --- /dev/null +++ b/src/network/network_config.cpp @@ -0,0 +1,57 @@ +// +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2015 Joerg Henrichs +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// 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_config.hpp" + +NetworkConfig *NetworkConfig::m_network_config = NULL; + +/** \class NetworkConfig + * This class is the interface between STK and the online code, particularly + * STKHost. It stores all online related properties (e.g. if this is a server + * or a host, name of the server, maximum number of players, ip address, ...). + * They can either be set from the GUI code, or via the command line (for a + * stand-alone server). + * When STKHost is created, it takes all necessary information from this + * instance. + */ +// ============================================================================ +/** Constructor for a client + */ +NetworkConfig::NetworkConfig() +{ + m_network_type = NETWORK_NONE; + m_is_server = false; + m_max_players = 4; + m_is_registered = false; + m_server_name = ""; + m_private_port = 0; + m_my_address.lock(); + m_my_address.getData().clear(); + m_my_address.unlock(); +} // NetworkConfig + +//----------------------------------------------------------------------------- +/** Stores the public address of this host. + */ +void NetworkConfig::setMyAddress(const TransportAddress& addr) +{ + m_my_address.lock(); + m_my_address.getData().copy(addr); + m_my_address.unlock(); +} // setPublicAddress + diff --git a/src/network/network_config.hpp b/src/network/network_config.hpp new file mode 100644 index 000000000..3e13e7f70 --- /dev/null +++ b/src/network/network_config.hpp @@ -0,0 +1,166 @@ +// +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2015 Joerg Henrichs +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/*! \file stk_host.hpp + * \brief Defines an interface to use network low-level functions easily. + */ +#ifndef HEADER_NETWORK_CONFIG +#define HEADER_NETWORK_CONFIG + +#include "network/transport_address.hpp" +#include "utils/synchronised.hpp" + +#include "irrString.h" + +class NetworkConfig +{ +private: + /** The singleton instance. */ + static NetworkConfig *m_network_config; + + enum NetworkType + { NETWORK_NONE, NETWORK_WAN, NETWORK_LAN }; + + /** Keeps the type of network connection: none (yet), LAN or WAN. */ + NetworkType m_network_type; + + /** True if this host is a server, false otherwise. */ + bool m_is_server; + + /** This is either this computer's public IP address, or the LAN + * address in case of a LAN game. With lock since it can + * be updated from a separate thread. */ + Synchronised m_my_address; + + /** Even if this is a WAN server, we also store the private (LAN) + * port number, to allow direct connection to clients on the same + * LAN. */ + uint16_t m_private_port; + + /** Maximum number of players on the server. */ + int m_max_players; + + /** If this is a server, it indicates if this server is registered + * with the stk server. */ + bool m_is_registered; + + /** If this is a server, the server name. */ + irr::core::stringw m_server_name; + + NetworkConfig(); + +public: + /** Singleton get, which creates this object if necessary. */ + static NetworkConfig *get() + { + if(!m_network_config) + m_network_config = new NetworkConfig(); + return m_network_config; + } // get + + // ------------------------------------------------------------------------ + static void destroy() + { + delete m_network_config; // It's ok to delete NULL + m_network_config = NULL; + } // destroy + + // ------------------------------------------------------------------------ + void setMyAddress(const TransportAddress& addr); + + // ------------------------------------------------------------------------ + /** Return if a network setting is happening. A network setting is active + * if a host (server or client) exists. */ + bool isNetworking() const { return m_network_type!=NETWORK_NONE; } + // ------------------------------------------------------------------------ + /** Return true if it's a networked game with a LAN server. */ + bool isLAN() const { return m_network_type == NETWORK_LAN; } + // ------------------------------------------------------------------------ + /** Return true if it's a networked game but with a WAN server. */ + bool isWAN() const { return m_network_type == NETWORK_WAN; } + // ------------------------------------------------------------------------ + /** Set that this is a LAN networked game. */ + void setIsLAN() { m_network_type = NETWORK_LAN; } + // ------------------------------------------------------------------------ + void setIsWAN() { m_network_type = NETWORK_WAN; } + // ------------------------------------------------------------------------ + /** Sets the maximum number of players for this server. */ + void setMaxPlayers(int n) { m_max_players = n; } + // -------------------------------------------------------------------- + /** Returns the maximum number of players for this server. */ + int getMaxPlayers() const { return m_max_players; } + // -------------------------------------------------------------------- + /** Sets if this instance is a server or client. */ + void setIsServer(bool b) { m_is_server = b; } + // -------------------------------------------------------------------- + /** Returns if this instance is a server. */ + bool isServer() const { return m_is_server; } + // -------------------------------------------------------------------- + /** Returns if this instance is a client. */ + bool isclient() const { return !m_is_server; } + // -------------------------------------------------------------------- + /** Sets the name of this server. */ + void setServerName(const irr::core::stringw &name) + { + m_server_name = name; + } // setServerName + // -------------------------------------------------------------------- + /** Returns the server name. */ + const irr::core::stringw& getServerName() const + { + assert(isServer()); + return m_server_name; + } // getServerName + // -------------------------------------------------------------------- + /** Sets if this server is registered with the stk server. */ + void setRegistered(bool registered) + { + assert(isServer()); + m_is_registered = registered; + } // setRegistered + // -------------------------------------------------------------------- + /** Returns if this server is registered with the stk server. */ + bool isRegistered() const + { + assert(isServer()); + return m_is_registered; + } // isRegistered + + // -------------------------------------------------------------------- + /** Returns the IP address of this host. We need to return a copy + * to make sure the address is thread safe (otherwise it could happen + * that e.g. data is taken when the IP address was written, but not + * yet the port). */ + const TransportAddress getMyAddress() const + { + TransportAddress a; + m_my_address.lock(); + a.copy(m_my_address.getData()); + m_my_address.unlock(); + return a; + } // getMyAddress + // ------------------------------------------------------------------------ + /** Sets the private (LAN) port for this instance. */ + void setPrivatePort(uint16_t port) { m_private_port = port; } + // ------------------------------------------------------------------------ + /** Returns the private (LAN) port. */ + uint16_t getPrivatePort() const { return m_private_port; } + +}; // class NetworkConfig + +#endif // HEADER_NETWORK_CONFIG diff --git a/src/network/network_console.cpp b/src/network/network_console.cpp index 012dfa675..cb578eca7 100644 --- a/src/network/network_console.cpp +++ b/src/network/network_console.cpp @@ -1,6 +1,6 @@ // // SuperTuxKart - a fun racing game with go-kart -// Copyright (C) 2013-2015 SuperTuxKart-Team +// Copyright (C) 2015 Joerg Henrichs // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License @@ -19,6 +19,7 @@ #include "network/network_console.hpp" #include "main_loop.hpp" +#include "network/network_config.hpp" #include "network/protocol_manager.hpp" #include "network/stk_host.hpp" #include "network/protocols/client_lobby_room_protocol.hpp" @@ -67,11 +68,11 @@ void* NetworkConsole::mainLoop(void* data) { stop = true; } - else if (str == "kickall" && STKHost::isServer()) + else if (str == "kickall" && NetworkConfig::get()->isServer()) { me->kickAllPlayers(); } - else if (str == "start" && STKHost::isServer()) + else if (str == "start" && NetworkConfig::get()->isServer()) { ServerLobbyRoomProtocol* protocol = static_cast @@ -79,7 +80,7 @@ void* NetworkConsole::mainLoop(void* data) assert(protocol); protocol->startGame(); } - else if (str == "selection" && STKHost::isServer()) + else if (str == "selection" && NetworkConfig::get()->isServer()) { ServerLobbyRoomProtocol* protocol = static_cast @@ -87,17 +88,17 @@ void* NetworkConsole::mainLoop(void* data) assert(protocol); protocol->startSelection(); } - else if (str == "compute_race"&& STKHost::isServer()) + else if (str == "compute_race"&& NetworkConfig::get()->isServer()) { GameSetup* setup = STKHost::get()->getGameSetup(); setup->getRaceConfig()->computeRaceMode(); } - else if (str == "compute_track" && STKHost::isServer()) + else if (str == "compute_track" && NetworkConfig::get()->isServer()) { GameSetup* setup = STKHost::get()->getGameSetup(); setup->getRaceConfig()->computeNextTrack(); } - else if (str == "select" && STKHost::isclient()) + else if (str == "select" && NetworkConfig::get()->isclient()) { std::string str2; getline(std::cin, str2); @@ -106,7 +107,7 @@ void* NetworkConsole::mainLoop(void* data) ClientLobbyRoomProtocol* clrp = static_cast(protocol); clrp->requestKartSelection(str2); } - else if (str == "vote" && STKHost::isclient()) + else if (str == "vote" && NetworkConfig::get()->isclient()) { std::cout << "Vote for ? (track/laps/reversed/major/minor/race#) :"; std::string str2; @@ -153,7 +154,8 @@ void* NetworkConsole::mainLoop(void* data) // FIXME // If STK shuts down, but should receive an input after the network // manager was deleted, the getInstance call will return NULL. - else if (STKHost::isclient() && STKHost::get()->getPeerCount() > 0) + else if (NetworkConfig::get()->isclient() && + STKHost::get()->getPeerCount() > 0) { NetworkString msg(1 + str.size()); msg.ai8(0); diff --git a/src/network/network_world.cpp b/src/network/network_world.cpp index 1a1c8357d..32e311f95 100644 --- a/src/network/network_world.cpp +++ b/src/network/network_world.cpp @@ -1,10 +1,10 @@ #include "network/network_world.hpp" +#include "network/network_config.hpp" #include "network/protocol_manager.hpp" #include "network/protocols/synchronization_protocol.hpp" #include "network/protocols/controller_events_protocol.hpp" #include "network/protocols/game_events_protocol.hpp" -#include "network/stk_host.hpp" #include "modes/world.hpp" #include "karts/controller/controller.hpp" @@ -62,7 +62,7 @@ bool NetworkWorld::isRaceOver() void NetworkWorld::collectedItem(Item *item, AbstractKart *kart) { - assert(STKHost::isServer()); // this is only called in the server + assert(NetworkConfig::get()->isServer()); // this is only called in the server GameEventsProtocol* protocol = static_cast( ProtocolManager::getInstance()->getProtocol(PROTOCOL_GAME_EVENTS)); protocol->collectedItem(item,kart); diff --git a/src/network/protocols/connect_to_peer.cpp b/src/network/protocols/connect_to_peer.cpp index 5d5c8cead..fc63cd3ce 100644 --- a/src/network/protocols/connect_to_peer.cpp +++ b/src/network/protocols/connect_to_peer.cpp @@ -19,6 +19,7 @@ #include "network/protocols/connect_to_peer.hpp" #include "network/event.hpp" +#include "network/network_config.hpp" #include "network/protocols/get_public_address.hpp" #include "network/protocols/get_peer_address.hpp" #include "network/protocols/hide_public_address.hpp" @@ -102,7 +103,8 @@ void ConnectToPeer::asynchronousUpdate() // the Ping protocol to keep the port available. We can't rely on // STKHost::isLAN(), since we might get a LAN connection even if // the server itself accepts connections from anywhere. - if (m_peer_address.getIP() != STKHost::get()->getPublicAddress().getIP()) + if (m_peer_address.getIP() != NetworkConfig::get() + ->getMyAddress().getIP()) { m_current_protocol = new PingProtocol(m_peer_address, /*time-between-ping*/2.0); diff --git a/src/network/protocols/connect_to_peer.hpp b/src/network/protocols/connect_to_peer.hpp index a22eec80e..b8e611d2f 100644 --- a/src/network/protocols/connect_to_peer.hpp +++ b/src/network/protocols/connect_to_peer.hpp @@ -16,8 +16,8 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -#ifndef CONNECT_TO_SERVER_HPP -#define CONNECT_TO_SERVER_HPP +#ifndef CONNECT_TO_PEER_HPP +#define CONNECT_TO_PEER_HPP #include "network/protocol.hpp" #include "network/transport_address.hpp" diff --git a/src/network/protocols/connect_to_server.cpp b/src/network/protocols/connect_to_server.cpp index 76b690006..8e502bbfc 100644 --- a/src/network/protocols/connect_to_server.cpp +++ b/src/network/protocols/connect_to_server.cpp @@ -20,6 +20,7 @@ #include "config/player_manager.hpp" #include "network/event.hpp" +#include "network/network_config.hpp" #include "network/protocols/get_public_address.hpp" #include "network/protocols/get_peer_address.hpp" #include "network/protocols/hide_public_address.hpp" @@ -29,6 +30,7 @@ #include "network/protocol_manager.hpp" #include "network/stk_host.hpp" #include "network/stk_peer.hpp" +#include "online/servers_manager.hpp" #include "utils/time.hpp" #include "utils/log.hpp" @@ -47,7 +49,7 @@ ConnectToServer::ConnectToServer() : Protocol(PROTOCOL_CONNECTION) m_server_id = 0; m_host_id = 0; m_quick_join = true; - m_state = NONE; + m_server_address.clear(); } // ConnectToServer() // ---------------------------------------------------------------------------- @@ -61,7 +63,10 @@ ConnectToServer::ConnectToServer(uint32_t server_id, uint32_t host_id) m_server_id = server_id; m_host_id = host_id; m_quick_join = false; - m_state = NONE; + const Online::Server *server = + Online::ServersManager::get()->getServerByID(server_id); + m_server_address.copy(server->getAddress()); + } // ConnectToServer(server, host) // ---------------------------------------------------------------------------- @@ -71,30 +76,28 @@ ConnectToServer::~ConnectToServer() { } // ~ConnectToServer -// ---------------------------------------------------------------------------- - -bool ConnectToServer::notifyEventAsynchronous(Event* event) -{ - if (event->getType() == EVENT_TYPE_CONNECTED) - { - Log::info("ConnectToServer", "The Connect To Server protocol has " - "received an event notifying that he's connected to the peer."); - m_state = CONNECTED; // we received a message, we are connected - } - return true; -} // notifyEventAsynchronous - // ---------------------------------------------------------------------------- /** Initialise the protocol. */ void ConnectToServer::setup() { Log::info("ConnectToServer", "SETUP"); - m_server_address.clear(); - m_state = NONE; m_current_protocol = NULL; + // In case of LAN we already have the server's and our ip address, + // so we can immediately start requesting a connection. + m_state = NetworkConfig::get()->isLAN() ? GETTING_SERVER_ADDRESS : NONE; } // setup +// ---------------------------------------------------------------------------- +/** Sets the server transport address. This is used in case of LAN networking, + * when we do not query the stk server and instead have the address from the + * LAN server directly. + * \param address Address of server to connect to. + */ +void ConnectToServer::setServerAddress(const TransportAddress &address) +{ +} // setServerAddress + // ---------------------------------------------------------------------------- void ConnectToServer::asynchronousUpdate() { @@ -149,7 +152,7 @@ void ConnectToServer::asynchronousUpdate() // we're in the same lan (same public ip address) !! if (m_server_address.getIP() == - STKHost::get()->getPublicAddress().getIP()) + NetworkConfig::get()->getMyAddress().getIP()) { Log::info("ConnectToServer", "Server appears to be in the same LAN."); @@ -160,7 +163,9 @@ void ConnectToServer::asynchronousUpdate() break; } case REQUESTING_CONNECTION: - if (m_current_protocol->getState() == PROTOCOL_STATE_TERMINATED) + // In case of a LAN server, m_crrent_protocol is NULL + if (NetworkConfig::get()->isLAN() || + m_current_protocol->getState() == PROTOCOL_STATE_TERMINATED) { // Server knows we want to connect Log::info("ConnectToServer", "Connection request made"); @@ -176,7 +181,7 @@ void ConnectToServer::asynchronousUpdate() return; } if (m_server_address.getIP() - == STKHost::get()->getPublicAddress().getIP()) + == NetworkConfig::get()->getMyAddress().getIP()) { // we're in the same lan (same public ip address) !! handleSameLAN(); @@ -268,13 +273,14 @@ void ConnectToServer::registerWithSTKServer() { // Our public address is now known, register details with // STK server. - const TransportAddress& addr = STKHost::get()->getPublicAddress(); + const TransportAddress& addr = NetworkConfig::get()->getMyAddress(); Online::XMLRequest *request = new Online::XMLRequest(); PlayerManager::setUserDetails(request, "set", Online::API::SERVER_PATH); request->addParameter("address", addr.getIP()); request->addParameter("port", addr.getPort()); - request->addParameter("private_port", STKHost::get()->getPort()); + request->addParameter("private_port", + NetworkConfig::get()->getPrivatePort()); Log::info("ConnectToServer", "Registering addr %s", addr.toString().c_str()); @@ -321,8 +327,11 @@ void ConnectToServer::handleQuickConnect() uint16_t port; // If we are using a LAN connection, we need the private (local) port - if (m_server_address.getIP() == STKHost::get()->getPublicAddress().getIP()) + if (m_server_address.getIP() == + NetworkConfig::get()->getMyAddress().getIP()) + { result->get("private_port", &port); + } else result->get("port", &port); @@ -424,3 +433,17 @@ void ConnectToServer::handleSameLAN() m_state = CONNECTING; } } // handleSameLAN + +// ---------------------------------------------------------------------------- + +bool ConnectToServer::notifyEventAsynchronous(Event* event) +{ + if (event->getType() == EVENT_TYPE_CONNECTED) + { + Log::info("ConnectToServer", "The Connect To Server protocol has " + "received an event notifying that he's connected to the peer."); + m_state = CONNECTED; // we received a message, we are connected + } + return true; +} // notifyEventAsynchronous + diff --git a/src/network/protocols/connect_to_server.hpp b/src/network/protocols/connect_to_server.hpp index 1b83bbf36..1a6922357 100644 --- a/src/network/protocols/connect_to_server.hpp +++ b/src/network/protocols/connect_to_server.hpp @@ -55,7 +55,7 @@ private: void handleSameLAN(); public: - ConnectToServer(); + ConnectToServer(); ConnectToServer(uint32_t server_id, uint32_t host_id); virtual ~ConnectToServer(); @@ -64,7 +64,7 @@ public: virtual void asynchronousUpdate(); virtual void callback(Protocol *protocol); virtual void update() OVERRIDE {} - + void setServerAddress(const TransportAddress &address); }; // class ConnectToServer #endif // CONNECT_TO_SERVER_HPP diff --git a/src/network/protocols/controller_events_protocol.cpp b/src/network/protocols/controller_events_protocol.cpp index a7bcbce40..e0f219ae4 100644 --- a/src/network/protocols/controller_events_protocol.cpp +++ b/src/network/protocols/controller_events_protocol.cpp @@ -4,7 +4,9 @@ #include "karts/abstract_kart.hpp" #include "karts/controller/controller.hpp" #include "network/event.hpp" +#include "network/network_config.hpp" #include "network/game_setup.hpp" +#include "network/network_config.hpp" #include "network/network_world.hpp" #include "network/protocol_manager.hpp" #include "network/stk_host.hpp" @@ -39,7 +41,7 @@ void ControllerEventsProtocol::setup() m_self_controller_index = i; } STKPeer* peer = NULL; - if (STKHost::isServer()) + if (NetworkConfig::get()->isServer()) { for (unsigned int j = 0; j < peers.size(); j++) { @@ -116,7 +118,7 @@ bool ControllerEventsProtocol::notifyEventAsynchronous(Event* event) Log::warn("ControllerEventProtocol", "Couldn't have a client id."); return true; } - if (STKHost::isServer()) + if (NetworkConfig::get()->isServer()) { // notify everybody of the event : for (unsigned int i = 0; i < m_controllers.size(); i++) @@ -145,7 +147,7 @@ void ControllerEventsProtocol::update() void ControllerEventsProtocol::controllerAction(Controller* controller, PlayerAction action, int value) { - assert(!STKHost::isServer()); + assert(!NetworkConfig::get()->isServer()); KartControl* controls = controller->getControls(); uint8_t serialized_1 = 0; diff --git a/src/network/protocols/get_peer_address.cpp b/src/network/protocols/get_peer_address.cpp index 4866a3a43..c2c736d20 100644 --- a/src/network/protocols/get_peer_address.cpp +++ b/src/network/protocols/get_peer_address.cpp @@ -20,8 +20,8 @@ #include "config/player_manager.hpp" #include "config/user_config.hpp" +#include "network/network_config.hpp" #include "network/protocol_manager.hpp" -#include "network/stk_host.hpp" #include "online/request_manager.hpp" #include "utils/log.hpp" @@ -65,7 +65,8 @@ void GetPeerAddress::asynchronousUpdate() m_address.setIP(ip); uint16_t port; - if (m_address.getIP() == STKHost::get()->getPublicAddress().getIP()) + uint32_t my_ip = NetworkConfig::get()->getMyAddress().getIP(); + if (m_address.getIP() == my_ip) result->get("private_port", &port); else result->get("port", &port); diff --git a/src/network/protocols/get_public_address.cpp b/src/network/protocols/get_public_address.cpp index 2e93516b0..402c53f80 100644 --- a/src/network/protocols/get_public_address.cpp +++ b/src/network/protocols/get_public_address.cpp @@ -20,8 +20,8 @@ #include "config/user_config.hpp" #include "network/network.hpp" +#include "network/network_config.hpp" #include "network/protocols/connect_to_server.hpp" -#include "network/stk_host.hpp" #include "utils/log.hpp" #include @@ -184,7 +184,7 @@ std::string GetPublicAddress::parseStunResponse() Log::debug("GetPublicAddress", "The public address has been found: %s", address.toString().c_str()); - STKHost::get()->setPublicAddress(address); + NetworkConfig::get()->setMyAddress(address); break; } // type = 0 or 1 pos += 4 + size; diff --git a/src/network/protocols/kart_update_protocol.cpp b/src/network/protocols/kart_update_protocol.cpp index 043e6801e..9abb83ad2 100644 --- a/src/network/protocols/kart_update_protocol.cpp +++ b/src/network/protocols/kart_update_protocol.cpp @@ -3,9 +3,9 @@ #include "karts/abstract_kart.hpp" #include "modes/world.hpp" #include "network/event.hpp" +#include "network/network_config.hpp" #include "network/network_world.hpp" #include "network/protocol_manager.hpp" -#include "network/stk_host.hpp" #include "utils/time.hpp" KartUpdateProtocol::KartUpdateProtocol() : Protocol(PROTOCOL_KART_UPDATE) @@ -77,7 +77,7 @@ void KartUpdateProtocol::update() if (current_time > time + 0.1) // 10 updates per second { time = current_time; - if (STKHost::isServer()) + if (NetworkConfig::get()->isServer()) { NetworkString ns(4+m_karts.size()*32); ns.af( World::getWorld()->getTime()); @@ -118,7 +118,7 @@ void KartUpdateProtocol::update() { uint32_t id = m_karts_ids.back(); // server takes all updates - if (id != m_self_kart_index || STKHost::isServer()) + if (id != m_self_kart_index || NetworkConfig::get()->isServer()) { Vec3 pos = m_next_positions.back(); btTransform transform = m_karts[id]->getBody()->getInterpolationWorldTransform(); diff --git a/src/network/protocols/request_connection.cpp b/src/network/protocols/request_connection.cpp index feadea532..955fc462f 100644 --- a/src/network/protocols/request_connection.cpp +++ b/src/network/protocols/request_connection.cpp @@ -20,6 +20,8 @@ #include "config/player_manager.hpp" #include "config/user_config.hpp" +#include "network/network.hpp" +#include "network/network_config.hpp" #include "network/protocol_manager.hpp" #include "online/servers_manager.hpp" @@ -71,12 +73,26 @@ void RequestConnection::asynchronousUpdate() { case NONE: { - m_request = new ServerJoinRequest(); - PlayerManager::setUserDetails(m_request, "request-connection", Online::API::SERVER_PATH); + if(NetworkConfig::get()->isLAN()) + { + Network *broadcast = new Network(1, 1, 0, 0); - m_request->addParameter("server_id", m_server_id); - m_request->queue(); - m_state = REQUEST_PENDING; + NetworkString s(std::string("connection-request")); + const Online::Server *server = + ServersManager::get()->getServerByID(m_server_id); + broadcast->sendRawPacket(s.getBytes(), s.size(), + server->getAddress()); + m_state = EXITING; + } + else + { + m_request = new ServerJoinRequest(); + PlayerManager::setUserDetails(m_request, "request-connection", Online::API::SERVER_PATH); + + m_request->addParameter("server_id", m_server_id); + m_request->queue(); + m_state = REQUEST_PENDING; + } break; } case REQUEST_PENDING: diff --git a/src/network/protocols/server_lobby_room_protocol.cpp b/src/network/protocols/server_lobby_room_protocol.cpp index ef8f40a30..2a9b4ca4c 100644 --- a/src/network/protocols/server_lobby_room_protocol.cpp +++ b/src/network/protocols/server_lobby_room_protocol.cpp @@ -22,6 +22,7 @@ #include "config/user_config.hpp" #include "modes/world.hpp" #include "network/event.hpp" +#include "network/network_config.hpp" #include "network/network_world.hpp" #include "network/protocols/get_public_address.hpp" #include "network/protocols/connect_to_peer.hpp" @@ -60,7 +61,7 @@ void ServerLobbyRoomProtocol::setup() // In case of LAN we don't need our public address or register with the STK // server, so we can directly go to the working state. - m_state = STKHost::isLAN() ? WORKING : NONE; + m_state = NetworkConfig::get()->isLAN() ? WORKING : NONE; m_selection_enabled = false; m_in_race = false; m_current_protocol = NULL; @@ -177,7 +178,7 @@ void ServerLobbyRoomProtocol::callback(Protocol *protocol) void ServerLobbyRoomProtocol::registerServer() { Online::XMLRequest *request = new Online::XMLRequest(); - const TransportAddress& addr = STKHost::get()->getPublicAddress(); + const TransportAddress& addr = NetworkConfig::get()->getMyAddress(); #ifdef NEW_PROTOCOL PlayerManager::setUserDetails(request, "register", Online::API::SERVER_PATH); #else @@ -185,8 +186,9 @@ void ServerLobbyRoomProtocol::registerServer() #endif request->addParameter("address", addr.getIP() ); request->addParameter("port", addr.getPort() ); - request->addParameter("private_port", STKHost::get()->getPort() ); - request->addParameter("name", STKHost::get()->getServerName() ); + request->addParameter("private_port", + NetworkConfig::get()->getPrivatePort()); + request->addParameter("name", NetworkConfig::get()->getServerName() ); request->addParameter("max_players", UserConfigParams::m_server_max_players ); Log::info("RegisterServer", "Showing addr %s", addr.toString().c_str()); @@ -258,7 +260,7 @@ void ServerLobbyRoomProtocol::checkIncomingConnectionRequests() PlayerManager::setUserDetails(request, "poll-connection-requests", Online::API::SERVER_PATH); - const TransportAddress &addr = STKHost::get()->getPublicAddress(); + const TransportAddress &addr = NetworkConfig::get()->getMyAddress(); request->addParameter("address", addr.getIP() ); request->addParameter("port", addr.getPort()); @@ -408,7 +410,7 @@ void ServerLobbyRoomProtocol::connectionRequested(Event* event) uint32_t player_id = 0; player_id = data.getUInt32(1); // can we add the player ? - if (m_setup->getPlayerCount() < STKHost::getMaxPlayers()) //accept + if (m_setup->getPlayerCount() < NetworkConfig::get()->getMaxPlayers()) //accept { // add the player to the game setup m_next_id = m_setup->getPlayerCount(); diff --git a/src/network/protocols/start_game_protocol.cpp b/src/network/protocols/start_game_protocol.cpp index eefea582b..2281aa480 100644 --- a/src/network/protocols/start_game_protocol.cpp +++ b/src/network/protocols/start_game_protocol.cpp @@ -8,6 +8,7 @@ #include "modes/world.hpp" #include "network/event.hpp" #include "network/game_setup.hpp" +#include "network/network_config.hpp" #include "network/network_world.hpp" #include "network/protocol_manager.hpp" #include "network/protocols/synchronization_protocol.hpp" @@ -52,7 +53,7 @@ bool StartGameProtocol::notifyEventAsynchronous(Event* event) Log::error("StartGameProtocol", "Bad token received."); return true; } - if (STKHost::isServer() && ready) // on server, player is ready + if (NetworkConfig::get()->isServer() && ready) // on server, player is ready { Log::info("StartGameProtocol", "One of the players is ready."); m_player_states[peer->getPlayerProfile()] = READY; @@ -150,7 +151,7 @@ void StartGameProtocol::update() rki.setDifficulty(profile->difficulty); rki.setGlobalPlayerId(profile->race_id); // on the server, the race id must be the local one. - rki.setLocalPlayerId(STKHost::isServer() + rki.setLocalPlayerId(NetworkConfig::get()->isServer() ? profile->race_id : (is_me ? 0 : 1) ); rki.setHostId(profile->race_id); @@ -206,7 +207,7 @@ void StartGameProtocol::update() void StartGameProtocol::ready() // on clients, means the loading is finished { - if (!STKHost::isServer()) // if we're a client + if (!NetworkConfig::get()->isServer()) // if we're a client { assert(STKHost::get()->getPeerCount() == 1); NetworkString ns(5); diff --git a/src/network/protocols/stop_server.cpp b/src/network/protocols/stop_server.cpp index ea20d5c53..8699bb3f7 100644 --- a/src/network/protocols/stop_server.cpp +++ b/src/network/protocols/stop_server.cpp @@ -20,7 +20,7 @@ #include "config/player_manager.hpp" #include "config/user_config.hpp" -#include "network/stk_host.hpp" +#include "network/network_config.hpp" #include "online/request_manager.hpp" StopServer::StopServer() : Protocol(PROTOCOL_SILENT) @@ -45,7 +45,7 @@ void StopServer::asynchronousUpdate() { if (m_state == NONE) { - const TransportAddress& addr = STKHost::get()->getPublicAddress(); + const TransportAddress& addr = NetworkConfig::get()->getMyAddress(); m_request = new Online::XMLRequest(); PlayerManager::setUserDetails(m_request, "stop", Online::API::SERVER_PATH); diff --git a/src/network/protocols/synchronization_protocol.cpp b/src/network/protocols/synchronization_protocol.cpp index fd37464e2..e39cab771 100644 --- a/src/network/protocols/synchronization_protocol.cpp +++ b/src/network/protocols/synchronization_protocol.cpp @@ -1,6 +1,7 @@ #include "network/protocols/synchronization_protocol.hpp" #include "network/event.hpp" +#include "network/network_config.hpp" #include "network/protocols/kart_update_protocol.hpp" #include "network/protocols/controller_events_protocol.hpp" #include "network/protocols/game_events_protocol.hpp" @@ -52,7 +53,7 @@ bool SynchronizationProtocol::notifyEventAsynchronous(Event* event) const std::vector &peers = STKHost::get()->getPeers(); assert(peers.size() > 0); - if (STKHost::isServer()) + if (NetworkConfig::get()->isServer()) { if (talk_id > peers.size()) { @@ -83,7 +84,7 @@ bool SynchronizationProtocol::notifyEventAsynchronous(Event* event) Log::verbose("SynchronizationProtocol", "Answering sequence %u", sequence); // countdown time in the message - if (data.size() == 14 && !STKHost::isServer()) + if (data.size() == 14 && !NetworkConfig::get()->isServer()) { uint32_t time_to_start = data.gui32(10); Log::debug("SynchronizationProtocol", "Request to start game in %d.", time_to_start); @@ -163,7 +164,7 @@ void SynchronizationProtocol::asynchronousUpdate() NetworkString ns(10); ns.ai8(i).addUInt32(peers[i]->getClientServerToken()).addUInt8(1).addUInt32(m_pings[i].size()); // now add the countdown if necessary - if (m_countdown_activated && STKHost::isServer()) + if (m_countdown_activated && NetworkConfig::get()->isServer()) { ns.addUInt32((int)(m_countdown*1000.0)); Log::debug("SynchronizationProtocol", "CNTActivated: Countdown value : %f", m_countdown); diff --git a/src/network/race_config.cpp b/src/network/race_config.cpp index 8c51a4d0b..4ca991ccc 100644 --- a/src/network/race_config.cpp +++ b/src/network/race_config.cpp @@ -17,6 +17,7 @@ #include "network/race_config.hpp" +#include "network/network_config.hpp" #include "race/race_manager.hpp" #include "utils/log.hpp" @@ -200,7 +201,7 @@ uint8_t RaceVote::getLapsVote(uint8_t track_number) const RaceConfig::RaceConfig() { - m_max_players = 0; + m_max_players = NetworkConfig::get()->getMaxPlayers(); } //----------------------------------------------------------------------------- diff --git a/src/network/stk_host.cpp b/src/network/stk_host.cpp index 231da5149..504dd92a1 100644 --- a/src/network/stk_host.cpp +++ b/src/network/stk_host.cpp @@ -21,12 +21,15 @@ #include "config/user_config.hpp" #include "io/file_manager.hpp" #include "network/event.hpp" +#include "network/network_config.hpp" #include "network/network_console.hpp" #include "network/network_string.hpp" +#include "network/protocols/connect_to_peer.hpp" #include "network/protocols/connect_to_server.hpp" #include "network/protocols/server_lobby_room_protocol.hpp" #include "network/protocol_manager.hpp" #include "network/stk_peer.hpp" +#include "online/servers_manager.hpp" #include "utils/log.hpp" #include "utils/time.hpp" @@ -42,11 +45,27 @@ #include #include -STKHost *STKHost::m_stk_host = NULL; -int STKHost::m_max_players = 0; -bool STKHost::m_is_server = false; -STKHost::NetworkType STKHost::m_network_type = STKHost::NETWORK_NONE; +STKHost *STKHost::m_stk_host = NULL; +void STKHost::create() +{ + assert(m_stk_host == NULL); + if(NetworkConfig::get()->isServer()) + m_stk_host = new STKHost(NetworkConfig::get()->getServerName()); + else + { + Online::Server *server = + Online::ServersManager::get()->getJoinedServer(); + m_stk_host = new STKHost(server->getServerId(), 0); + } + if(!m_stk_host->m_network) + { + delete m_stk_host; + m_stk_host = NULL; + } +} // create + +// ============================================================================ /** \class STKHost * \brief Represents the local host. It is the main managing point for * networking. It is responsible for sending and receiving messages, @@ -135,7 +154,6 @@ STKHost::NetworkType STKHost::m_network_type = STKHost::NETWORK_NONE; */ STKHost::STKHost(uint32_t server_id, uint32_t host_id) { - m_is_server = false; init(); m_network = new Network(/*peer_count*/1, /*channel_limit*/2, @@ -156,15 +174,13 @@ STKHost::STKHost(uint32_t server_id, uint32_t host_id) */ STKHost::STKHost(const irr::core::stringw &server_name) { - m_is_server = true; init(); - m_server_name = server_name; ENetAddress addr; addr.host = STKHost::HOST_ANY; addr.port = 2758; - m_network= new Network(getMaxPlayers(), + m_network= new Network(NetworkConfig::get()->getMaxPlayers(), /*channel_limit*/2, /*max_in_bandwidth*/0, /*max_out_bandwidth*/ 0, &addr); @@ -192,10 +208,6 @@ void STKHost::init() m_is_registered = false; m_error_message = ""; - m_public_address.lock(); - m_public_address.getData().clear(); - m_public_address.unlock(); - pthread_mutex_init(&m_exit_mutex, NULL); // Start with initialising ENet @@ -241,16 +253,6 @@ STKHost::~STKHost() delete m_network; } // ~STKHost -//----------------------------------------------------------------------------- -/** Stores the public address of this host. - */ -void STKHost::setPublicAddress(const TransportAddress& addr) -{ - m_public_address.lock(); - m_public_address.getData().copy(addr); - m_public_address.unlock(); -} // setPublicAddress - //----------------------------------------------------------------------------- /** A previous GameSetup is deletea and a new one is created. * \return Newly create GameSetup object. @@ -260,7 +262,6 @@ GameSetup* STKHost::setupNewGame() if (m_game_setup) delete m_game_setup; m_game_setup = new GameSetup(); - m_game_setup->getRaceConfig()->setMaxPlayerCount(m_max_players); return m_game_setup; } // setupNewGame @@ -349,7 +350,7 @@ bool STKHost::connect(const TransportAddress& address) */ void STKHost::sendMessage(const NetworkString& data, bool reliable) { - if (m_is_server) + if (NetworkConfig::get()->isServer()) broadcastPacket(data, reliable); else { @@ -412,7 +413,8 @@ void* STKHost::mainLoop(void* self) STKHost* myself = (STKHost*)(self); ENetHost* host = myself->m_network->getENetHost(); - if(myself->isServer() && STKHost::isLAN()) + if(NetworkConfig::get()->isServer() && + NetworkConfig::get()->isLAN() ) { TransportAddress address(0, 2757); ENetAddress eaddr = address.toEnetAddress(); @@ -483,7 +485,9 @@ void STKHost::handleLANRequests() if (std::string(buffer, len) == "stk-server") { Log::verbose("STKHost", "Received LAN server query"); - std::string name = StringUtils::wide_to_utf8(getServerName().c_str()); + std::string name = + StringUtils::wide_to_utf8(NetworkConfig::get() + ->getServerName().c_str()); // Avoid buffer overflows if (name.size() > 255) name = name.substr(0, 255); @@ -495,12 +499,18 @@ void STKHost::handleLANRequests() NetworkString s; s.addUInt8((uint8_t)name.size()); s.addString(name.c_str()); - s.addUInt8(getMaxPlayers()); + s.addUInt8(NetworkConfig::get()->getMaxPlayers()); s.addUInt8(0); // FIXME: current number of connected players s.addUInt32(sender.getIP()); s.addUInt16(sender.getPort()); m_lan_network->sendRawPacket(s.getBytes(), s.size(), sender); } // if message is server-requested + else if (std::string(buffer, len) == "connection-request") + { + Protocol *c = new ConnectToPeer(0); + c->requestStart(); + } + } // handleLANRequests // ---------------------------------------------------------------------------- diff --git a/src/network/stk_host.hpp b/src/network/stk_host.hpp index 753d3f682..78ac4fb03 100644 --- a/src/network/stk_host.hpp +++ b/src/network/stk_host.hpp @@ -61,9 +61,6 @@ private: /** Singleton pointer to the instance. */ static STKHost* m_stk_host; - /** True if this host is a server, false otherwise. */ - static bool m_is_server; - /** ENet host interfacing sockets. */ Network* m_network; @@ -76,10 +73,6 @@ private: /** The list of peers connected to this instance. */ std::vector m_peers; - /** This computer's public IP address. With lock since it can - * be updated from a separate thread. */ - Synchronised m_public_address; - /** Stores data about the online game to play. */ GameSetup* m_game_setup; @@ -89,26 +82,14 @@ private: /** Mutex used to stop this thread. */ pthread_mutex_t m_exit_mutex; - /** Maximum number of players on the server. */ - static int m_max_players; - /** If this is a server, it indicates if this server is registered * with the stk server. */ bool m_is_registered; - /** If this is a server, the server name. */ - irr::core::stringw m_server_name; - /** An error message, which is set by a protocol to be displayed * in the GUI. */ irr::core::stringw m_error_message; - enum NetworkType - { NETWORK_NONE, NETWORK_WAN, NETWORK_LAN }; - - /** Keeps the type of network connection: none (yet), LAN or WAN. */ - static NetworkType m_network_type; - STKHost(uint32_t server_id, uint32_t host_id); STKHost(const irr::core::stringw &server_name); virtual ~STKHost(); @@ -117,31 +98,11 @@ private: public: - /** Creates the singleton for a client. In case of an error - * m_stk_host is NULL (which can be tested using isNetworking(). */ - static void create(uint32_t server_id, uint32_t host_id) - { - assert(m_stk_host == NULL); - m_stk_host = new STKHost(server_id, host_id); - if(!m_stk_host->m_network) - { - delete m_stk_host; - m_stk_host = NULL; - } - } // create - // ------------------------------------------------------------------------ - /** Creates the singleton for a server. In case of an error - * m_stk_host is NULL (which can be tested using isNetworking(). */ - static void create(const irr::core::stringw &server_name) - { - assert(m_stk_host == NULL); - m_stk_host = new STKHost(server_name); - if(!m_stk_host->m_network) - { - delete m_stk_host; - m_stk_host = NULL; - } - } // create + /** Creates the STKHost. It takes all confifguration parameters from + * NetworkConfig. This STKHost can either be a client or a server. + */ + static void create(); + // ------------------------------------------------------------------------ /** Returns the instance of STKHost. */ static STKHost *get() @@ -155,27 +116,15 @@ public: assert(m_stk_host != NULL); delete m_stk_host; m_stk_host = NULL; - } + } // destroy // ------------------------------------------------------------------------ - /** Return if a network setting is happening. A network setting is active - * if a host (server or client) exists. */ - static bool isNetworking() { return m_stk_host!=NULL; } - // ------------------------------------------------------------------------ - /** Return true if it's a networked game with a LAN server. */ - static bool isLAN() { return m_network_type == NETWORK_LAN; } - // ------------------------------------------------------------------------ - /** Return true if it's a networked game but with a WAN server. */ - static bool isWAN() { return m_network_type == NETWORK_WAN; } - // ------------------------------------------------------------------------ - static void setIsLAN() { m_network_type = NETWORK_LAN; } - // ------------------------------------------------------------------------ - static void setIsWAN() { m_network_type = NETWORK_WAN; } + /** Checks if the STKHost has been created. */ + static bool existHost() { return m_stk_host != NULL; } // ------------------------------------------------------------------------ static void* mainLoop(void* self); virtual GameSetup* setupNewGame(); - void setPublicAddress(const TransportAddress& addr); void abort(); void deleteAllPeers(); void reset(); @@ -231,26 +180,6 @@ public: return m_network->getENetHost()->address.host; } // getAddress - // -------------------------------------------------------------------- - /** Returns the public IP address (thread safe). The network manager - * is a friend of TransportAddress and so has access to the copy - * constructor, which is otherwise declared private. */ - const TransportAddress getPublicAddress() - { - m_public_address.lock(); - TransportAddress a; - a.copy(m_public_address.getData()); - m_public_address.unlock(); - return a; - } // getPublicAddress - - // -------------------------------------------------------------------- - /** Sets the maximum number of players for this server. */ - static void setMaxPlayers(int n) { m_max_players = n; } - - // -------------------------------------------------------------------- - /** Returns the maximum number of players for this server. */ - static int getMaxPlayers() { return m_max_players; } // -------------------------------------------------------------------- /** Returns a const reference to the list of peers. */ @@ -260,30 +189,15 @@ public: /** Returns the number of currently connected peers. */ unsigned int getPeerCount() { return (int)m_peers.size(); } // -------------------------------------------------------------------- - /** Returns if this instance is a server. */ - static bool isServer() { return m_is_server; } - // -------------------------------------------------------------------- - /** Returns if this instance is a client. */ - static bool isclient() { return !m_is_server; } - // -------------------------------------------------------------------- - /** Returns the server name. */ - const irr::core::stringw& getServerName() const - { - assert(isServer()); - return m_server_name; - } - // -------------------------------------------------------------------- /** Sets if this server is registered with the stk server. */ void setRegistered(bool registered) { - assert(isServer()); m_is_registered = registered; } // setRegistered // -------------------------------------------------------------------- /** Returns if this server is registered with the stk server. */ bool isRegistered() const { - assert(isServer()); return m_is_registered; } // isRegistered diff --git a/src/network/stk_peer.hpp b/src/network/stk_peer.hpp index 26636b760..6a71f2de8 100644 --- a/src/network/stk_peer.hpp +++ b/src/network/stk_peer.hpp @@ -30,7 +30,6 @@ class NetworkPlayerProfile; class NetworkString; -class STKHost; class TransportAddress; /*! \class STKPeer diff --git a/src/network/transport_address.hpp b/src/network/transport_address.hpp index 809d49760..36b8902d2 100644 --- a/src/network/transport_address.hpp +++ b/src/network/transport_address.hpp @@ -61,10 +61,10 @@ public: ~TransportAddress() {} // ------------------------------------------------------------------------ private: - friend class STKHost; + friend class NetworkConfig; /** The copy constructor is private, so that the friend class - * NetworkManager can access it to create a copy, but no other - * class can. */ + * NetworkConfig can access it to create a copy (getMyAddress), but + * no other class can. */ TransportAddress(const TransportAddress &other) { copy(other); diff --git a/src/online/server.hpp b/src/online/server.hpp index db8e0118f..b1b4943e5 100644 --- a/src/online/server.hpp +++ b/src/online/server.hpp @@ -91,7 +91,10 @@ public: int current_players, const TransportAddress &address); bool filterByWords(const irr::core::stringw words) const; // ------------------------------------------------------------------------ - /** Sets the sort order used in the comparison function. It is static, so + /** Returns ip address and port of this server. */ + const TransportAddress& getAddress() const { return m_address; } + // ------------------------------------------------------------------------ + /** Sets the sort order used in the comparison function. It is static, so * that each instance can access the sort order. */ static void setSortOrder(SortOrder so) { m_sort_order = so; } diff --git a/src/online/servers_manager.cpp b/src/online/servers_manager.cpp index cf08d405a..9366c30bc 100644 --- a/src/online/servers_manager.cpp +++ b/src/online/servers_manager.cpp @@ -18,7 +18,9 @@ #include "online/servers_manager.hpp" #include "config/user_config.hpp" -#include "network/stk_host.hpp" +#include "network/network.hpp" +#include "network/network_config.hpp" +#include "network/network_string.hpp" #include "utils/translation.hpp" #include "utils/time.hpp" @@ -183,6 +185,8 @@ XMLRequest* ServersManager::getLANRefreshRequest() const ->addServer(new Server(name_w, /*lan*/true, max_players, players, sender) ); + TransportAddress me(my_ip, my_port); + NetworkConfig::get()->setMyAddress(me); m_success = true; } // if received_data } // while still waiting @@ -222,8 +226,8 @@ XMLRequest* ServersManager::getRefreshRequest(bool request_now) } cleanUpServers(); - XMLRequest *request = STKHost::isWAN() ? getWANRefreshRequest() - : getLANRefreshRequest(); + XMLRequest *request = NetworkConfig::get()->isWAN() ? getWANRefreshRequest() + : getLANRefreshRequest(); if (request_now) RequestManager::get()->addRequest(request); diff --git a/src/race/race_manager.cpp b/src/race/race_manager.cpp index f2414528a..d10b51b2a 100644 --- a/src/race/race_manager.cpp +++ b/src/race/race_manager.cpp @@ -44,9 +44,9 @@ #include "modes/three_strikes_battle.hpp" #include "modes/soccer_world.hpp" #include "network/protocol_manager.hpp" +#include "network/network_config.hpp" #include "network/network_world.hpp" #include "network/protocols/start_game_protocol.hpp" -#include "network/stk_host.hpp" #include "states_screens/grand_prix_cutscene.hpp" #include "states_screens/grand_prix_lose.hpp" #include "states_screens/grand_prix_win.hpp" @@ -512,7 +512,7 @@ void RaceManager::startNextRace() m_kart_status[i].m_last_time = 0; } - if(STKHost::isNetworking()) + if(NetworkConfig::get()->isNetworking()) { StartGameProtocol* protocol = static_cast( ProtocolManager::getInstance()->getProtocol(PROTOCOL_START_GAME)); diff --git a/src/states_screens/create_server_screen.cpp b/src/states_screens/create_server_screen.cpp index 3d194e58b..2cfd38a98 100644 --- a/src/states_screens/create_server_screen.cpp +++ b/src/states_screens/create_server_screen.cpp @@ -23,6 +23,7 @@ #include "challenges/unlock_manager.hpp" #include "config/player_manager.hpp" #include "modes/demo_world.hpp" +#include "network/network_config.hpp" #include "network/stk_host.hpp" #include "online/servers_manager.hpp" #include "states_screens/online_screen.hpp" @@ -80,13 +81,15 @@ void CreateServerScreen::init() m_info_widget->setText("", false); LabelWidget *title = getWidget("title"); - title->setText(STKHost::isLAN() ? _("Create LAN Server") - : _("Create Server") , false); + title->setText(NetworkConfig::get()->isLAN() ? _("Create LAN Server") + : _("Create Server") , + false); // I18n: Name of the server. %s is either the online or local user name m_name_widget->setText(_("%s's server", - STKHost::isLAN() ? PlayerManager::getCurrentPlayer()->getName() - : PlayerManager::getCurrentOnlineUserName() + NetworkConfig::get()->isLAN() + ? PlayerManager::getCurrentPlayer()->getName() + : PlayerManager::getCurrentOnlineUserName() ) ); } // init @@ -119,7 +122,7 @@ void CreateServerScreen::eventCallback(Widget* widget, const std::string& name, void CreateServerScreen::onUpdate(float delta) { // If no host has been created, keep on waiting. - if(!STKHost::isNetworking()) + if(!STKHost::existHost()) return; // First check if an error happened while registering the server: @@ -175,7 +178,7 @@ void CreateServerScreen::createServer() } // In case of a LAN game, we can create the new server object now - if (STKHost::isLAN()) + if (NetworkConfig::get()->isLAN()) { // FIXME Is this actually necessary?? Only in case of WAN, or LAN and WAN? TransportAddress address(0x7f000001,0); // 127.0.0.1 @@ -187,8 +190,9 @@ void CreateServerScreen::createServer() // In case of a WAN game, we register this server with the // stk server, and will get the server's id when this // request is finished. - STKHost::setMaxPlayers(max_players); - STKHost::create(name); + NetworkConfig::get()->setMaxPlayers(max_players); + NetworkConfig::get()->setServerName(name); + STKHost::create(); } // createServer diff --git a/src/states_screens/dialogs/server_info_dialog.cpp b/src/states_screens/dialogs/server_info_dialog.cpp index b47f281fa..68be81871 100644 --- a/src/states_screens/dialogs/server_info_dialog.cpp +++ b/src/states_screens/dialogs/server_info_dialog.cpp @@ -91,7 +91,7 @@ void ServerInfoDialog::requestJoin() //m_server_join_request = Online::CurrentUser::get()->requestServerJoin(m_server_id); Online::ServersManager::get()->setJoinedServer(m_server_id); - STKHost::create(m_server_id, m_host_id); + STKHost::create(); ModalDialog::dismiss(); NetworkingLobby::getInstance()->push(); //Online::CurrentUser::release(); diff --git a/src/states_screens/online_screen.cpp b/src/states_screens/online_screen.cpp index 062ec37c1..45108db3c 100644 --- a/src/states_screens/online_screen.cpp +++ b/src/states_screens/online_screen.cpp @@ -27,10 +27,10 @@ #include "input/input_manager.hpp" #include "io/file_manager.hpp" #include "modes/demo_world.hpp" +#include "network/network_config.hpp" #include "network/protocol_manager.hpp" #include "network/protocols/connect_to_server.hpp" #include "network/protocols/request_connection.hpp" -#include "network/stk_host.hpp" #include "online/profile_manager.hpp" #include "online/request.hpp" #include "online/servers_manager.hpp" @@ -248,13 +248,15 @@ void OnlineScreen::eventCallback(Widget* widget, const std::string& name, } else if (selection == m_create_lan_server_widget->m_properties[PROP_ID]) { - STKHost::setIsLAN(); + NetworkConfig::get()->setIsLAN(); + NetworkConfig::get()->setIsServer(true); CreateServerScreen::getInstance()->push(); // TODO: create lan server } else if (selection == m_find_lan_server_widget->m_properties[PROP_ID]) { - STKHost::setIsLAN(); + NetworkConfig::get()->setIsLAN(); + NetworkConfig::get()->setIsServer(false); ServerSelection::getInstance()->push(); } else if (selection == m_manage_user->m_properties[PROP_ID]) @@ -268,12 +270,14 @@ void OnlineScreen::eventCallback(Widget* widget, const std::string& name, } else if (selection == m_find_wan_server_widget->m_properties[PROP_ID]) { - STKHost::setIsWAN(); + NetworkConfig::get()->setIsWAN(); + NetworkConfig::get()->setIsServer(false); ServerSelection::getInstance()->push(); } else if (selection == m_create_wan_server_widget->m_properties[PROP_ID]) { - STKHost::setIsWAN(); + NetworkConfig::get()->setIsWAN(); + NetworkConfig::get()->setIsServer(true); CreateServerScreen::getInstance()->push(); } else if (selection == m_quick_wan_play_widget->m_properties[PROP_ID])