Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
proller committed Sep 21, 2024
1 parent 4ba603a commit 0e53995
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 88 deletions.
71 changes: 17 additions & 54 deletions src/clientiface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ You should have received a copy of the GNU General Public License
along with Freeminer. If not, see <http://www.gnu.org/licenses/>.
*/

#include <sstream>
#include "clientiface.h"
#include "irr_v3d.h"
#include "log.h"
#include "network/fm_connection_use.h"
#include "util/numeric.h"
#include "profiler.h"
#include "gamedef.h"

#include <atomic>
#include <sstream>
#include "clientiface.h"
#include "network/serveropcodes.h"
#include "remoteplayer.h"
#include "settings.h"
Expand All @@ -34,15 +39,10 @@ along with Freeminer. If not, see <http://www.gnu.org/licenses/>.
#include "emerge.h"
#include "server/luaentity_sao.h"
#include "server/player_sao.h"
#include "log_types.h"
#include "log.h"
#include "util/srp.h"
#include "face_position_cache.h"

#include "util/numeric.h"
#include "profiler.h"
#include "gamedef.h"


const char *ClientInterface::statenames[] = {
"Invalid",
"Disconnecting",
Expand Down Expand Up @@ -99,14 +99,11 @@ LuaEntitySAO *getAttachedObject(PlayerSAO *sao, ServerEnvironment *env)
return dynamic_cast<LuaEntitySAO *>(ao);
}

#if WTF
int RemoteClient::GetNextBlocks (
ServerEnvironment *env,
EmergeManager * emerge,
float dtime,
std::vector<PrioritySortedBlockTransfer> &dest,
double m_uptime
)
std::vector<PrioritySortedBlockTransfer> &dest)
{
// Increment timers
m_nothing_to_send_pause_timer -= dtime;
Expand All @@ -123,24 +120,22 @@ int RemoteClient::GetNextBlocks (
}

if (m_nothing_to_send_pause_timer >= 0)
return;
return 0;

RemotePlayer *player = env->getPlayer(peer_id);
// This can happen sometimes; clients and players are not in perfect sync.
if (!player)
return;
return 0;

PlayerSAO *sao = player->getPlayerSAO();
if (!sao)
return;
return 0;

/*
// Won't send anything if already sending
if (m_blocks_sending.size() >= m_max_simul_sends) {
//infostream<<"Not sending any blocks, Queue full."<<std::endl;
return;
return 0;
}
*/

v3f playerpos = sao->getBasePosition();
// if the player is attached, get the velocity from the attached object
Expand Down Expand Up @@ -176,20 +171,12 @@ int RemoteClient::GetNextBlocks (
if (m_time_from_building < m_min_time_from_building) {
max_simul_sends_usually
= LIMITED_MAX_SIMULTANEOUS_BLOCK_SENDS;
*/
if(d_start<=1)
d_start=2;
++m_nearest_unsent_reset_want;
} else if (m_nearest_unsent_reset_want) {
m_nearest_unsent_reset_want = 0;
m_nearest_unsent_reset_timer = 999; //magical number more than ^ other number 120 - need to reset d on next iteration
}

/*
Number of blocks sending + number of blocks selected for sending
*/
u32 num_blocks_selected = 0;
u32 num_blocks_sending = 0;
u32 num_blocks_selected = m_blocks_sending.size();

/*
next time d will be continued from the d from which the nearest
Expand Down Expand Up @@ -227,7 +214,7 @@ int RemoteClient::GetNextBlocks (
if (m_nearest_unsent_d > 0) {
// make sure any blocks modified since the last time we sent blocks are resent
for (const v3s16 &p : m_blocks_modified) {
m_nearest_unsent_d = std::min(m_nearest_unsent_d, center.getDistanceFrom(p));
m_nearest_unsent_d = std::min(m_nearest_unsent_d.load(std::memory_order::relaxed), center.getDistanceFrom(p));
}
}
m_blocks_modified.clear();
Expand Down Expand Up @@ -257,10 +244,6 @@ int RemoteClient::GetNextBlocks (
s16 max_d_increment_at_time = 2;
if (d_max > d_start + max_d_increment_at_time)
d_max = d_start + max_d_increment_at_time;
/*if(d_max_gen > d_start+2)
d_max_gen = d_start+2;*/

//infostream<<"Starting from "<<d_start<<std::endl;

// cos(angle between velocity and camera) * |velocity|
// Limit to 0.0f in case player moves backwards.
Expand All @@ -283,8 +266,7 @@ int RemoteClient::GetNextBlocks (
Get the border/face dot coordinates of a "d-radiused"
box
*/
list = FacePositionCache::getFacePositions(d);
}
std::vector<v3s16> list = FacePositionCache::getFacePositions(d);

std::vector<v3s16>::iterator li;
for (li = list.begin(); li != list.end(); ++li) {
Expand Down Expand Up @@ -314,8 +296,6 @@ int RemoteClient::GetNextBlocks (
// If this is true, inexistent block will be made from scratch
bool generate = d <= d_max_gen;

//infostream<<"d="<<d<<std::endl;

/*
Don't generate or send if not in sight
FIXME This only works if the client uses a small enough
Expand Down Expand Up @@ -397,7 +377,6 @@ int RemoteClient::GetNextBlocks (
// get next one.
continue;
}
*/

/*
Add inexistent block to emerge queue.
Expand All @@ -411,9 +390,6 @@ int RemoteClient::GetNextBlocks (
nearest_emergefull_d = d;
goto queue_full_break;
}
} else {
//infostream << "skip tryload " << p << "\n";
}

// get next one.
continue;
Expand All @@ -429,22 +405,11 @@ int RemoteClient::GetNextBlocks (

dest.push_back(q);

if (block->content_only == CONTENT_AIR)
++num_blocks_air;
else
num_blocks_selected += 1;
}
}
queue_full_break:

//infostream<<"Stopped at "<<d<<" d_start="<<d_start<< " d_max="<<d_max<<" nearest_emerged_d="<<nearest_emerged_d<<" nearest_emergefull_d="<<nearest_emergefull_d<< " new_nearest_unsent_d="<<new_nearest_unsent_d<< " sel="<<num_blocks_selected<< "+"<<num_blocks_sending << " air="<<num_blocks_air<< " culled=" << blocks_occlusion_culled <<" cEN="<<occlusion_culling_enabled<<std::endl;
num_blocks_selected += num_blocks_sending;
if(!num_blocks_selected && !num_blocks_air && d_start <= d) {
//new_nearest_unsent_d = 0;
m_nothing_to_send_pause_timer = 1.0;
}


// If nothing was found for sending and nothing was queued for
// emerging, continue next time browsing from here
if (nearest_emerged_d != -1) {
Expand Down Expand Up @@ -472,11 +437,9 @@ int RemoteClient::GetNextBlocks (
// if the distance has changed, clear the occlusion cache
m_blocks_occ.clear();
}
return num_blocks_selected - num_blocks_sending;
return num_blocks_selected;
}

#endif

/*
void RemoteClient::GotBlock(v3s16 p)
{
Expand Down
69 changes: 37 additions & 32 deletions src/clientiface.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,33 @@ along with Freeminer. If not, see <http://www.gnu.org/licenses/>.

#pragma once

#include "irr_v3d.h" // for irrlicht datatypes

#include "constants.h"
#include "serialization.h" // for SER_FMT_VER_INVALID
#include "threading/concurrent_set.h"
#include "threading/concurrent_unordered_map.h"
#include "threading/concurrent_unordered_set.h"
#include "network/fm_connection_use.h"
#include "util/unordered_map_hash.h"
#include <atomic>
#include "msgpack_fix.h"


#include "irr_v3d.h" // for irrlicht datatypes

#include "constants.h"
#include "serialization.h" // for SER_FMT_VER_INVALID
#include "network/networkpacket.h"
#include "network/networkprotocol.h"
#include "network/address.h"
#include "porting.h"
#include "threading/mutex_auto_lock.h"
#include "clientdynamicinfo.h"

#include <atomic>
#include <list>
#include <vector>
#include <set>
#include <unordered_set>
#include <memory>
#include <mutex>

#include "msgpack_fix.h"

class MapBlock;
class ServerEnvironment;
class EmergeManager;
Expand Down Expand Up @@ -245,6 +246,7 @@ class RemoteClient
// The serialization version to use with the client
u8 serialization_version = SER_FMT_VER_INVALID;
//
std::atomic_ushort net_proto_version = 0;

//fm:
u16 net_proto_version_fm = 0;
Expand All @@ -260,11 +262,13 @@ class RemoteClient
v3f m_last_direction;
float m_nearest_unsent_reset_timer;
std::unordered_map<v3bpos_t, uint8_t> blocks;
std::atomic_ushort net_proto_version = {0};
void SetBlocksNotSent();
void SetBlockDeleted(v3bpos_t p);
std::unordered_map<v3bpos_t, uint8_t> far_blocks_requested;
std::unordered_set<v3bpos_t> far_blocks_sent;
int GetNextBlocksFm(ServerEnvironment *env, EmergeManager* emerge,
float dtime, std::vector<PrioritySortedBlockTransfer> &dest, double m_uptime);

// ==

/* Authentication information */
Expand All @@ -291,7 +295,7 @@ class RemoteClient
dtime is used for resetting send radius at slow interval
*/
int GetNextBlocks(ServerEnvironment *env, EmergeManager* emerge,
float dtime, std::vector<PrioritySortedBlockTransfer> &dest, double m_uptime);
float dtime, std::vector<PrioritySortedBlockTransfer> &dest);

void SentBlock(v3bpos_t p, double time);

Expand Down Expand Up @@ -440,7 +444,7 @@ class RemoteClient
Block is removed when GOTBLOCKS is received.
Value is time from sending. (not used at the moment)
*/
//std::unordered_map<v3s16, float> m_blocks_sending;
std::unordered_map<v3s16, float> m_blocks_sending;

/*
Blocks that have been modified since blocks were
Expand All @@ -450,7 +454,7 @@ class RemoteClient
List of block positions.
*/
//std::unordered_set<v3s16> m_blocks_modified;
std::unordered_set<v3s16> m_blocks_modified;

/*
Count of excess GotBlocks().
Expand Down Expand Up @@ -497,6 +501,28 @@ using RemoteClientVector = std::vector<std::shared_ptr<RemoteClient>>;
class ClientInterface {
public:

// fm:
/* send message to client */
void send(u16 peer_id, u8 channelnum, const msgpack::sbuffer &data, bool reliable);

void send(u16 peer_id, u8 channelnum, SharedBuffer<u8> data,
bool reliable); //todo: delete
void sendToAll(u16 channelnum, SharedBuffer<u8> data, bool reliable);
void sendToAll(u16 channelnum, msgpack::sbuffer const &buffer, bool reliable);
RemoteClientPtr getClient(u16 peer_id, ClientState state_min = CS_Active);
RemoteClientVector getClientList() {
RemoteClientVector clients;
auto lock = m_clients.lock_unique_rec();
for(auto & ir : m_clients) {
auto c = ir.second;
if (c)
clients.emplace_back(c);
}
return clients;
}
// ==


friend class Server;

ClientInterface(const std::shared_ptr<con_use::Connection> &con);
Expand All @@ -520,14 +546,7 @@ class ClientInterface {
/* send message to client */
void send(session_t peer_id, u8 channelnum, NetworkPacket *pkt, bool reliable);

/* send message to client */
void send(u16 peer_id, u8 channelnum, const msgpack::sbuffer &data, bool reliable);

void send(u16 peer_id, u8 channelnum, SharedBuffer<u8> data, bool reliable); //todo: delete

/* send to all clients */
void sendToAll(u16 channelnum, SharedBuffer<u8> data, bool reliable);
void sendToAll(u16 channelnum, msgpack::sbuffer const &buffer, bool reliable);
void sendToAll(NetworkPacket *pkt);

/* delete a client */
Expand All @@ -536,8 +555,6 @@ class ClientInterface {
/* create client */
void CreateClient(session_t peer_id);

RemoteClientPtr getClient(u16 peer_id, ClientState state_min=CS_Active);

/* get a client by peer_id */
RemoteClient *getClientNoEx(session_t peer_id, ClientState state_min = CS_Active);

Expand Down Expand Up @@ -581,18 +598,6 @@ class ClientInterface {
RemoteClientMap& getClientList() { return m_clients; }
*/

public:
RemoteClientVector getClientList() {
RemoteClientVector clients;
auto lock = m_clients.lock_unique_rec();
for(auto & ir : m_clients) {
auto c = ir.second;
if (c)
clients.emplace_back(c);
}
return clients;
}

private:
/* update internal player list */
void UpdatePlayerList();
Expand Down
2 changes: 1 addition & 1 deletion src/fm_clientiface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "util/directiontables.h"
#include "util/numeric.h"

int RemoteClient::GetNextBlocks(ServerEnvironment *env, EmergeManager *emerge,
int RemoteClient::GetNextBlocksFm(ServerEnvironment *env, EmergeManager *emerge,
float dtime, std::vector<PrioritySortedBlockTransfer> &dest, double m_uptime)
{
auto lock = try_lock_unique_rec();
Expand Down
7 changes: 6 additions & 1 deletion src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2898,7 +2898,12 @@ int Server::SendBlocks(float dtime)

//total_sending += client->getSendingCount();
const auto old_count = queue.size();
total += client->GetNextBlocks(m_env,m_emerge, dtime, queue, m_uptime_counter->get() + m_env->m_game_time_start);
if (client->net_proto_version_fm) {
total += client->GetNextBlocksFm(m_env, m_emerge, dtime, queue,
m_uptime_counter->get() + m_env->m_game_time_start);
} else {
total += client->GetNextBlocks(m_env, m_emerge, dtime, queue);
}
//total_sending += queue.size();
unique_clients += queue.size() > old_count ? 1 : 0;
}
Expand Down

0 comments on commit 0e53995

Please sign in to comment.