TOCLIENT_ACTIVE_OBJECT_REMOVE_ADD can be unreliable, catch PacketError exception.
authorLoic Blot <loic.blot@unix-experience.fr>
Sun, 5 Apr 2015 09:37:53 +0000 (11:37 +0200)
committerLoic Blot <loic.blot@unix-experience.fr>
Sun, 5 Apr 2015 09:39:38 +0000 (11:39 +0200)
Also set the packet size at creation not when pushing rawString, no functional change

src/network/clientpackethandler.cpp
src/server.cpp

index a9096accc1adac85e0febe777c4913b2b84dcc33..68d4245f8a0ba49ba0c89ab281725cf0769245e9 100644 (file)
@@ -377,7 +377,6 @@ void Client::handleCommand_ActiveObjectRemoveAdd(NetworkPacket* pkt)
 void Client::handleCommand_ActiveObjectMessages(NetworkPacket* pkt)
 {
        /*
-               u16 command
                for all objects
                {
                        u16 id
@@ -391,21 +390,27 @@ void Client::handleCommand_ActiveObjectMessages(NetworkPacket* pkt)
        // Throw them in an istringstream
        std::istringstream is(datastring, std::ios_base::binary);
 
-       while(is.eof() == false) {
-               is.read(buf, 2);
-               u16 id = readU16((u8*)buf);
-               if (is.eof())
-                       break;
-               is.read(buf, 2);
-               size_t message_size = readU16((u8*)buf);
-               std::string message;
-               message.reserve(message_size);
-               for (u32 i = 0; i < message_size; i++) {
-                       is.read(buf, 1);
-                       message.append(buf, 1);
+       try {
+               while(is.eof() == false) {
+                       is.read(buf, 2);
+                       u16 id = readU16((u8*)buf);
+                       if (is.eof())
+                               break;
+                       is.read(buf, 2);
+                       size_t message_size = readU16((u8*)buf);
+                       std::string message;
+                       message.reserve(message_size);
+                       for (u32 i = 0; i < message_size; i++) {
+                               is.read(buf, 1);
+                               message.append(buf, 1);
+                       }
+                       // Pass on to the environment
+                       m_env.processActiveObjectMessage(id, message);
                }
-               // Pass on to the environment
-               m_env.processActiveObjectMessage(id, message);
+       // Packet could be unreliable then ignore it
+       } catch (PacketError &e) {
+               infostream << "handleCommand_ActiveObjectMessages: " << e.what()
+                                       << ". The packet is unreliable, ignoring" << std::endl;
        }
 }
 
index 4971e6f66733e571c544bf3b0dc106746c2dbcb8..cb62f7e86115ffa12cd50472b2e403de11544afe 100644 (file)
@@ -1897,7 +1897,7 @@ void Server::SendPlayerInventoryFormspec(u16 peer_id)
 
 u32 Server::SendActiveObjectRemoveAdd(u16 peer_id, const std::string &datas)
 {
-       NetworkPacket pkt(TOCLIENT_ACTIVE_OBJECT_REMOVE_ADD, 0, peer_id);
+       NetworkPacket pkt(TOCLIENT_ACTIVE_OBJECT_REMOVE_ADD, datas.size(), peer_id);
        pkt.putRawString(datas.c_str(), datas.size());
        Send(&pkt);
        return pkt.getSize();
@@ -1906,7 +1906,7 @@ u32 Server::SendActiveObjectRemoveAdd(u16 peer_id, const std::string &datas)
 void Server::SendActiveObjectMessages(u16 peer_id, const std::string &datas, bool reliable)
 {
        NetworkPacket pkt(TOCLIENT_ACTIVE_OBJECT_MESSAGES,
-                       0, peer_id);
+                       datas.size(), peer_id);
 
        pkt.putRawString(datas.c_str(), datas.size());