mirror of https://github.com/OpenTTD/OpenTTD.git
Codechange: encapsulate reading the size of a Packet
This commit is contained in:
parent
6f161f6559
commit
f71fb0f54a
|
@ -230,6 +230,18 @@ bool Packet::HasPacketSizeData() const
|
||||||
return this->pos >= sizeof(PacketSize);
|
return this->pos >= sizeof(PacketSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the number of bytes in the packet.
|
||||||
|
* When sending a packet this is the size of the data up to that moment.
|
||||||
|
* When receiving a packet (before PrepareToRead) this is the allocated size for the data to be read.
|
||||||
|
* When reading a packet (after PrepareToRead) this is the full size of the packet.
|
||||||
|
* @return The packet's size.
|
||||||
|
*/
|
||||||
|
size_t Packet::Size() const
|
||||||
|
{
|
||||||
|
return this->size;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads the packet size from the raw packet and stores it in the packet->size
|
* Reads the packet size from the raw packet and stores it in the packet->size
|
||||||
* @return True iff the packet size seems plausible.
|
* @return True iff the packet size seems plausible.
|
||||||
|
|
|
@ -77,6 +77,7 @@ public:
|
||||||
/* Reading/receiving of packets */
|
/* Reading/receiving of packets */
|
||||||
bool HasPacketSizeData() const;
|
bool HasPacketSizeData() const;
|
||||||
bool ParsePacketSize();
|
bool ParsePacketSize();
|
||||||
|
size_t Size() const;
|
||||||
void PrepareToRead();
|
void PrepareToRead();
|
||||||
|
|
||||||
bool CanReadFromPacket(size_t bytes_to_read, bool close_connection = false);
|
bool CanReadFromPacket(size_t bytes_to_read, bool close_connection = false);
|
||||||
|
|
|
@ -137,7 +137,7 @@ void NetworkUDPSocketHandler::ReceivePackets()
|
||||||
|
|
||||||
/* If the size does not match the packet must be corrupted.
|
/* If the size does not match the packet must be corrupted.
|
||||||
* Otherwise it will be marked as corrupted later on. */
|
* Otherwise it will be marked as corrupted later on. */
|
||||||
if (!p.ParsePacketSize() || nbytes != p.size) {
|
if (!p.ParsePacketSize() || (size_t)nbytes != p.Size()) {
|
||||||
DEBUG(net, 1, "received a packet with mismatching size from %s", address.GetAddressAsString().c_str());
|
DEBUG(net, 1, "received a packet with mismatching size from %s", address.GetAddressAsString().c_str());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
|
@ -247,7 +247,7 @@ Packet *ServerNetworkGameSocketHandler::ReceivePacket()
|
||||||
/* We can receive a packet, so try that and if needed account for
|
/* We can receive a packet, so try that and if needed account for
|
||||||
* the amount of received data. */
|
* the amount of received data. */
|
||||||
Packet *p = this->NetworkTCPSocketHandler::ReceivePacket();
|
Packet *p = this->NetworkTCPSocketHandler::ReceivePacket();
|
||||||
if (p != nullptr) this->receive_limit -= p->size;
|
if (p != nullptr) this->receive_limit -= p->Size();
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1832,7 +1832,7 @@ void NetworkServer_Tick(bool send_frame)
|
||||||
for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
|
for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
|
||||||
/* We allow a number of bytes per frame, but only to the burst amount
|
/* We allow a number of bytes per frame, but only to the burst amount
|
||||||
* to be available for packet receiving at any particular time. */
|
* to be available for packet receiving at any particular time. */
|
||||||
cs->receive_limit = std::min<int>(cs->receive_limit + _settings_client.network.bytes_per_frame,
|
cs->receive_limit = std::min<size_t>(cs->receive_limit + _settings_client.network.bytes_per_frame,
|
||||||
_settings_client.network.bytes_per_frame_burst);
|
_settings_client.network.bytes_per_frame_burst);
|
||||||
|
|
||||||
/* Check if the speed of the client is what we can expect from a client */
|
/* Check if the speed of the client is what we can expect from a client */
|
||||||
|
|
|
@ -67,7 +67,7 @@ public:
|
||||||
uint32 last_token_frame; ///< The last frame we received the right token
|
uint32 last_token_frame; ///< The last frame we received the right token
|
||||||
ClientStatus status; ///< Status of this client
|
ClientStatus status; ///< Status of this client
|
||||||
CommandQueue outgoing_queue; ///< The command-queue awaiting delivery
|
CommandQueue outgoing_queue; ///< The command-queue awaiting delivery
|
||||||
int receive_limit; ///< Amount of bytes that we can receive at this moment
|
size_t receive_limit; ///< Amount of bytes that we can receive at this moment
|
||||||
|
|
||||||
struct PacketWriter *savegame; ///< Writer used to write the savegame.
|
struct PacketWriter *savegame; ///< Writer used to write the savegame.
|
||||||
NetworkAddress client_address; ///< IP-address of the client (so he can be banned)
|
NetworkAddress client_address; ///< IP-address of the client (so he can be banned)
|
||||||
|
|
Loading…
Reference in New Issue