Skip to content

Commit aa340fd

Browse files
committedApr 3, 2015
Create PacketError exception and use it with ACTIVEOBJECT_REMOVE_ADD handler which can be unreliable
1 parent 92f2069 commit aa340fd

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed
 

Diff for: ‎src/exceptions.h

+5
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ class SerializationError : public BaseException {
7070
SerializationError(const std::string &s): BaseException(s) {}
7171
};
7272

73+
class PacketError : public BaseException {
74+
public:
75+
PacketError(const std::string &s): BaseException(s) {}
76+
};
77+
7378
class LoadError : public BaseException {
7479
public:
7580
LoadError(const std::string &s): BaseException(s) {}

Diff for: ‎src/network/clientpackethandler.cpp

+18-14
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,6 @@ void Client::handleCommand_ChatMessage(NetworkPacket* pkt)
336336
void Client::handleCommand_ActiveObjectRemoveAdd(NetworkPacket* pkt)
337337
{
338338
/*
339-
u16 command
340339
u16 count of removed objects
341340
for all removed objects {
342341
u16 id
@@ -350,23 +349,28 @@ void Client::handleCommand_ActiveObjectRemoveAdd(NetworkPacket* pkt)
350349
}
351350
*/
352351

353-
// Read removed objects
354-
u8 type;
355-
u16 removed_count, added_count, id;
352+
try {
353+
u8 type;
354+
u16 removed_count, added_count, id;
356355

357-
*pkt >> removed_count;
356+
// Read removed objects
357+
*pkt >> removed_count;
358358

359-
for (u16 i = 0; i < removed_count; i++) {
360-
*pkt >> id;
361-
m_env.removeActiveObject(id);
362-
}
359+
for (u16 i = 0; i < removed_count; i++) {
360+
*pkt >> id;
361+
m_env.removeActiveObject(id);
362+
}
363363

364-
// Read added objects
365-
*pkt >> added_count;
364+
// Read added objects
365+
*pkt >> added_count;
366366

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

Diff for: ‎src/network/networkpacket.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void NetworkPacket::checkReadOffset(u32 from_offset)
4545
std::stringstream ss;
4646
ss << "Reading outside packet (offset: " <<
4747
from_offset << ", packet size: " << getSize() << ")";
48-
throw SerializationError(ss.str());
48+
throw PacketError(ss.str());
4949
}
5050
}
5151

1 commit comments

Comments
 (1)

Megaf commented on Apr 3, 2015

@Megaf
Contributor

#2541 fixed.

Please sign in to comment.