mirror of https://github.com/OpenTTD/OpenTTD
(svn r21357) -Codechange: make it possible to resize the packet's buffer
parent
44937dfa5e
commit
9c83a8975f
|
@ -26,10 +26,11 @@ Packet::Packet(NetworkSocketHandler *cs)
|
||||||
{
|
{
|
||||||
assert(cs != NULL);
|
assert(cs != NULL);
|
||||||
|
|
||||||
this->cs = cs;
|
this->cs = cs;
|
||||||
this->next = NULL;
|
this->next = NULL;
|
||||||
this->pos = 0; // We start reading from here
|
this->pos = 0; // We start reading from here
|
||||||
this->size = 0;
|
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 */
|
/* Skip the size so we can write that in before sending the packet */
|
||||||
this->pos = 0;
|
this->pos = 0;
|
||||||
this->size = sizeof(PacketSize);
|
this->size = sizeof(PacketSize);
|
||||||
|
this->buffer = MallocT<byte>(SEND_MTU);
|
||||||
this->buffer[this->size++] = type;
|
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
|
* 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)
|
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;
|
this->buffer[this->size++] = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Packet::Send_uint16(uint16 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, 0, 8);
|
||||||
this->buffer[this->size++] = GB(data, 8, 8);
|
this->buffer[this->size++] = GB(data, 8, 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Packet::Send_uint32(uint32 data)
|
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, 0, 8);
|
||||||
this->buffer[this->size++] = GB(data, 8, 8);
|
this->buffer[this->size++] = GB(data, 8, 8);
|
||||||
this->buffer[this->size++] = GB(data, 16, 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)
|
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, 0, 8);
|
||||||
this->buffer[this->size++] = GB(data, 8, 8);
|
this->buffer[this->size++] = GB(data, 8, 8);
|
||||||
this->buffer[this->size++] = GB(data, 16, 8);
|
this->buffer[this->size++] = GB(data, 16, 8);
|
||||||
|
@ -121,7 +131,7 @@ void Packet::Send_string(const char *data)
|
||||||
{
|
{
|
||||||
assert(data != NULL);
|
assert(data != NULL);
|
||||||
/* The <= *is* valid due to the fact that we are comparing sizes and not the index. */
|
/* 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') {}
|
while ((this->buffer[this->size++] = *data++) != '\0') {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,14 +42,17 @@ struct Packet {
|
||||||
PacketSize size;
|
PacketSize size;
|
||||||
/** The current read/write position in the packet */
|
/** The current read/write position in the packet */
|
||||||
PacketSize pos;
|
PacketSize pos;
|
||||||
/** The buffer of this packet */
|
/** The buffer of this packet, of basically variable length up to SEND_MTU. */
|
||||||
byte buffer[SEND_MTU];
|
byte *buffer;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
/** Socket we're associated with. */
|
||||||
NetworkSocketHandler *cs;
|
NetworkSocketHandler *cs;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Packet(NetworkSocketHandler *cs);
|
Packet(NetworkSocketHandler *cs);
|
||||||
Packet(PacketType type);
|
Packet(PacketType type);
|
||||||
|
~Packet();
|
||||||
|
|
||||||
/* Sending/writing of packets */
|
/* Sending/writing of packets */
|
||||||
void PrepareToSend();
|
void PrepareToSend();
|
||||||
|
|
|
@ -63,6 +63,11 @@ void NetworkTCPSocketHandler::Send_Packet(Packet *packet)
|
||||||
|
|
||||||
packet->PrepareToSend();
|
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 */
|
/* Locate last packet buffered for the client */
|
||||||
p = this->packet_queue;
|
p = this->packet_queue;
|
||||||
if (p == NULL) {
|
if (p == NULL) {
|
||||||
|
|
|
@ -121,12 +121,11 @@ void NetworkUDPSocketHandler::ReceivePackets()
|
||||||
memset(&client_addr, 0, sizeof(client_addr));
|
memset(&client_addr, 0, sizeof(client_addr));
|
||||||
|
|
||||||
Packet p(this);
|
Packet p(this);
|
||||||
int packet_len = sizeof(p.buffer);
|
|
||||||
socklen_t client_len = sizeof(client_addr);
|
socklen_t client_len = sizeof(client_addr);
|
||||||
|
|
||||||
/* Try to receive anything */
|
/* Try to receive anything */
|
||||||
SetNonBlocking(s->second); // Some OSes seem to lose the non-blocking status of the socket
|
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. */
|
/* We got some bytes for the base header of the packet. */
|
||||||
if (nbytes > 2) {
|
if (nbytes > 2) {
|
||||||
|
|
Loading…
Reference in New Issue