diff options
author | SmallJoker <SmallJoker@users.noreply.github.com> | 2019-08-24 19:07:38 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-24 19:07:38 +0200 |
commit | 0b4f424f414380b7a46270e1389e8aa0524d8fba (patch) | |
tree | 5731cbe72bb3178d697b3cda43b838f26ba34f7d /src/server.cpp | |
parent | 008b80fe1ca9d46610b8971e2ac92fb56da8e69f (diff) | |
download | minetest-0b4f424f414380b7a46270e1389e8aa0524d8fba.tar.gz minetest-0b4f424f414380b7a46270e1389e8aa0524d8fba.tar.bz2 minetest-0b4f424f414380b7a46270e1389e8aa0524d8fba.zip |
Inventory: Send dirty lists where appropriate (#8742)
This change reduces the amount of sent data towards clients. Inventory lists that are already known to the player are skipped, saving quite some data over time.
Raises protocol version to 38 to ensure correct backwards-compatible code.
Diffstat (limited to 'src/server.cpp')
-rw-r--r-- | src/server.cpp | 39 |
1 files changed, 27 insertions, 12 deletions
diff --git a/src/server.cpp b/src/server.cpp index 871612e60..59bc12581 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1057,7 +1057,7 @@ PlayerSAO* Server::StageTwoClientInit(session_t peer_id) SendPlayerInventoryFormspec(peer_id); // Send inventory - SendInventory(playersao); + SendInventory(playersao, false); // Send HP or death screen if (playersao->isDead()) @@ -1241,19 +1241,22 @@ void Server::setInventoryModified(const InventoryLocation &loc, bool playerSend) break; case InventoryLocation::PLAYER: { - if (!playerSend) - return; RemotePlayer *player = m_env->getPlayer(loc.name.c_str()); if (!player) return; + player->setModified(true); + + if (!playerSend) + return; + PlayerSAO *playersao = player->getPlayerSAO(); if(!playersao) return; - SendInventory(playersao); + SendInventory(playersao, true); } break; case InventoryLocation::NODEMETA: @@ -1527,21 +1530,27 @@ void Server::SendNodeDef(session_t peer_id, Non-static send methods */ -void Server::SendInventory(PlayerSAO* playerSAO) +void Server::SendInventory(PlayerSAO *sao, bool incremental) { - UpdateCrafting(playerSAO->getPlayer()); + RemotePlayer *player = sao->getPlayer(); + + // Do not send new format to old clients + incremental &= player->protocol_version >= 38; + + UpdateCrafting(player); /* Serialize it */ - NetworkPacket pkt(TOCLIENT_INVENTORY, 0, playerSAO->getPeerID()); - - std::ostringstream os; - playerSAO->getInventory()->serialize(os); + NetworkPacket pkt(TOCLIENT_INVENTORY, 0, sao->getPeerID()); - std::string s = os.str(); + std::ostringstream os(std::ios::binary); + sao->getInventory()->serialize(os, incremental); + sao->getInventory()->setModified(false); + player->setModified(true); + const std::string &s = os.str(); pkt.putRawString(s.c_str(), s.size()); Send(&pkt); } @@ -2595,8 +2604,9 @@ void Server::sendDetachedInventory(const std::string &name, session_t peer_id) // Serialization & NetworkPacket isn't a love story std::ostringstream os(std::ios_base::binary); inv_it->second->serialize(os); + inv_it->second->setModified(false); - std::string os_str = os.str(); + const std::string &os_str = os.str(); pkt << static_cast<u16>(os_str.size()); // HACK: to keep compatibility with 5.0.0 clients pkt.putRawString(os_str); } @@ -2822,6 +2832,11 @@ void Server::UpdateCrafting(RemotePlayer *player) if (!clist || clist->getSize() == 0) return; + if (!clist->checkModified()) { + verbosestream << "Skip Server::UpdateCrafting(): list unmodified" << std::endl; + return; + } + // Get a preview for crafting ItemStack preview; InventoryLocation loc; |