mirror of https://github.com/OpenTTD/OpenTTD.git
(svn r21357) -Codechange: make it possible to resize the packet's buffer
This commit is contained in:
parent
44937dfa5e
commit
9c83a8975f
|
@ -26,10 +26,11 @@ Packet::Packet(NetworkSocketHandler *cs)
|
|||
{
|
||||
assert(cs != NULL);
|
||||
|
||||
this->cs = cs;
|
||||
this->next = NULL;
|
||||
this->pos = 0; // We start reading from here
|
||||
this->size = 0;
|
||||
this->cs = cs;
|
||||
this->next = NULL;
|
||||
this->pos = 0; // We start reading from here
|
||||
this->size = 0;
|
||||
this->buffer = MallocT<byte>(SEND_MTU);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -44,9 +45,18 @@ Packet::Packet(PacketType type)
|
|||
/* Skip the size so we can write that in before sending the packet */
|
||||
this->pos = 0;
|
||||
this->size = sizeof(PacketSize);
|
||||
this->buffer = MallocT<byte>(SEND_MTU);
|
||||
this->buffer[this->size++] = type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Free the buffer of this packet.
|
||||
*/
|
||||
Packet::~Packet()
|
||||
{
|
||||
free(this->buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the packet size from the raw packet from packet->size
|
||||
*/
|
||||
|
@ -79,20 +89,20 @@ void Packet::Send_bool(bool data)
|
|||
|
||||
void Packet::Send_uint8(uint8 data)
|
||||
{
|
||||
assert(this->size < sizeof(this->buffer) - sizeof(data));
|
||||
assert(this->size < SEND_MTU - sizeof(data));
|
||||
this->buffer[this->size++] = data;
|
||||
}
|
||||
|
||||
void Packet::Send_uint16(uint16 data)
|
||||
{
|
||||
assert(this->size < sizeof(this->buffer) - sizeof(data));
|
||||
assert(this->size < SEND_MTU - sizeof(data));
|
||||
this->buffer[this->size++] = GB(data, 0, 8);
|
||||
this->buffer[this->size++] = GB(data, 8, 8);
|
||||
}
|
||||
|
||||
void Packet::Send_uint32(uint32 data)
|
||||
{
|
||||
assert(this->size < sizeof(this->buffer) - sizeof(data));
|
||||
assert(this->size < SEND_MTU - sizeof(data));
|
||||
this->buffer[this->size++] = GB(data, 0, 8);
|
||||
this->buffer[this->size++] = GB(data, 8, 8);
|
||||
this->buffer[this->size++] = GB(data, 16, 8);
|
||||
|
@ -101,7 +111,7 @@ void Packet::Send_uint32(uint32 data)
|
|||
|
||||
void Packet::Send_uint64(uint64 data)
|
||||
{
|
||||
assert(this->size < sizeof(this->buffer) - sizeof(data));
|
||||
assert(this->size < SEND_MTU - sizeof(data));
|
||||
this->buffer[this->size++] = GB(data, 0, 8);
|
||||
this->buffer[this->size++] = GB(data, 8, 8);
|
||||
this->buffer[this->size++] = GB(data, 16, 8);
|
||||
|
@ -121,7 +131,7 @@ void Packet::Send_string(const char *data)
|
|||
{
|
||||
assert(data != NULL);
|
||||
/* The <= *is* valid due to the fact that we are comparing sizes and not the index. */
|
||||
assert(this->size + strlen(data) + 1 <= sizeof(this->buffer));
|
||||
assert(this->size + strlen(data) + 1 <= SEND_MTU);
|
||||
while ((this->buffer[this->size++] = *data++) != '\0') {}
|
||||
}
|
||||
|
||||
|
|
|
@ -42,14 +42,17 @@ struct Packet {
|
|||
PacketSize size;
|
||||
/** The current read/write position in the packet */
|
||||
PacketSize pos;
|
||||
/** The buffer of this packet */
|
||||
byte buffer[SEND_MTU];
|
||||
/** The buffer of this packet, of basically variable length up to SEND_MTU. */
|
||||
byte *buffer;
|
||||
|
||||
private:
|
||||
/** Socket we're associated with. */
|
||||
NetworkSocketHandler *cs;
|
||||
|
||||
public:
|
||||
Packet(NetworkSocketHandler *cs);
|
||||
Packet(PacketType type);
|
||||
~Packet();
|
||||
|
||||
/* Sending/writing of packets */
|
||||
void PrepareToSend();
|
||||
|
|
|
@ -63,6 +63,11 @@ void NetworkTCPSocketHandler::Send_Packet(Packet *packet)
|
|||
|
||||
packet->PrepareToSend();
|
||||
|
||||
/* Reallocate the packet as in 99+% of the times we send at most 25 bytes and
|
||||
* keeping the other 1400+ bytes wastes memory, especially when someone tries
|
||||
* to do a denial of service attack! */
|
||||
packet->buffer = ReallocT(packet->buffer, packet->size);
|
||||
|
||||
/* Locate last packet buffered for the client */
|
||||
p = this->packet_queue;
|
||||
if (p == NULL) {
|
||||
|
|
|
@ -121,12 +121,11 @@ void NetworkUDPSocketHandler::ReceivePackets()
|
|||
memset(&client_addr, 0, sizeof(client_addr));
|
||||
|
||||
Packet p(this);
|
||||
int packet_len = sizeof(p.buffer);
|
||||
socklen_t client_len = sizeof(client_addr);
|
||||
|
||||
/* Try to receive anything */
|
||||
SetNonBlocking(s->second); // Some OSes seem to lose the non-blocking status of the socket
|
||||
int nbytes = recvfrom(s->second, (char*)p.buffer, packet_len, 0, (struct sockaddr *)&client_addr, &client_len);
|
||||
int nbytes = recvfrom(s->second, (char*)p.buffer, SEND_MTU, 0, (struct sockaddr *)&client_addr, &client_len);
|
||||
|
||||
/* We got some bytes for the base header of the packet. */
|
||||
if (nbytes > 2) {
|
||||
|
|
Loading…
Reference in New Issue