From 4c2fe75cb48d37be38370ffc34dc4859a933a068 Mon Sep 17 00:00:00 2001 From: Benau Date: Sat, 9 Mar 2019 15:14:13 +0800 Subject: [PATCH] Add network capabilities to handle same server version changes --- src/network/network_config.cpp | 1 + src/network/network_config.hpp | 13 +++++++++++++ src/network/protocols/client_lobby.cpp | 16 +++++++++++++++- src/network/protocols/server_lobby.cpp | 17 ++++++++++++++++- src/network/stk_peer.hpp | 10 ++++++++++ 5 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/network/network_config.cpp b/src/network/network_config.cpp index cf01be7de..3d562f58e 100644 --- a/src/network/network_config.cpp +++ b/src/network/network_config.cpp @@ -67,6 +67,7 @@ NetworkConfig::NetworkConfig() */ void NetworkConfig::unsetNetworking() { + clearServerCapabilities(); m_network_type = NETWORK_NONE; ServerConfig::m_private_server_password = ""; } // unsetNetworking diff --git a/src/network/network_config.hpp b/src/network/network_config.hpp index b2cf7cf2a..7583fd6b2 100644 --- a/src/network/network_config.hpp +++ b/src/network/network_config.hpp @@ -93,6 +93,10 @@ private: /** Set by client or server which is required to be the same. */ int m_state_frequency; + /** List of server capabilities set when joining it, to determine features + * available in same version. */ + std::vector m_server_capabilities; + public: /** Singleton get, which creates this object if necessary. */ static NetworkConfig *get() @@ -231,6 +235,15 @@ public: int getStateFrequency() const { return m_state_frequency; } // ------------------------------------------------------------------------ bool roundValuesNow() const; + // ------------------------------------------------------------------------ + void setServerCapabilities(std::vector& caps) + { m_server_capabilities = std::move(caps); } + // ------------------------------------------------------------------------ + void clearServerCapabilities() { m_server_capabilities.clear(); } + // ------------------------------------------------------------------------ + const std::vector& getServerCapabilities() const + { return m_server_capabilities; } + }; // class NetworkConfig #endif // HEADER_NETWORK_CONFIG diff --git a/src/network/protocols/client_lobby.cpp b/src/network/protocols/client_lobby.cpp index b84d14270..5b27b4460 100644 --- a/src/network/protocols/client_lobby.cpp +++ b/src/network/protocols/client_lobby.cpp @@ -332,10 +332,13 @@ void ClientLobby::update(int ticks) break; case LINKED: { + NetworkConfig::get()->clearServerCapabilities(); NetworkString* ns = getNetworkString(); ns->addUInt8(LE_CONNECTION_REQUESTED) .addUInt32(ServerConfig::m_server_version) - .encodeString(StringUtils::getUserAgentString()); + .encodeString(StringUtils::getUserAgentString()) + // Reserved for future to supply list of network capabilities + .addUInt16(0); auto all_k = kart_properties_manager->getAllAvailableKarts(); auto all_t = track_manager->getAllTrackIdentifiers(); @@ -578,6 +581,17 @@ void ClientLobby::connectionAccepted(Event* event) assert(server_version != 0); m_auto_started = false; m_state.store(CONNECTED); + + unsigned list_caps = data.getUInt16(); + std::vector caps; + for (unsigned i = 0; i < list_caps; i++) + { + std::string cap; + data.decodeString(&cap); + caps.push_back(cap); + } + NetworkConfig::get()->setServerCapabilities(caps); + float auto_start_timer = data.getFloat(); int state_frequency_in_server = data.getUInt32(); NetworkConfig::get()->setStateFrequency(state_frequency_in_server); diff --git a/src/network/protocols/server_lobby.cpp b/src/network/protocols/server_lobby.cpp index 17b380f3e..e89b02801 100644 --- a/src/network/protocols/server_lobby.cpp +++ b/src/network/protocols/server_lobby.cpp @@ -2011,6 +2011,16 @@ void ServerLobby::connectionRequested(Event* event) data.decodeString(&user_version); event->getPeer()->setUserVersion(user_version); + unsigned list_caps = data.getUInt16(); + std::vector caps; + for (unsigned i = 0; i < list_caps; i++) + { + std::string cap; + data.decodeString(&cap); + caps.push_back(cap); + } + event->getPeer()->setClientCapabilities(caps); + std::set client_karts, client_tracks; const unsigned kart_num = data.getUInt16(); const unsigned track_num = data.getUInt16(); @@ -2267,7 +2277,12 @@ void ServerLobby::handleUnencryptedConnection(std::shared_ptr peer, (m_timeout.load() - (int64_t)StkTime::getRealTimeMs()) / 1000.0f; } message_ack->addUInt8(LE_CONNECTION_ACCEPTED).addUInt32(peer->getHostId()) - .addUInt32(ServerConfig::m_server_version).addFloat(auto_start_timer) + .addUInt32(ServerConfig::m_server_version); + + // Reserved for future to supply list of network capabilities + message_ack->addUInt16(0); + + message_ack->addFloat(auto_start_timer) .addUInt32(ServerConfig::m_state_frequency); peer->setSpectator(false); diff --git a/src/network/stk_peer.hpp b/src/network/stk_peer.hpp index d59366bfb..e2caa5357 100644 --- a/src/network/stk_peer.hpp +++ b/src/network/stk_peer.hpp @@ -100,6 +100,10 @@ protected: std::string m_user_version; + /** List of client capabilities set when connecting it, to determine + * features available in same version. */ + std::vector m_client_capabilities; + public: STKPeer(ENetPeer *enet_peer, STKHost* host, uint32_t host_id); // ------------------------------------------------------------------------ @@ -231,6 +235,12 @@ public: return 0; return (int)(diff / 1000); } + // ------------------------------------------------------------------------ + void setClientCapabilities(std::vector& caps) + { m_client_capabilities = std::move(caps); } + // ------------------------------------------------------------------------ + const std::vector& getClientCapabilities() const + { return m_client_capabilities; } }; // STKPeer #endif // STK_PEER_HPP