mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-03-10 08:00:05 +00:00
Codechange: make encoded length of packet size and type more explicit
This commit is contained in:
parent
5706801ea7
commit
d26629c15b
@ -223,7 +223,7 @@ bool Packet::CanReadFromPacket(size_t bytes_to_read, bool close_connection)
|
|||||||
*/
|
*/
|
||||||
bool Packet::HasPacketSizeData() const
|
bool Packet::HasPacketSizeData() const
|
||||||
{
|
{
|
||||||
return this->pos >= sizeof(PacketSize);
|
return this->pos >= EncodedLengthOfPacketSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -250,10 +250,10 @@ bool Packet::ParsePacketSize()
|
|||||||
/* If the size of the packet is less than the bytes required for the size and type of
|
/* If the size of the packet is less than the bytes required for the size and type of
|
||||||
* the packet, or more than the allowed limit, then something is wrong with the packet.
|
* the packet, or more than the allowed limit, then something is wrong with the packet.
|
||||||
* In those cases the packet can generally be regarded as containing garbage data. */
|
* In those cases the packet can generally be regarded as containing garbage data. */
|
||||||
if (size < sizeof(PacketSize) + sizeof(PacketType) || size > this->limit) return false;
|
if (size < EncodedLengthOfPacketSize() + EncodedLengthOfPacketType() || size > this->limit) return false;
|
||||||
|
|
||||||
this->buffer.resize(size);
|
this->buffer.resize(size);
|
||||||
this->pos = sizeof(PacketSize);
|
this->pos = static_cast<PacketSize>(EncodedLengthOfPacketSize());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -263,7 +263,7 @@ bool Packet::ParsePacketSize()
|
|||||||
void Packet::PrepareToRead()
|
void Packet::PrepareToRead()
|
||||||
{
|
{
|
||||||
/* Put the position on the right place */
|
/* Put the position on the right place */
|
||||||
this->pos = sizeof(PacketSize);
|
this->pos = static_cast<PacketSize>(EncodedLengthOfPacketSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -272,8 +272,8 @@ void Packet::PrepareToRead()
|
|||||||
*/
|
*/
|
||||||
PacketType Packet::GetPacketType() const
|
PacketType Packet::GetPacketType() const
|
||||||
{
|
{
|
||||||
assert(this->Size() >= sizeof(PacketSize) + sizeof(PacketType));
|
assert(this->Size() >= EncodedLengthOfPacketSize() + EncodedLengthOfPacketType());
|
||||||
return static_cast<PacketType>(buffer[sizeof(PacketSize)]);
|
return static_cast<PacketType>(buffer[EncodedLengthOfPacketSize()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -40,6 +40,8 @@ typedef uint8_t PacketType; ///< Identifier for the packet
|
|||||||
* (year % 4 == 0) and ((year % 100 != 0) or (year % 400 == 0))
|
* (year % 4 == 0) and ((year % 100 != 0) or (year % 400 == 0))
|
||||||
*/
|
*/
|
||||||
struct Packet {
|
struct Packet {
|
||||||
|
static constexpr size_t EncodedLengthOfPacketSize() { return sizeof(PacketSize); }
|
||||||
|
static constexpr size_t EncodedLengthOfPacketType() { return sizeof(PacketType); }
|
||||||
private:
|
private:
|
||||||
/** The current read/write position in the packet */
|
/** The current read/write position in the packet */
|
||||||
PacketSize pos;
|
PacketSize pos;
|
||||||
@ -52,7 +54,7 @@ private:
|
|||||||
NetworkSocketHandler *cs;
|
NetworkSocketHandler *cs;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Packet(NetworkSocketHandler *cs, size_t limit, size_t initial_read_size = sizeof(PacketSize));
|
Packet(NetworkSocketHandler *cs, size_t limit, size_t initial_read_size = EncodedLengthOfPacketSize());
|
||||||
Packet(NetworkSocketHandler *cs, PacketType type, size_t limit = COMPAT_MTU);
|
Packet(NetworkSocketHandler *cs, PacketType type, size_t limit = COMPAT_MTU);
|
||||||
|
|
||||||
/* Sending/writing of packets */
|
/* Sending/writing of packets */
|
||||||
|
Loading…
Reference in New Issue
Block a user