Packets now dynamically allocates more memory when they need it.

Increased local receive buffer for Client and Server() and removed the magic number that was used
In Server.h and Client.h
(char readBuffer[INPUTSIZE] = { 0 }).
Packets start size has now been increased to 512 bytes.
Changed ComponentInfo::FieldsInOrder to store strings instead of pointers.
Removed HasEntity() and are now using ValidEntity() instead.
This commit is contained in:
Jocke
2016-01-12 12:00:19 +01:00
parent b37ffc7449
commit 236230d12c
13 changed files with 307 additions and 356 deletions
+26 -6
View File
@@ -35,13 +35,13 @@ void Packet::Init(MessageType type, unsigned int & packetID)
packetID++;
}
void Packet::WriteString(std::string str)
void Packet::WriteString(const std::string& str)
{
// Message, add one extra byte for null terminator
int sizeOfString = str.size() + 1;
if (m_Offset + sizeOfString > m_MaxPacketSize) {
LOG_WARNING("Package::WriteString(): Data size in packet exceeded maximum package size.\n");
return;
LOG_WARNING("Package::WriteString(): Data size in packet exceeded maximum package size. New size is %i bytes\n", m_MaxPacketSize*2);
resizeData();
}
memcpy(m_Data + m_Offset, str.data(), sizeOfString * sizeof(char));
m_Offset += sizeOfString * sizeof(char);
@@ -50,8 +50,8 @@ void Packet::WriteString(std::string str)
void Packet::WriteData(char * data, int sizeOfData)
{
if (m_Offset + sizeOfData > m_MaxPacketSize) {
LOG_WARNING("Packet::WriteData(): Data size in packet exceeded maximum packet size.\n");
return;
LOG_WARNING("Packet::WriteData(): Data size in packet exceeded maximum packet size. New size is %i bytes\n", m_MaxPacketSize*2);
resizeData();
}
memcpy(m_Data + m_Offset, data, sizeOfData);
m_Offset += sizeOfData;
@@ -78,4 +78,24 @@ char * Packet::ReadData(int SizeOfData)
unsigned int oldReturnDataOffset = m_ReturnDataOffset;
m_ReturnDataOffset += SizeOfData;
return (m_Data + oldReturnDataOffset);
}
}
void Packet::resizeData()
{
// Allocate memory to store our data in
char* holdData = new char[m_MaxPacketSize];
// Copy our data to the newly allocated memory
memcpy(holdData, m_Data, m_Offset);
// Increase max packet size
m_MaxPacketSize = m_MaxPacketSize * 2;
// Delete our data
delete m_Data;
// Allocate twice the memory we had before
m_Data = new char[m_MaxPacketSize];
// Copy our data to new location
memcpy(m_Data, holdData, m_Offset);
// Delete the memory allocated to hold our data
// while we resized the old data container.
delete holdData;
}