Skip to content

Commit 0b2f091

Browse files
committed
Improve client-side packet receiving
1 parent 154080c commit 0b2f091

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

src/client/client.cpp

+16-19
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ void Client::connect(Address address, bool is_local_server)
311311
{
312312
initLocalMapSaving(address, m_address_name, is_local_server);
313313

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

796797
void Client::ReceiveAll()
797798
{
799+
NetworkPacket pkt;
798800
u64 start_ms = porting::getTimeMs();
799-
for(;;)
800-
{
801+
const u64 budget = 100;
802+
for(;;) {
801803
// Limit time even if there would be huge amounts of data to
802804
// process
803-
if(porting::getTimeMs() > start_ms + 100)
805+
if (porting::getTimeMs() > start_ms + budget) {
806+
infostream << "Client::ReceiveAll(): "
807+
"Packet processing budget exceeded." << std::endl;
804808
break;
809+
}
805810

811+
pkt.clear();
806812
try {
807-
Receive();
808-
g_profiler->graphAdd("client_received_packets", 1);
809-
}
810-
catch(con::NoIncomingDataException &e) {
811-
break;
812-
}
813-
catch(con::InvalidIncomingDataException &e) {
814-
infostream<<"Client::ReceiveAll(): "
813+
if (!m_con->TryReceive(&pkt))
814+
break;
815+
ProcessData(&pkt);
816+
} catch (const con::InvalidIncomingDataException &e) {
817+
infostream << "Client::ReceiveAll(): "
815818
"InvalidIncomingDataException: what()="
816-
<<e.what()<<std::endl;
819+
<< e.what() << std::endl;
817820
}
818821
}
819822
}
820823

821-
void Client::Receive()
822-
{
823-
NetworkPacket pkt;
824-
m_con->Receive(&pkt);
825-
ProcessData(&pkt);
826-
}
827-
828824
inline void Client::handleCommand(NetworkPacket* pkt)
829825
{
830826
const ToClientCommandHandler& opHandle = toClientCommandTable[pkt->getCommand()];
@@ -841,6 +837,7 @@ void Client::ProcessData(NetworkPacket *pkt)
841837

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

845842
/*
846843
If this check is removed, be sure to change the queue

src/client/client.h

-1
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,6 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
453453
bool is_local_server);
454454

455455
void ReceiveAll();
456-
void Receive();
457456

458457
void sendPlayerPos();
459458

0 commit comments

Comments
 (0)