From 0b62d2f82b395811938e21d791aa34ddf4a715fb Mon Sep 17 00:00:00 2001 From: hiker Date: Mon, 2 Nov 2015 08:11:51 +1100 Subject: [PATCH] Created two different constructors for STKHost - one for server, one for client. --- src/main.cpp | 8 +- src/network/stk_host.cpp | 82 +++++++++++---------- src/network/stk_host.hpp | 21 ++++-- src/states_screens/create_server_screen.cpp | 9 +-- src/states_screens/create_server_screen.hpp | 2 +- 5 files changed, 65 insertions(+), 57 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index ef23fe7f0..5a77881ae 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -531,7 +531,7 @@ void cmdLineHelp() // " --history=n Replay history file 'history.dat' using:\n" // " n=1: recorded positions\n" // " n=2: recorded key strokes\n" - " --server Start a server (not a playing client).\n" + " --server=name Start a server (not a playing client).\n" " --login=s Automatically log in (set the login).\n" " --password=s Automatically log in (set the password).\n" " --port=n Port number to use.\n" @@ -773,15 +773,15 @@ int handleCmdLine() } // Networking command lines - if(CommandLine::has("--server") ) + if(CommandLine::has("--server", &s) ) { STKHost::setMaxPlayers(UserConfigParams::m_server_max_players); - STKHost::create(/*is_Server*/true); + STKHost::create(core::stringw(s.c_str())); Log::info("main", "Creating a server."); } else if(CommandLine::has("--client")) { - STKHost::create(/*is_server*/false); + STKHost::create(); Log::info("main", "Creating a client."); } diff --git a/src/network/stk_host.cpp b/src/network/stk_host.cpp index 24ed49eb7..a2690e609 100644 --- a/src/network/stk_host.cpp +++ b/src/network/stk_host.cpp @@ -46,10 +46,51 @@ bool STKHost::m_is_server = false; STKHost::NetworkType STKHost::m_network_type = STKHost::NETWORK_NONE; // ============================================================================ -/** Constructor that just initialises this object (esp. opening the packet - * log file), but it does not start a listener thread. +/** Constructor for a client */ STKHost::STKHost() +{ + m_is_server = false; + init(); +} // STKHost + +// ---------------------------------------------------------------------------- +/** The constructor for a server. + * \param server_name Name of the server to be registered with the STK server. + */ +STKHost::STKHost(const irr::core::stringw &server_name) +{ + m_is_server = true; + init(); + + // The server control flow starts with the ServerLobbyRoomProtocol. + // This will in turn spawn more protocols: + // GetPublicAddress: Use STUN to discover the public ip address + // and port number for this host. + ENetAddress addr; + addr.host = STKHost::HOST_ANY; + addr.port = 2758; + + m_network= new Network(getMaxPlayers(), + /*channel_limit*/2, + /*max_in_bandwidth*/0, + /*max_out_bandwidth*/ 0, &addr); + if (!m_network) + { + Log::fatal("STKHost", "An error occurred while trying to create an ENet" + " server host."); + } + + startListening(); + ProtocolManager::getInstance()->requestStart(new ServerLobbyRoomProtocol()); + +} // STKHost(server_name) + +// ---------------------------------------------------------------------------- +/** Initialises the internal data structures and starts the protocol manager + * and the debug console. + */ +void STKHost::init() { m_network = NULL; m_listening_thread = NULL; @@ -77,16 +118,6 @@ STKHost::STKHost() m_network_console = new NetworkConsole(); m_network_console->run(); - if (m_is_server) - { - // The server control flow starts with the ServerLobbyRoomProtocol. - // This will in turn spawn more protocols: - // GetPublicAddress: Use STUN to discover the public ip address - // and port number for this host. - setupServer(STKHost::HOST_ANY, 7321, 16, 2, 0, 0); - startListening(); - ProtocolManager::getInstance()->requestStart(new ServerLobbyRoomProtocol()); - } } // STKHost // ---------------------------------------------------------------------------- @@ -313,33 +344,6 @@ void* STKHost::mainLoop(void* self) return NULL; } // mainLoop -// ---------------------------------------------------------------------------- -/** \brief Setup this host as a server. - * \param address : The IPv4 address of incoming connections. - * \param port : The port on which the server listens. - * \param peer_count : The maximum number of peers. - * \param channel_limit : The maximum number of channels per peer. - * \param max_incoming_bandwidth : The maximum incoming bandwidth. - * \param max_outgoing_bandwidth : The maximum outgoing bandwidth. - */ -void STKHost::setupServer(uint32_t address, uint16_t port, int peer_count, - int channel_limit, uint32_t max_incoming_bandwidth, - uint32_t max_outgoing_bandwidth) -{ - ENetAddress* addr = (ENetAddress*)(malloc(sizeof(ENetAddress))); - addr->host = address; - addr->port = port; - - m_network= new Network(peer_count, channel_limit, - max_incoming_bandwidth, - max_outgoing_bandwidth, addr); - if (!m_network) - { - Log::fatal("STKHost", "An error occurred while trying to create an ENet" - " server host."); - } -} // setupServer - // ---------------------------------------------------------------------------- /** \brief Setups the host as a client. * In fact there is only one peer connected to this host. diff --git a/src/network/stk_host.hpp b/src/network/stk_host.hpp index 07fe9b6a6..1146e45a5 100644 --- a/src/network/stk_host.hpp +++ b/src/network/stk_host.hpp @@ -105,19 +105,28 @@ private: 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(); + STKHost(const irr::core::stringw &server_name); virtual ~STKHost(); + void init(); public: - /** Creates the singleton. */ - static void create(bool is_server) + /** Creates the singleton for a client. */ + static void create() { - m_is_server = is_server; assert(m_stk_host == NULL); - m_stk_host = new STKHost(); + m_stk_host = new STKHost(); + } + // ------------------------------------------------------------------------ + /** Creates the singleton for a server. */ + static void create(const irr::core::stringw &server_name) + { + assert(m_stk_host == NULL); + m_stk_host = new STKHost(server_name); } // create // ------------------------------------------------------------------------ /** Returns the instance of STKHost. */ @@ -162,10 +171,6 @@ public: void sendPacketExcept(STKPeer* peer, const NetworkString& data, bool reliable = true); - void setupServer(uint32_t address, uint16_t port, - int peer_count, int channel_limit, - uint32_t max_incoming_bandwidth, - uint32_t max_outgoing_bandwidth); void setupClient(int peer_count, int channel_limit, uint32_t max_incoming_bandwidth, uint32_t max_outgoing_bandwidth); diff --git a/src/states_screens/create_server_screen.cpp b/src/states_screens/create_server_screen.cpp index 0e1b3da4c..9f25136d1 100644 --- a/src/states_screens/create_server_screen.cpp +++ b/src/states_screens/create_server_screen.cpp @@ -108,7 +108,7 @@ void CreateServerScreen::eventCallback(Widget* widget, const std::string& name, } else if (selection == m_create_widget->m_properties[PROP_ID]) { - serverCreationRequest(); + createServer(); } // is create_widget } } // eventCallback @@ -152,7 +152,7 @@ void CreateServerScreen::onUpdate(float delta) /** In case of WAN it adds the server to the list of servers. In case of LAN * networking, it registers this game server with the stk server. */ -void CreateServerScreen::serverCreationRequest() +void CreateServerScreen::createServer() { const irr::core::stringw name = m_name_widget->getText().trim(); const int max_players = m_max_players_widget->getValue(); @@ -183,8 +183,7 @@ void CreateServerScreen::serverCreationRequest() } STKHost::setMaxPlayers(max_players); - STKHost::create(/*is_server*/true); - STKHost::get()->setServerName(name); + STKHost::create(name); // Now must be WAN: forward request to the stk server m_server_creation_request = new ServerCreationRequest(); @@ -194,7 +193,7 @@ void CreateServerScreen::serverCreationRequest() m_server_creation_request->addParameter("max_players", max_players); m_server_creation_request->queue(); -} // serverCreationRequest +} // createServer // ---------------------------------------------------------------------------- /** Callbacks from the online create server request. diff --git a/src/states_screens/create_server_screen.hpp b/src/states_screens/create_server_screen.hpp index d42469dbc..3d0f4afa6 100644 --- a/src/states_screens/create_server_screen.hpp +++ b/src/states_screens/create_server_screen.hpp @@ -63,7 +63,7 @@ private: ServerCreationRequest *m_server_creation_request; - void serverCreationRequest(); + void createServer(); public: