Improve client-side packet receiving
authorsfan5 <sfan5@live.de>
Tue, 19 Nov 2019 19:23:00 +0000 (20:23 +0100)
committersfan5 <sfan5@live.de>
Mon, 25 Nov 2019 19:00:24 +0000 (20:00 +0100)
src/client/client.cpp
src/client/client.h

index 5d4793c8afb1a6026d8a46d0fec160fcc0d4e47d..315fcd4108fce315d1226bebef4d4c2f7a230f09 100644 (file)
@@ -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);
 }
@@ -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()];
@@ -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
index 0e0765caea109a3136cbd184096c96af8bdfdd0b..10608ccf92d1888f333d498c30b3919e3f38a022 100644 (file)
@@ -453,7 +453,6 @@ private:
                        bool is_local_server);
 
        void ReceiveAll();
-       void Receive();
 
        void sendPlayerPos();