Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Create PacketError exception and use it with ACTIVEOBJECT_REMOVE_ADD …
…handler which can be unreliable
  • Loading branch information
nerzhul committed Apr 3, 2015
1 parent 92f2069 commit aa340fd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
5 changes: 5 additions & 0 deletions src/exceptions.h
Expand Up @@ -70,6 +70,11 @@ class SerializationError : public BaseException {
SerializationError(const std::string &s): BaseException(s) {}
};

class PacketError : public BaseException {
public:
PacketError(const std::string &s): BaseException(s) {}
};

class LoadError : public BaseException {
public:
LoadError(const std::string &s): BaseException(s) {}
Expand Down
32 changes: 18 additions & 14 deletions src/network/clientpackethandler.cpp
Expand Up @@ -336,7 +336,6 @@ void Client::handleCommand_ChatMessage(NetworkPacket* pkt)
void Client::handleCommand_ActiveObjectRemoveAdd(NetworkPacket* pkt)
{
/*
u16 command
u16 count of removed objects
for all removed objects {
u16 id
Expand All @@ -350,23 +349,28 @@ void Client::handleCommand_ActiveObjectRemoveAdd(NetworkPacket* pkt)
}
*/

// Read removed objects
u8 type;
u16 removed_count, added_count, id;
try {
u8 type;
u16 removed_count, added_count, id;

*pkt >> removed_count;
// Read removed objects
*pkt >> removed_count;

for (u16 i = 0; i < removed_count; i++) {
*pkt >> id;
m_env.removeActiveObject(id);
}
for (u16 i = 0; i < removed_count; i++) {
*pkt >> id;
m_env.removeActiveObject(id);
}

// Read added objects
*pkt >> added_count;
// Read added objects
*pkt >> added_count;

for (u16 i = 0; i < added_count; i++) {
*pkt >> id >> type;
m_env.addActiveObject(id, type, pkt->readLongString());
for (u16 i = 0; i < added_count; i++) {
*pkt >> id >> type;
m_env.addActiveObject(id, type, pkt->readLongString());
}
} catch (PacketError &e) {
infostream << "handleCommand_ActiveObjectRemoveAdd: " << e.what()
<< ". The packet is unreliable, ignoring" << std::endl;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/network/networkpacket.cpp
Expand Up @@ -45,7 +45,7 @@ void NetworkPacket::checkReadOffset(u32 from_offset)
std::stringstream ss;
ss << "Reading outside packet (offset: " <<
from_offset << ", packet size: " << getSize() << ")";
throw SerializationError(ss.str());
throw PacketError(ss.str());
}
}

Expand Down

1 comment on commit aa340fd

@Megaf
Copy link
Contributor

@Megaf Megaf commented on aa340fd Apr 3, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#2541 fixed.

Please sign in to comment.