Fail with packet size in constructor

This commit is contained in:
Atlante45 2015-07-09 14:19:47 -07:00
parent c77310019d
commit a2a60f253c
2 changed files with 7 additions and 21 deletions

View file

@ -30,16 +30,6 @@ qint64 NLPacket::localHeaderSize() const {
}
std::unique_ptr<NLPacket> NLPacket::create(PacketType::Value type, qint64 size) {
auto maxPayload = maxPayloadSize(type);
if (size == -1) {
// default size of -1, means biggest packet possible
size = maxPayload;
}
// Fail with invalid size
Q_ASSERT(size >= 0 || size < maxPayload);
// allocate memory
return std::unique_ptr<NLPacket>(new NLPacket(type, size));
}

View file

@ -24,16 +24,6 @@ qint64 Packet::maxPayloadSize(PacketType::Value type) {
}
std::unique_ptr<Packet> Packet::create(PacketType::Value type, qint64 size) {
auto maxPayload = maxPayloadSize(type);
if (size == -1) {
// default size of -1, means biggest packet possible
size = maxPayload;
}
// Fail with invalid size
Q_ASSERT(size >= 0 || size < maxPayload);
// allocate memory
return std::unique_ptr<Packet>(new Packet(type, size));
}
@ -55,8 +45,14 @@ Packet::Packet(PacketType::Value type, qint64 size) :
_packet(new char(_packetSize)),
_payloadStart(_packet.get() + localHeaderSize(_type)),
_capacity(size) {
auto maxPayload = maxPayloadSize(type);
if (size == -1) {
// default size of -1, means biggest packet possible
size = maxPayload;
}
// Sanity check
Q_ASSERT(size <= maxPayloadSize(type));
Q_ASSERT(size >= 0 || size < maxPayload);
// copy packet type and version in header
writePacketTypeAndVersion(type);