Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improve client-side packet receiving
  • Loading branch information
sfan5 committed Nov 25, 2019
1 parent 154080c commit 0b2f091
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
35 changes: 16 additions & 19 deletions src/client/client.cpp
Expand Up @@ -311,6 +311,7 @@ void Client::connect(Address address, bool is_local_server)
{
initLocalMapSaving(address, m_address_name, is_local_server);

// Since we use TryReceive() a timeout here would be ineffective anyway
m_con->SetTimeoutMs(0);
m_con->Connect(address);
}
Expand Down Expand Up @@ -795,36 +796,31 @@ void Client::initLocalMapSaving(const Address &address,

void Client::ReceiveAll()
{
NetworkPacket pkt;
u64 start_ms = porting::getTimeMs();
for(;;)
{
const u64 budget = 100;
for(;;) {
// Limit time even if there would be huge amounts of data to
// process
if(porting::getTimeMs() > start_ms + 100)
if (porting::getTimeMs() > start_ms + budget) {
infostream << "Client::ReceiveAll(): "
"Packet processing budget exceeded." << std::endl;
break;
}

pkt.clear();
try {
Receive();
g_profiler->graphAdd("client_received_packets", 1);
}
catch(con::NoIncomingDataException &e) {
break;
}
catch(con::InvalidIncomingDataException &e) {
infostream<<"Client::ReceiveAll(): "
if (!m_con->TryReceive(&pkt))
break;
ProcessData(&pkt);
} catch (const con::InvalidIncomingDataException &e) {
infostream << "Client::ReceiveAll(): "
"InvalidIncomingDataException: what()="
<<e.what()<<std::endl;
<< e.what() << std::endl;
}
}
}

void Client::Receive()
{
NetworkPacket pkt;
m_con->Receive(&pkt);
ProcessData(&pkt);
}

inline void Client::handleCommand(NetworkPacket* pkt)
{
const ToClientCommandHandler& opHandle = toClientCommandTable[pkt->getCommand()];
Expand All @@ -841,6 +837,7 @@ void Client::ProcessData(NetworkPacket *pkt)

//infostream<<"Client: received command="<<command<<std::endl;
m_packetcounter.add((u16)command);
g_profiler->graphAdd("client_received_packets", 1);

/*
If this check is removed, be sure to change the queue
Expand Down
1 change: 0 additions & 1 deletion src/client/client.h
Expand Up @@ -453,7 +453,6 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
bool is_local_server);

void ReceiveAll();
void Receive();

void sendPlayerPos();

Expand Down

0 comments on commit 0b2f091

Please sign in to comment.