diff --git a/src/client/client.h b/src/client/client.h index 9dc2df50c..9e642951c 100644 --- a/src/client/client.h +++ b/src/client/client.h @@ -140,7 +140,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef unsigned int overload = 0; void handleCommand_FreeminerInit(NetworkPacket *pkt); - void handleCommand_BlockDatas(NetworkPacket *pkt); + void handleCommand_BlockDataFm(NetworkPacket *pkt); void sendInitFm(); void sendDrawControl(); void sendGetBlocks(); diff --git a/src/client/fm_client.cpp b/src/client/fm_client.cpp index 1884dd241..8d68676d1 100644 --- a/src/client/fm_client.cpp +++ b/src/client/fm_client.cpp @@ -162,7 +162,7 @@ void Client::createFarMesh(MapBlockP &block) } } -void Client::handleCommand_BlockDatas(NetworkPacket *pkt) +void Client::handleCommand_BlockDataFm(NetworkPacket *pkt) { const auto str = std::string{pkt->getString(0), pkt->getSize()}; if (!pkt->packet_unpack()) { diff --git a/src/fm_clientiface.cpp b/src/fm_clientiface.cpp index 5990a2298..1b2bd939c 100644 --- a/src/fm_clientiface.cpp +++ b/src/fm_clientiface.cpp @@ -565,6 +565,8 @@ int RemoteClient::GetNextBlocksFm(ServerEnvironment *env, EmergeManager *emerge, TRY_UNIQUE_LOCK(far_blocks_requested_mutex) { std::multimap ordered; + constexpr uint16_t send_max = 50; + uint16_t sent_cnt = 0; for (auto &far_blocks : far_blocks_requested) { for (auto &[bpos, step_sent] : far_blocks) { auto &[step, sent_ts] = step_sent; @@ -585,6 +587,10 @@ int RemoteClient::GetNextBlocksFm(ServerEnvironment *env, EmergeManager *emerge, block->far_step = step; step_sent.second = 0; ordered.emplace(sent_ts - step, block); + + if (++sent_cnt > send_max) { + break; + } } } for (const auto &[key, block] : std::views::reverse(ordered)) { diff --git a/src/fm_server.cpp b/src/fm_server.cpp index 3ce15fd3d..b5ea78944 100644 --- a/src/fm_server.cpp +++ b/src/fm_server.cpp @@ -505,8 +505,8 @@ void Server::handleCommand_GetBlocks(NetworkPacket *pkt) { ServerMap::far_blocks_req_t blocks; packet[TOSERVER_GET_BLOCKS_BLOCKS].convert(blocks); - for (const auto &[bpos, step_ts] : blocks) { - const auto &[step, ts] = step_ts; + for (const auto &[bpos, step_iteration] : blocks) { + const auto &[step, iteation] = step_iteration; if (step >= FARMESH_STEP_MAX - 1) { continue; } @@ -514,7 +514,7 @@ void Server::handleCommand_GetBlocks(NetworkPacket *pkt) client->far_blocks_requested.resize(step); } client->far_blocks_requested[step][bpos].first = step; - client->far_blocks_requested[step][bpos].second = ts; + client->far_blocks_requested[step][bpos].second = iteation; } } } @@ -601,7 +601,7 @@ void Server::SendBlockFm(session_t peer_id, MapBlockP block, u8 ver, g_profiler->add("Connection: blocks sent", 1); - MSGPACK_PACKET_INIT((int)TOCLIENT_BLOCKDATAS, 8); + MSGPACK_PACKET_INIT((int)TOCLIENT_BLOCKDATA_FM, 8); PACK(TOCLIENT_BLOCKDATA_POS, block->getPos()); std::ostringstream os(std::ios_base::binary); @@ -618,7 +618,7 @@ void Server::SendBlockFm(session_t peer_id, MapBlockP block, u8 ver, PACK(TOCLIENT_BLOCKDATA_CONTENT_ONLY_PARAM1, block->content_only_param1); PACK(TOCLIENT_BLOCKDATA_CONTENT_ONLY_PARAM2, block->content_only_param2); - NetworkPacket pkt(TOCLIENT_BLOCKDATAS, buffer.size(), peer_id); + NetworkPacket pkt(TOCLIENT_BLOCKDATA_FM, buffer.size(), peer_id); pkt.putLongString({buffer.data(), buffer.size()}); auto s = std::string{pkt.getString(0), pkt.getSize()}; Send(&pkt); diff --git a/src/network/clientopcodes.cpp b/src/network/clientopcodes.cpp index 2b884f10c..1db350b02 100644 --- a/src/network/clientopcodes.cpp +++ b/src/network/clientopcodes.cpp @@ -42,7 +42,7 @@ const ToClientCommandHandler toClientCommandTable[TOCLIENT_NUM_MSG_TYPES] = null_command_handler, // 0x0F null_command_handler, // 0x10 { "TOCLIENT_PUNCH_PLAYER", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_PunchPlayer }, // 0x11 - { "TOCLIENT_BLOCKDATAS", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_BlockDatas }, // 0x12 + { "TOCLIENT_BLOCKDATA_FM", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_BlockDataFm }, // 0x12 null_command_handler, null_command_handler, null_command_handler, diff --git a/src/network/fm_networkprotocol.h b/src/network/fm_networkprotocol.h index bd78c803b..ec59b229b 100644 --- a/src/network/fm_networkprotocol.h +++ b/src/network/fm_networkprotocol.h @@ -27,7 +27,7 @@ enum TOCLIENT_INIT_GAMEID, }; -#define TOCLIENT_BLOCKDATAS 0x12 +#define TOCLIENT_BLOCKDATA_FM 0x12 enum { diff --git a/src/network/serveropcodes.cpp b/src/network/serveropcodes.cpp index cd52163b8..d56cd00ea 100644 --- a/src/network/serveropcodes.cpp +++ b/src/network/serveropcodes.cpp @@ -147,7 +147,7 @@ const ClientCommandFactory clientCommandFactoryTable[TOCLIENT_NUM_MSG_TYPES] = null_command_factory, // 0x0F { "TOCLIENT_INIT", 0, true }, // 0x10 null_command_factory, // 0x11 - { "TOCLIENT_BLOCKDATAS", 2, true }, // 0x20 + { "TOCLIENT_BLOCKDATA_FM", 2, true }, // 0x20 null_command_factory, // 0x13 null_command_factory, // 0x14 null_command_factory, // 0x15 diff --git a/src/util/msgpack_serialize.h b/src/util/msgpack_serialize.h index 6b32a7209..420ee724d 100644 --- a/src/util/msgpack_serialize.h +++ b/src/util/msgpack_serialize.h @@ -22,64 +22,82 @@ along with Freeminer. If not, see . #pragma once +#include #include "../msgpack_fix.h" #include "../serialization.h" //decompressZlib -#define PACK(x, y) {pk.pack((int)x); pk.pack(y);} +using packet_field_t = uint8_t; -#define PACK_ZIP(x, y) { \ - msgpack::sbuffer buffer_zip; \ - msgpack::packer pk_zip(&buffer_zip); \ - pk_zip.pack(y); \ - std::string s; \ - compressZlib(std::string(buffer_zip.data(), buffer_zip.size()), s); \ - PACK(x, s); \ +#define PACK(x, y) \ + { \ + pk.pack((packet_field_t)x); \ + pk.pack(y); \ + } + +#define PACK_ZIP(x, y) \ + { \ + msgpack::sbuffer buffer_zip; \ + msgpack::packer pk_zip(&buffer_zip); \ + pk_zip.pack(y); \ + std::string s; \ + compressZlib(std::string(buffer_zip.data(), buffer_zip.size()), s); \ + PACK(x, s); \ } #define MSGPACK_COMMAND -1 -#define MSGPACK_PACKET_INIT(id, x) \ - msgpack::sbuffer buffer; \ - msgpack::packer pk(&buffer); \ - pk.pack_map((x)+1); \ +#define MSGPACK_PACKET_INIT(id, x) \ + msgpack::sbuffer buffer; \ + msgpack::packer pk(&buffer); \ + pk.pack_map((x) + 1); \ PACK(MSGPACK_COMMAND, id); #if MSGPACK_VERSION_MAJOR < 1 #include -typedef std::map MsgpackPacket; +typedef std::map MsgpackPacket; #else #include -typedef std::unordered_map MsgpackPacket; +typedef std::unordered_map MsgpackPacket; #endif -template -bool packet_convert_safe(MsgpackPacket & packet, int field, T & to) { - if (!packet.count(field)) +template +bool packet_convert_safe(const MsgpackPacket &packet, packet_field_t field, T &to) +{ + const auto it = packet.find(field); + if (it == packet.end()) { return false; - packet[field].convert(to); + } + it->second.convert(to); return true; } -template -bool packet_convert_safe_zip(MsgpackPacket & packet, int field, T & to) { - if (!packet.count(field)) +template +bool packet_convert_safe_zip(const MsgpackPacket &packet, packet_field_t field, T &to) +{ + const auto it = packet.find(field); + if (it == packet.end()) { return false; + } try { std::string sz, s; - packet[field].convert(sz); + it->second.convert(sz); decompressZlib(sz, s); msgpack::unpacked msg; msgpack::unpack(msg, s.c_str(), s.size()); msgpack::object obj = msg.get(); obj.convert(to); - } catch (...) { return false; } + } catch (...) { + return false; + } return true; } -class MsgpackPacketSafe : public MsgpackPacket { +class MsgpackPacketSafe : public MsgpackPacket +{ public: - template - bool convert_safe(int field, T & to) { + template + bool convert_safe(packet_field_t field, T &to) + { return packet_convert_safe(*this, field, to); } };