Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
proller committed Oct 12, 2024
1 parent b5ecbd5 commit 8747a51
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/client/fm_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
6 changes: 6 additions & 0 deletions src/fm_clientiface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,8 @@ int RemoteClient::GetNextBlocksFm(ServerEnvironment *env, EmergeManager *emerge,
TRY_UNIQUE_LOCK(far_blocks_requested_mutex)
{
std::multimap<int32_t, MapBlockP> 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;
Expand All @@ -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)) {
Expand Down
10 changes: 5 additions & 5 deletions src/fm_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,16 +505,16 @@ 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;
}
if (client->far_blocks_requested.size() < step) {
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;
}
}
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/network/clientopcodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/network/fm_networkprotocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ enum
TOCLIENT_INIT_GAMEID,
};

#define TOCLIENT_BLOCKDATAS 0x12
#define TOCLIENT_BLOCKDATA_FM 0x12

enum
{
Expand Down
2 changes: 1 addition & 1 deletion src/network/serveropcodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 44 additions & 26 deletions src/util/msgpack_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,64 +22,82 @@ along with Freeminer. If not, see <http://www.gnu.org/licenses/>.

#pragma once

#include <cstdint>
#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<msgpack::sbuffer> 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<msgpack::sbuffer> 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<msgpack::sbuffer> pk(&buffer); \
pk.pack_map((x)+1); \
#define MSGPACK_PACKET_INIT(id, x) \
msgpack::sbuffer buffer; \
msgpack::packer<msgpack::sbuffer> pk(&buffer); \
pk.pack_map((x) + 1); \
PACK(MSGPACK_COMMAND, id);

#if MSGPACK_VERSION_MAJOR < 1
#include <map>
typedef std::map<int, msgpack::object> MsgpackPacket;
typedef std::map<packet_field_t, msgpack::object> MsgpackPacket;
#else
#include <unordered_map>
typedef std::unordered_map<int, msgpack::object> MsgpackPacket;
typedef std::unordered_map<packet_field_t, msgpack::object> MsgpackPacket;
#endif

template<typename T>
bool packet_convert_safe(MsgpackPacket & packet, int field, T & to) {
if (!packet.count(field))
template <typename T>
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<typename T>
bool packet_convert_safe_zip(MsgpackPacket & packet, int field, T & to) {
if (!packet.count(field))
template <typename T>
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<typename T>
bool convert_safe(int field, T & to) {
template <typename T>
bool convert_safe(packet_field_t field, T &to)
{
return packet_convert_safe(*this, field, to);
}
};

0 comments on commit 8747a51

Please sign in to comment.